finished FromStr implementation.

This commit is contained in:
Jacob VanDomelen 2019-05-31 02:52:54 -07:00
parent 7be7b6282f
commit 219b00b95d
2 changed files with 62 additions and 32 deletions

View file

@ -3,6 +3,7 @@ mod state;
use super::tree;
use uuid::Uuid;
use std::str::FromStr;
impl tree::Tree<Uuid> {
pub fn run_simulation(&self) {
@ -53,8 +54,8 @@ pub fn run_bracket() {
println!("{}\n\n", tree);
let s = format!("{}", tree);
println!("{}\n\n", s);
// let tree2: tree::Tree<Uuid> = FromStr::from_str(&s).expect("");
// println!("{}\n\n", tree2);
let tree2: tree::Tree<Uuid> = tree::Tree::from_str(&s).unwrap();
println!("{}\n\n", tree2);
break;
}
}

View file

@ -53,7 +53,7 @@ fn seperate_nodes(s: &str) -> Result<(&str, &str), ParseTreeError> {
}
stack.pop();
} else if c == '(' && stack.is_empty() {
} else if c == '|' && stack.is_empty() {
result = Ok((&s[..i], &s[i+1..]));
break;
}
@ -62,15 +62,32 @@ fn seperate_nodes(s: &str) -> Result<(&str, &str), ParseTreeError> {
result
}
fn from_str_helper<T>(s: &str) -> Result<Option<Box<Tree<T>>>, ParseTreeError> {
fn from_str_helper<T: FromStr>(s: &str) -> Result<Option<Box<Tree<T>>>, ParseTreeError> {
let mut result = Err(ParseTreeError::new(String::from("Unable to parse tree, string format unrecognized.")));
let emptyre = Regex::new(r"\s*_\s*").unwrap();
let re = Regex::new(r"\(([0-9a-fA-F-]+)\s*:\s*(.*)\)$").unwrap();
let caps = re.captures(s);
if let Some(c) = caps {
result = T::from_str(c.get(1).unwrap().as_str())
.or(Err(ParseTreeError::new(format!("Unable to parse node value: {}", c.get(1).unwrap().as_str()))))
.and_then(|v| {
seperate_nodes(c.get(2).unwrap().as_str()).and_then(|(left, right)| {
from_str_helper(left).and_then(|l| {
from_str_helper(right).and_then(|r| {
Ok(Some(Box::new(Tree::new(v, l, r))))
})
})
})
})
} else if emptyre.is_match(s) {
result = Ok(None);
}
result
}
impl<T: FromStr> FromStr for Tree<T> {
impl<T> FromStr for Tree<T> where T: FromStr {
type Err = ParseTreeError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
@ -79,33 +96,45 @@ impl<T: FromStr> FromStr for Tree<T> {
let caps = re.captures(s);
if let Some(c) = caps {
let val = T::from_str(c.get(1).unwrap().as_str());
result = T::from_str(c.get(1).unwrap().as_str())
.or(Err(ParseTreeError::new(format!("Unable to parse node value: {}", c.get(1).unwrap().as_str()))))
.and_then(|v| {
seperate_nodes(c.get(2).unwrap().as_str()).and_then(|(left, right)| {
from_str_helper(left).and_then(|l| {
from_str_helper(right).and_then(|r| {
Ok(Tree::new(v, l, r))
})
})
})
})
}
if let Ok(v) = val {
// if let Some(c) = caps {
// let val = T::from_str(c.get(1).unwrap().as_str());
match seperate_nodes(c.get(2).unwrap().as_str()) {
Ok((l, r)) => {
match (from_str_helper(l), from_str_helper(r)) {
(Ok(left), Ok(right)) => {
result = Ok(Tree::new(v, left, right));
},
(Err(e), _) => {
result = Err(e);
},
(_, Err(e)) => {
result = Err(e);
}
}
},
Err(e) => {
result = Err(e);
}
}
} else {
result = Err(ParseTreeError::new(format!("Unable to parse node value: {}", c.get(1).unwrap().as_str())));
}
}
// if let Ok(v) = val {
// match seperate_nodes(c.get(2).unwrap().as_str()) {
// Ok((l, r)) => {
// match (from_str_helper(l), from_str_helper(r)) {
// (Ok(left), Ok(right)) => {
// result = Ok(Tree::new(v, left, right));
// },
// (Err(e), _) => {
// result = Err(e);
// },
// (_, Err(e)) => {
// result = Err(e);
// }
// }
// },
// Err(e) => {
// result = Err(e);
// }
// }
// } else {
// result = Err(ParseTreeError::new(format!("Unable to parse node value: {}", c.get(1).unwrap().as_str())));
// }
// }
result
}