aboutsummaryrefslogtreecommitdiff
path: root/src/executor.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/executor.rs')
-rw-r--r--src/executor.rs68
1 files changed, 68 insertions, 0 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"))
+ }
+}