diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 40 |
1 files changed, 4 insertions, 36 deletions
diff --git a/src/main.rs b/src/main.rs index 0c4abde..c1fe2ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,48 +1,16 @@ mod barbfile; +mod executor; use jsonformat::{format_json, Indentation}; use barbfile::BarbFile; +use executor::Executor; use clap::Parser; -use std::collections::HashMap; use std::env; use std::fs; use std::str::FromStr; -use ureq; - -struct Context { - vars: HashMap<String, String>, -} - -impl Context { - pub fn new() -> Context { - Context { - vars: HashMap::new(), - } - } - - pub fn get_var(&self, name: String) -> Option<String> { - self.vars - .get(&name) - .map(|val| val.clone()) - .or_else(|| env::var(name).ok()) - } - - pub fn execute(&self, bfile: BarbFile) -> Result<ureq::Response, String> { - let req = ureq::request(bfile.method_as_string().as_str(), &bfile.url()); - - match bfile.method().takes_body() { - true => match bfile.body() { - Some(body) => req.send_string(body.as_str()), - None => req.call(), - }, - false => req.call(), - } - .map_err(|_| String::from("Error")) - } -} #[derive(Parser, Debug)] #[clap(version)] @@ -73,14 +41,14 @@ impl Args { fn main() { let args = Args::parse(); - let context = Context::new(); + let mut executor = Executor::new(); let bfile = BarbFile::from_str( fs::read_to_string("test.barb") .expect("Failed to read file") .as_str(), ) .expect("Failed to parse file"); - let response = context.execute(bfile).unwrap(); + let response = executor.execute(bfile).unwrap(); if args.print_headers() { println!("{} {}", response.status(), response.status_text()); |