aboutsummaryrefslogtreecommitdiff
path: root/src/barbfile.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/barbfile.rs')
-rw-r--r--src/barbfile.rs76
1 files changed, 72 insertions, 4 deletions
diff --git a/src/barbfile.rs b/src/barbfile.rs
index 858d035..54ee031 100644
--- a/src/barbfile.rs
+++ b/src/barbfile.rs
@@ -4,6 +4,10 @@ use serde_json::Value;
use std::str::FromStr;
use std::string::ToString;
use std::{error::Error, fmt};
+use std::cmp::{Eq, PartialEq, PartialOrd, Ord, Ordering};
+use std::fs;
+use std::path::Path;
+
#[cfg(feature = "jq")]
use jq_rs;
@@ -150,6 +154,7 @@ impl BarbFilter {
&self.name
}
+ #[cfg(test)]
pub fn filter(&self) -> &String {
&self.filter
}
@@ -201,22 +206,23 @@ struct BarbPreamble {
pub url: String,
pub headers: Vec<Header>,
pub filters: Vec<BarbFilter>,
- //pub filter: Option<BarbFilter>,
+ pub dependency: Option<String>,
}
impl BarbPreamble {
- fn new(method: Method, url: String, headers: Vec<Header>, filters: Vec<BarbFilter>) -> Self {
+ fn new(method: Method, url: String, headers: Vec<Header>, filters: Vec<BarbFilter>, dependency: Option<String>) -> Self {
BarbPreamble {
method,
url,
headers,
- //filter: Vec<BarbFilter>,
filters,
+ dependency,
}
}
}
pub struct BarbFile {
+ file_name: String,
preamble: BarbPreamble,
body: Option<String>,
}
@@ -245,6 +251,38 @@ impl BarbFile {
pub fn body(&self) -> &Option<String> {
&self.body
}
+
+ pub fn dependency(&self) -> Option<String> {
+ let dep = self.preamble.dependency.as_ref()?;
+ let dep_path = Path::new(dep);
+
+ if !dep_path.is_absolute() {
+ let my_path = Path::new(&self.file_name).parent().or(Some(Path::new("")))?;
+ return Some(String::from(my_path.join(dep_path).to_str()?));
+ }
+
+ Some(String::from(dep_path.to_str()?))
+ }
+}
+
+impl PartialEq for BarbFile {
+ fn eq(&self, other: &Self) -> bool {
+ self.file_name == other.file_name
+ }
+}
+
+impl Eq for BarbFile {}
+
+impl PartialOrd for BarbFile {
+ fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+ Some(self.cmp(other))
+ }
+}
+
+impl Ord for BarbFile {
+ fn cmp(&self, other: &Self) -> Ordering {
+ self.file_name.cmp(&other.file_name)
+ }
}
fn decode_url_line(line: &str) -> Result<(Method, String), BarbParseError> {
@@ -264,6 +302,18 @@ fn decode_header(line: &str) -> Result<Header, BarbParseError> {
})
}
+impl BarbFile {
+ pub fn from_file(file_name: String) -> Result<Self, BarbParseError> {
+ let mut bfile = Self::from_str(
+ fs::read_to_string(file_name.as_str())
+ .map_err(|_| BarbParseError {})?
+ .as_str()
+ ).map_err(|_| BarbParseError {})?;
+ bfile.file_name = file_name;
+ Ok(bfile)
+ }
+}
+
impl FromStr for BarbFile {
type Err = BarbParseError;
@@ -272,6 +322,7 @@ impl FromStr for BarbFile {
let (method, url) = decode_url_line(lines.next().ok_or(BarbParseError {})?)?;
let mut headers: Vec<Header> = vec![];
let mut filters: Vec<BarbFilter> = vec![];
+ let mut dependency: Option<String> = None;
for line in &mut lines {
if line == "" {
@@ -283,6 +334,10 @@ impl FromStr for BarbFile {
headers.push(decode_header(line).map_err(|_| BarbParseError {})?);
}
+ if let Some('>') = line.chars().nth(1) {
+ dependency = line.get(2..).map(|x| String::from(x));
+ }
+
if BarbFilter::is_match(String::from(line)) {
match BarbFilter::from_str(line) {
Ok(filter) => filters.push(filter),
@@ -294,7 +349,8 @@ impl FromStr for BarbFile {
let body = lines.fold(String::from(""), |acc, x| acc + x);
Ok(BarbFile {
- preamble: BarbPreamble::new(method, url, headers, filters),
+ file_name: String::from(""),
+ preamble: BarbPreamble::new(method, url, headers, filters, dependency),
body: if body == "" { None } else { Some(body) },
})
}
@@ -419,4 +475,16 @@ mod tests {
let subject = String::from(r#"{"status": "OK"}"#);
assert_eq!(path_f.apply(&subject).unwrap(), String::from("OK"));
}
+
+ #[test]
+ fn test_parse_dependency() {
+ let bfile = BarbFile::from_str("#GET^http://test.com\n#>blah.barb").unwrap();
+ assert_eq!(bfile.dependency().as_ref().unwrap(), &String::from("blah.barb"));
+ }
+
+ #[test]
+ fn test_parse_mult_dependency_keeps_last() {
+ let bfile = BarbFile::from_str("#GET^http://test.com\n#>blah.barb\n#>foo.barb\n#>bar.barb").unwrap();
+ assert_eq!(bfile.dependency().as_ref().unwrap(), &String::from("bar.barb"));
+ }
}