aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorBourgond Aries <macocio@gmail.com>2016-03-27 20:46:19 +0200
committerBourgond Aries <macocio@gmail.com>2016-03-27 20:46:19 +0200
commit3517215eab1a2846b1059cf8182cb9d621a6623e (patch)
tree83424e0436d3c557176483ff3e16b5e14eb50665 /src/lib.rs
parentb171205c57f6b8cd7eb3a175946293ab5b41988f (diff)
downloadmilf-rs-3517215eab1a2846b1059cf8182cb9d621a6623e.tar.gz
milf-rs-3517215eab1a2846b1059cf8182cb9d621a6623e.zip
Implement a more advanced algorithm for lookup
The new algorithm allows the explicit usage of "" and '' to denote key names. This is useful for accessing tables or keys that are named in a non-conventional manner.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs52
1 files changed, 50 insertions, 2 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 8afaa3d..9019327 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -181,12 +181,16 @@ impl Value {
/// assert_eq!(no_bar.is_none(), true);
/// ```
pub fn lookup<'a>(&'a self, path: &'a str) -> Option<&'a Value> {
+ let ref path = match Parser::new(path).lookup() {
+ Some(path) => path,
+ None => return None,
+ };
let mut cur_value = self;
if path.len() == 0 {
return Some(cur_value)
}
- for key in path.split('.') {
+ for key in path {
match *cur_value {
Value::Table(ref hm) => {
match hm.get(key) {
@@ -205,8 +209,8 @@ impl Value {
};
Some(cur_value)
- }
+ }
/// Lookups for mutable value at specified path.
///
/// Uses '.' as a path separator.
@@ -278,6 +282,7 @@ impl FromStr for Value {
#[cfg(test)]
mod tests {
use super::Value;
+ use parser::Parser;
#[test]
fn lookup_mut_change() {
@@ -428,4 +433,47 @@ mod tests {
let baz = foo.lookup("foo").unwrap();
assert_eq!(baz.as_str().unwrap(), "bar");
}
+
+ #[test]
+ fn lookup_advanced() {
+ let value: Value = "[table]\n\"value\" = 0".parse().unwrap();
+ let looked = value.lookup("table.\"value\"").unwrap();
+ 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("")]);
+ }
+
}