aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKerhong <edgars.baduns@gmail.com>2015-09-25 19:26:47 +0300
committerKerhong <edgars.baduns@gmail.com>2015-09-25 19:26:47 +0300
commit547e202c15e814830d7e118a88a244becef2d269 (patch)
tree6e1cdcb17a2a5cf753da415b5cfaf7b7f01f62fd /src
parentbb12015a8a830b3c35292ded6f1f933762aa0355 (diff)
downloadmilf-rs-547e202c15e814830d7e118a88a244becef2d269.tar.gz
milf-rs-547e202c15e814830d7e118a88a244becef2d269.zip
Implement Value::lookup for empty path to return self
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index daf780e..b0c8e4b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -155,6 +155,8 @@ impl Value {
///
/// Note: arrays have zero-based indexes.
///
+ /// Note: empty path returns self.
+ ///
/// ```
/// # #![allow(unstable)]
/// let toml = r#"
@@ -180,6 +182,10 @@ impl Value {
/// ```
pub fn lookup<'a>(&'a self, path: &'a str) -> Option<&'a Value> {
let mut cur_value = self;
+ if path.len() == 0 {
+ return Some(cur_value)
+ }
+
for key in path.split('.') {
match cur_value {
&Value::Table(ref hm) => {
@@ -260,4 +266,18 @@ mod tests {
let foo = value.lookup("values.str.foo");
assert!(foo.is_none());
}
+
+ #[test]
+ fn lookup_self() {
+ let value: Value = r#"foo = "bar""#.parse().unwrap();
+
+ let foo = value.lookup("foo").unwrap();
+ assert_eq!(foo.as_str().unwrap(), "bar");
+
+ let foo = value.lookup("").unwrap();
+ assert!(foo.as_table().is_some());
+
+ let baz = foo.lookup("foo").unwrap();
+ assert_eq!(baz.as_str().unwrap(), "bar");
+ }
}