diff options
author | Guillaume Pasquet <dev@etenil.net> | 2021-11-16 22:36:42 +0000 |
---|---|---|
committer | Guillaume Pasquet <dev@etenil.net> | 2021-11-16 22:36:42 +0000 |
commit | 42b945b7289d8e36900074327a811a50f8cc5e15 (patch) | |
tree | 193e964f51cc12902c0fe10067c47e58ee8a54fc /src | |
parent | c9486ece952d2e07a78192a9ae0e7764fdee4164 (diff) |
Working actual queries and basic JSON formatting. No error handling.
Diffstat (limited to 'src')
-rw-r--r-- | src/barbfile.rs | 35 | ||||
-rw-r--r-- | src/main.rs | 17 |
2 files changed, 48 insertions, 4 deletions
diff --git a/src/barbfile.rs b/src/barbfile.rs index be7cead..31c1eac 100644 --- a/src/barbfile.rs +++ b/src/barbfile.rs @@ -1,9 +1,10 @@ use std::matches; use std::str::FromStr; +use std::string::ToString; use std::{error::Error, fmt}; #[derive(Debug)] -struct BarbParseError {} +pub struct BarbParseError {} impl Error for BarbParseError {} @@ -36,6 +37,18 @@ impl FromStr for Method { } } +impl ToString for Method { + fn to_string(&self) -> String { + match self { + Self::GET => String::from("GET"), + Self::PUT => String::from("PUT"), + Self::POST => String::from("POST"), + Self::PATCH => String::from("PATCH"), + Self::DELETE => String::from("DELETE"), + } + } +} + #[derive(Debug)] struct Header { name: String, @@ -66,11 +79,29 @@ impl BarbHeader { } } -struct BarbFile { +pub struct BarbFile { header: BarbHeader, body: Option<String>, } +impl BarbFile { + pub fn headers(&self) -> &Vec<Header> { + &self.header.headers + } + + pub fn method(&self) -> &Method { + &self.header.method + } + + pub fn method_as_string(&self) -> String { + self.header.method.to_string() + } + + pub fn url(&self) -> &String { + &self.header.url + } +} + fn decode_url_line(line: &str) -> Result<(Method, String), BarbParseError> { let mut components = line[1..].split('^'); let meth = components.next().ok_or(BarbParseError {})?; diff --git a/src/main.rs b/src/main.rs index 816bebc..b6255d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,22 @@ mod barbfile; +use jsonformat::{format_json, Indentation}; + use std::fs; +use std::str::FromStr; use ureq; fn main() { - let barbfile = fs::read_to_string("test.barb").expect("Failed to read file"); - ureq::get("https://api.met.no/weatherapi/tafmetar/1.0/tafmetar?icao=EGKK"); + let bfile = barbfile::BarbFile::from_str( + fs::read_to_string("test.barb") + .expect("Failed to read file") + .as_str(), + ) + .expect("Failed to parse file"); + let result = ureq::request(bfile.method_as_string().as_str(), &bfile.url()) + .call() + .unwrap() + .into_string() + .unwrap(); + println!("{}", format_json(result.as_str(), Indentation::Default)); } |