aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/executor.rs68
-rw-r--r--src/main.rs40
2 files changed, 72 insertions, 36 deletions
diff --git a/src/executor.rs b/src/executor.rs
new file mode 100644
index 0000000..ee17bbf
--- /dev/null
+++ b/src/executor.rs
@@ -0,0 +1,68 @@
+use crate::barbfile::BarbFile;
+
+use std::collections::HashMap;
+use std::env;
+use ureq;
+
+struct Context {
+ vars: HashMap<String, String>,
+}
+
+impl Context {
+ pub fn new() -> Context {
+ let mut vars = HashMap::new();
+ vars.extend(env::vars());
+
+ Context {
+ vars
+ }
+ }
+
+ // pub fn get_var(&self, name: String) -> Option<String> {
+ // self.vars
+ // .get(&name)
+ // .map(|val| val.clone())
+ // .or_else(|| env::var(name).ok())
+ // }
+
+ fn key_str(&self, key: &String) -> String {
+ format!("{{{}}}", key)
+ }
+
+ pub fn substitute(&self, string: &String) -> String {
+ let mut buffer = string.clone();
+ for (key, val) in self.vars.iter() {
+ buffer = buffer.replace(self.key_str(key).as_str(), val);
+ }
+
+ buffer
+ }
+}
+
+pub struct Executor {
+ context: Context
+}
+
+impl Executor {
+ pub fn new() -> Executor {
+ Executor {
+ context: Context::new()
+ }
+ }
+
+ pub fn execute(&mut self, bfile: BarbFile) -> Result<ureq::Response, String> {
+ let req = ureq::request(
+ bfile.method_as_string().as_str(),
+ self.context.substitute(&bfile.url()).as_str()
+ );
+
+ 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"))
+ }
+}
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());