Implement advanced key lookup for lookup_mut

This commit is contained in:
Bourgond Aries 2016-03-27 20:48:23 +02:00
parent 3517215eab
commit 0e3e0654ba

View file

@ -241,12 +241,17 @@ impl Value {
/// assert_eq!(result.as_str().unwrap(), "foo"); /// assert_eq!(result.as_str().unwrap(), "foo");
/// ``` /// ```
pub fn lookup_mut(&mut self, path: &str) -> Option<&mut Value> { pub fn lookup_mut(&mut self, path: &str) -> Option<&mut Value> {
let ref path = match Parser::new(path).lookup() {
Some(path) => path,
None => return None,
};
let mut cur = self; let mut cur = self;
if path.len() == 0 { if path.len() == 0 {
return Some(cur) return Some(cur)
} }
for key in path.split('.') { for key in path {
let tmp = cur; let tmp = cur;
match *tmp { match *tmp {
Value::Table(ref mut hm) => { Value::Table(ref mut hm) => {
@ -282,7 +287,6 @@ impl FromStr for Value {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::Value; use super::Value;
use parser::Parser;
#[test] #[test]
fn lookup_mut_change() { fn lookup_mut_change() {
@ -441,39 +445,4 @@ mod tests {
assert_eq!(*looked, Value::Integer(0)); assert_eq!(*looked, Value::Integer(0));
} }
#[test]
fn lookup_internal() {
let mut parser = Parser::new(r#"hello."world\t".a.0.'escaped'.value"#);
let result = vec![
String::from("hello"),
String::from("world\t"),
String::from("a"),
String::from("0"),
String::from("escaped"),
String::from("value")
];
assert_eq!(parser.lookup().unwrap(), result);
}
#[test]
fn lookup_internal_void() {
let mut parser = Parser::new("");
assert_eq!(parser.lookup().unwrap(), Vec::<String>::new());
}
#[test]
fn lookup_internal_simple() {
let mut parser = Parser::new("value");
assert_eq!(parser.lookup().unwrap(), vec![String::from("value")]);
}
// This is due to key_name not parsing an empty "" correctly. Disabled for now.
#[test]
#[ignore]
fn lookup_internal_quoted_void() {
let mut parser = Parser::new("\"\"");
assert_eq!(parser.lookup().unwrap(), vec![String::from("")]);
}
} }