From b78e38815f0a134678f3352ec655900de2661987 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 30 May 2019 12:27:31 +0700 Subject: Run rustfmt on whole project --- src/delete.rs | 360 ++++++++++++++++++++-------------- src/error.rs | 10 +- src/insert.rs | 141 +++++++------ src/lib.rs | 27 +-- src/log.rs | 25 ++- src/read.rs | 142 +++++++++----- src/resolver/mod.rs | 2 +- src/resolver/mut_creating_resolver.rs | 120 +++++++----- src/resolver/mut_resolver.rs | 164 +++++++++------- src/resolver/non_mut_resolver.rs | 171 +++++++++------- src/set.rs | 184 +++++++++-------- src/tokenizer.rs | 112 ++++++----- src/util.rs | 13 +- src/value.rs | 33 ++-- toml-query_derive/src/lib.rs | 20 +- 15 files changed, 879 insertions(+), 645 deletions(-) diff --git a/src/delete.rs b/src/delete.rs index 6480cea..4398c83 100644 --- a/src/delete.rs +++ b/src/delete.rs @@ -1,13 +1,11 @@ /// The Toml Delete extensions - use toml::Value; -use crate::tokenizer::Token; -use crate::tokenizer::tokenize_with_seperator; use crate::error::{Error, Result}; +use crate::tokenizer::tokenize_with_seperator; +use crate::tokenizer::Token; pub trait TomlValueDeleteExt { - /// Extension function for deleting a value in the current toml::Value document /// using a custom seperator. /// @@ -42,11 +40,9 @@ pub trait TomlValueDeleteExt { fn delete(&mut self, query: &str) -> Result> { self.delete_with_seperator(query, '.') } - } impl TomlValueDeleteExt for Value { - fn delete_with_seperator(&mut self, query: &str, sep: char) -> Result> { use crate::resolver::mut_resolver::resolve; use std::ops::Index; @@ -59,11 +55,11 @@ impl TomlValueDeleteExt for Value { #[inline] fn is_empty(val: Option<&Value>, default: bool) -> bool { val.map(|v| match v { - &Value::Table(ref tab) => tab.is_empty(), - &Value::Array(ref arr) => arr.is_empty(), - _ => default - }) - .unwrap_or(default) + &Value::Table(ref tab) => tab.is_empty(), + &Value::Array(ref arr) => arr.is_empty(), + _ => default, + }) + .unwrap_or(default) } #[inline] @@ -83,124 +79,114 @@ impl TomlValueDeleteExt for Value { if last_token.is_none() { match self { - &mut Value::Table(ref mut tab) => { - match tokens { - Token::Identifier { ident, .. } => { - if is_empty(tab.get(&ident), true) { - Ok(tab.remove(&ident)) + &mut Value::Table(ref mut tab) => match tokens { + Token::Identifier { ident, .. } => { + if is_empty(tab.get(&ident), true) { + Ok(tab.remove(&ident)) + } else { + if is_table(tab.get(&ident)) { + Err(Error::CannotDeleteNonEmptyTable(Some(ident.clone()))) + } else if is_array(tab.get(&ident)) { + Err(Error::CannotDeleteNonEmptyArray(Some(ident.clone()))) } else { - if is_table(tab.get(&ident)) { - Err(Error::CannotDeleteNonEmptyTable(Some(ident.clone()))) - } else if is_array(tab.get(&ident)) { - Err(Error::CannotDeleteNonEmptyArray(Some(ident.clone()))) - } else { - let act = name_of_val(tab.get(&ident)); - let tbl = "table"; - Err(Error::CannotAccessBecauseTypeMismatch(tbl, act)) - } + let act = name_of_val(tab.get(&ident)); + let tbl = "table"; + Err(Error::CannotAccessBecauseTypeMismatch(tbl, act)) } - }, - _ => Ok(None) + } } + _ => Ok(None), }, - &mut Value::Array(ref mut arr) => { - match tokens { - Token::Identifier { ident, .. } => Err(Error::NoIdentifierInArray(ident)), - Token::Index { idx , .. } => { - if is_empty(Some(arr.index(idx)), true) { - Ok(Some(arr.remove(idx))) + &mut Value::Array(ref mut arr) => match tokens { + Token::Identifier { ident, .. } => Err(Error::NoIdentifierInArray(ident)), + Token::Index { idx, .. } => { + if is_empty(Some(arr.index(idx)), true) { + Ok(Some(arr.remove(idx))) + } else { + if is_table(Some(arr.index(idx))) { + Err(Error::CannotDeleteNonEmptyTable(None)) + } else if is_array(Some(arr.index(idx))) { + Err(Error::CannotDeleteNonEmptyArray(None)) } else { - if is_table(Some(arr.index(idx))) { - Err(Error::CannotDeleteNonEmptyTable(None)) - } else if is_array(Some(arr.index(idx))) { - Err(Error::CannotDeleteNonEmptyArray(None)) - } else { - let act = name_of_val(Some(arr.index(idx))); - let tbl = "table"; - Err(Error::CannotAccessBecauseTypeMismatch(tbl, act)) - } + let act = name_of_val(Some(arr.index(idx))); + let tbl = "table"; + Err(Error::CannotAccessBecauseTypeMismatch(tbl, act)) } - }, + } } }, _ => { let kind = match tokens { Token::Identifier { ident, .. } => Error::QueryingValueAsTable(ident), - Token::Index { idx , .. } => Error::QueryingValueAsArray(idx), + Token::Index { idx, .. } => Error::QueryingValueAsArray(idx), }; Err(Error::from(kind)) } } } else { - let val = r#try!(resolve(self, &tokens, true)) - .unwrap(); // safe because of resolve() guarantees + let val = r#try!(resolve(self, &tokens, true)).unwrap(); // safe because of resolve() guarantees let last_token = last_token.unwrap(); match val { - &mut Value::Table(ref mut tab) => { - match *last_token { - Token::Identifier { ref ident, .. } => { - if is_empty(tab.get(ident), true) { - Ok(tab.remove(ident)) + &mut Value::Table(ref mut tab) => match *last_token { + Token::Identifier { ref ident, .. } => { + if is_empty(tab.get(ident), true) { + Ok(tab.remove(ident)) + } else { + if is_table(tab.get(ident)) { + Err(Error::CannotDeleteNonEmptyTable(Some(ident.clone()))) + } else if is_array(tab.get(ident)) { + Err(Error::CannotDeleteNonEmptyArray(Some(ident.clone()))) } else { - if is_table(tab.get(ident)) { - Err(Error::CannotDeleteNonEmptyTable(Some(ident.clone()))) - } else if is_array(tab.get(ident)) { - Err(Error::CannotDeleteNonEmptyArray(Some(ident.clone()))) - } else { - let act = name_of_val(tab.get(ident)); - let tbl = "table"; - Err(Error::CannotAccessBecauseTypeMismatch(tbl, act)) - } + let act = name_of_val(tab.get(ident)); + let tbl = "table"; + Err(Error::CannotAccessBecauseTypeMismatch(tbl, act)) } - }, - Token::Index { idx, .. } => Err(Error::NoIndexInTable(idx)), + } } + Token::Index { idx, .. } => Err(Error::NoIndexInTable(idx)), }, - &mut Value::Array(ref mut arr) => { - match *last_token { - Token::Identifier { ident, .. } => Err(Error::NoIdentifierInArray(ident)), - Token::Index { idx, .. } => { - if idx > arr.len() { - return Err(Error::ArrayIndexOutOfBounds(idx, arr.len())) - } - if is_empty(Some(&arr.index(idx)), true) { - Ok(Some(arr.remove(idx))) + &mut Value::Array(ref mut arr) => match *last_token { + Token::Identifier { ident, .. } => Err(Error::NoIdentifierInArray(ident)), + Token::Index { idx, .. } => { + if idx > arr.len() { + return Err(Error::ArrayIndexOutOfBounds(idx, arr.len())); + } + if is_empty(Some(&arr.index(idx)), true) { + Ok(Some(arr.remove(idx))) + } else { + if is_table(Some(&arr.index(idx))) { + Err(Error::CannotDeleteNonEmptyTable(None)) + } else if is_array(Some(&arr.index(idx))) { + Err(Error::CannotDeleteNonEmptyArray(None)) } else { - if is_table(Some(&arr.index(idx))) { - Err(Error::CannotDeleteNonEmptyTable(None)) - } else if is_array(Some(&arr.index(idx))) { - Err(Error::CannotDeleteNonEmptyArray(None)) - } else { - let act = name_of_val(Some(arr.index(idx))); - let tbl = "table"; - Err(Error::CannotAccessBecauseTypeMismatch(tbl, act)) - } + let act = name_of_val(Some(arr.index(idx))); + let tbl = "table"; + Err(Error::CannotAccessBecauseTypeMismatch(tbl, act)) } - }, + } } }, _ => { let kind = match *last_token { Token::Identifier { ident, .. } => Error::QueryingValueAsTable(ident), - Token::Index { idx, .. } => Error::QueryingValueAsArray(idx), + Token::Index { idx, .. } => Error::QueryingValueAsArray(idx), }; Err(Error::from(kind)) } } } } - } #[cfg(test)] mod test { use super::*; - use toml::Value; use toml::from_str as toml_from_str; + use toml::Value; #[test] fn test_delete_from_empty_document() { - let mut toml : Value = toml_from_str("").unwrap(); + let mut toml: Value = toml_from_str("").unwrap(); let res = toml.delete_with_seperator(&String::from("a"), '.'); @@ -212,9 +198,12 @@ mod test { #[test] fn test_delete_from_empty_table() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" [table] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("table.a"), '.'); @@ -226,9 +215,12 @@ mod test { #[test] fn test_delete_integer() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" value = 1 - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("value"), '.'); @@ -242,9 +234,12 @@ mod test { #[test] fn test_delete_integer_removes_entry_from_document() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" value = 1 - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("value"), '.'); @@ -257,15 +252,18 @@ mod test { match toml { Value::Table(tab) => assert!(tab.is_empty()), - _ => assert!(false, "Strange things are happening"), + _ => assert!(false, "Strange things are happening"), } } #[test] fn test_delete_string() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" value = "foo" - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("value"), '.'); @@ -283,9 +281,12 @@ mod test { #[test] fn test_delete_string_removes_entry_from_document() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" value = "foo" - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("value"), '.'); @@ -302,15 +303,18 @@ mod test { match toml { Value::Table(tab) => assert!(tab.is_empty()), - _ => assert!(false, "Strange things are happening"), + _ => assert!(false, "Strange things are happening"), } } #[test] fn test_delete_empty_table() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" [table] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("table"), '.'); @@ -328,9 +332,12 @@ mod test { #[test] fn test_delete_empty_table_removes_entry_from_document() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" [table] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("table"), '.'); @@ -347,15 +354,18 @@ mod test { match toml { Value::Table(tab) => assert!(tab.is_empty()), - _ => assert!(false, "Strange things are happening"), + _ => assert!(false, "Strange things are happening"), } } #[test] fn test_delete_empty_array() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = [] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("array"), '.'); @@ -373,9 +383,12 @@ mod test { #[test] fn test_delete_empty_array_removes_entry_from_document() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = [] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("array"), '.'); @@ -392,16 +405,19 @@ mod test { match toml { Value::Table(tab) => assert!(tab.is_empty()), - _ => assert!(false, "Strange things are happening"), + _ => assert!(false, "Strange things are happening"), } } #[test] fn test_delete_nonempty_table() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" [table] a = 1 - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("table"), '.'); @@ -413,9 +429,12 @@ mod test { #[test] fn test_delete_nonempty_array() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = [ 1 ] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("array"), '.'); @@ -427,10 +446,13 @@ mod test { #[test] fn test_delete_int_from_table() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" [table] int = 1 - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("table.int"), '.'); @@ -442,10 +464,13 @@ mod test { #[test] fn test_delete_array_from_table() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" [table] array = [] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("table.array"), '.'); @@ -457,10 +482,13 @@ mod test { #[test] fn test_delete_int_from_array_from_table() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" [table] array = [ 1 ] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("table.array.[0]"), '.'); @@ -472,9 +500,12 @@ mod test { #[test] fn test_delete_int_from_array() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = [ 1 ] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("array.[0]"), '.'); @@ -486,9 +517,12 @@ mod test { #[test] fn test_delete_int_from_table_from_array() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = [ { table = { int = 1 } } ] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("array.[0].table.int"), '.'); @@ -502,9 +536,12 @@ mod test { fn test_delete_from_array_value() { use crate::read::TomlValueReadExt; - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = [ 1 ] - "#).unwrap(); + "#, + ) + .unwrap(); let ary = toml.read_mut(&String::from("array")).unwrap().unwrap(); let res = ary.delete_with_seperator(&String::from("[0]"), '.'); @@ -519,9 +556,12 @@ mod test { fn test_delete_from_int_value() { use crate::read::TomlValueReadExt; - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = [ 1 ] - "#).unwrap(); + "#, + ) + .unwrap(); let ary = toml.read_mut(&String::from("array.[0]")).unwrap().unwrap(); let res = ary.delete_with_seperator(&String::from("nonexist"), '.'); @@ -536,9 +576,12 @@ mod test { fn test_delete_index_from_non_array() { use crate::read::TomlValueReadExt; - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = 1 - "#).unwrap(); + "#, + ) + .unwrap(); let ary = toml.read_mut(&String::from("array")).unwrap().unwrap(); let res = ary.delete_with_seperator(&String::from("[0]"), '.'); @@ -551,11 +594,14 @@ mod test { #[test] fn test_delete_index_from_table_in_table() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" table = { another = { int = 1 } } - "#).unwrap(); + "#, + ) + .unwrap(); - let res = toml.delete_with_seperator(&String::from("table.another.[0]"), '.'); + let res = toml.delete_with_seperator(&String::from("table.another.[0]"), '.'); assert!(res.is_err()); @@ -565,11 +611,14 @@ mod test { #[test] fn test_delete_identifier_from_array_in_table() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" table = { another = [ 1, 2, 3, 4, 5, 6 ] } - "#).unwrap(); + "#, + ) + .unwrap(); - let res = toml.delete_with_seperator(&String::from("table.another.nonexist"), '.'); + let res = toml.delete_with_seperator(&String::from("table.another.nonexist"), '.'); assert!(res.is_err()); @@ -579,11 +628,14 @@ mod test { #[test] fn test_delete_nonexistent_array_idx() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = [ 1, 2, 3 ] - "#).unwrap(); + "#, + ) + .unwrap(); - let res = toml.delete_with_seperator(&String::from("array.[22]"), '.'); + let res = toml.delete_with_seperator(&String::from("array.[22]"), '.'); assert!(res.is_err()); @@ -593,9 +645,12 @@ mod test { #[test] fn test_delete_non_empty_array_from_array() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = [ [ 1 ], [ 2 ] ] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("array.[1]"), '.'); @@ -607,9 +662,12 @@ mod test { #[test] fn test_delete_non_empty_table_from_array() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = [ { t = 1 }, { t = 2 } ] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("array.[1]"), '.'); @@ -623,9 +681,12 @@ mod test { fn test_delete_non_empty_table_from_top_level_array() { use crate::read::TomlValueReadExt; - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = [ { t = 1 }, { t = 2 } ] - "#).unwrap(); + "#, + ) + .unwrap(); let ary = toml.read_mut(&String::from("array")).unwrap().unwrap(); let res = ary.delete_with_seperator(&String::from("[1]"), '.'); @@ -638,9 +699,12 @@ mod test { #[test] fn test_delete_from_value_like_it_was_table() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" val = 5 - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("val.foo"), '.'); @@ -652,9 +716,12 @@ mod test { #[test] fn test_delete_from_value_like_it_was_array() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" val = 5 - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.delete_with_seperator(&String::from("val.[0]"), '.'); @@ -665,4 +732,3 @@ mod test { } } - diff --git a/src/error.rs b/src/error.rs index 7f80efe..34708e0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -13,7 +13,6 @@ pub enum Error { TomlDeserialize(#[cause] ::toml::de::Error), // Errors for tokenizer - #[fail(display = "Parsing the query '{}' failed", _0)] QueryParsingError(String), @@ -26,11 +25,12 @@ pub enum Error { #[fail(display = "The passed query tries to access an array but does not specify the index")] ArrayAccessWithoutIndex, - #[fail(display = "The passed query tries to access an array but does not specify a valid index")] + #[fail( + display = "The passed query tries to access an array but does not specify a valid index" + )] ArrayAccessWithInvalidIndex, // Errors for Resolver - #[fail(display = "The identfier '{}' is not present in the document", _0)] IdentifierNotFoundInDocument(String), @@ -56,13 +56,11 @@ pub enum Error { CannotAccessBecauseTypeMismatch(&'static str, &'static str), #[fail(display = "Cannot delete in array at {}, array has length {}", _0, _1)] - ArrayIndexOutOfBounds( usize, usize), + ArrayIndexOutOfBounds(usize, usize), #[fail(display = "Type Error. Requested {}, but got {}", _0, _1)] TypeError(&'static str, &'static str), #[fail(display = "Value at '{}' not there", _0)] NotAvailable(String), - } - diff --git a/src/insert.rs b/src/insert.rs index 5b969d3..85ad22c 100644 --- a/src/insert.rs +++ b/src/insert.rs @@ -4,12 +4,11 @@ use serde::Serialize; use toml::Value; -use crate::tokenizer::Token; -use crate::tokenizer::tokenize_with_seperator; use crate::error::{Error, Result}; +use crate::tokenizer::tokenize_with_seperator; +use crate::tokenizer::Token; pub trait TomlValueInsertExt { - /// Extension function for inserting a value in the current toml::Value document /// using a custom seperator. /// @@ -78,7 +77,12 @@ pub trait TomlValueInsertExt { /// assert!(res.is_ok()); // panics /// ``` /// - fn insert_with_seperator(&mut self, query: &str, sep: char, value: Value) -> Result>; + fn insert_with_seperator( + &mut self, + query: &str, + sep: char, + value: Value, + ) -> Result>; /// Extension function for inserting a value from the current toml::Value document /// @@ -93,55 +97,50 @@ pub trait TomlValueInsertExt { let value = Value::try_from(value).map_err(Error::TomlSerialize)?; self.insert(query, value) } - } impl TomlValueInsertExt for Value { - - fn insert_with_seperator(&mut self, query: &str, sep: char, value: Value) -> Result> { + fn insert_with_seperator( + &mut self, + query: &str, + sep: char, + value: Value, + ) -> Result> { use crate::resolver::mut_creating_resolver::resolve; let mut tokens = r#try!(tokenize_with_seperator(query, sep)); let (val, last) = match tokens.pop_last() { - None => (self, Box::new(tokens)), + None => (self, Box::new(tokens)), Some(last) => (r#try!(resolve(self, &tokens)), last), - }; match *last { - Token::Identifier { ident, .. } => { - match val { - &mut Value::Table(ref mut t) => { - Ok(t.insert(ident, value)) - }, - _ => Err(Error::NoIdentifierInArray(ident.clone())) - } + Token::Identifier { ident, .. } => match val { + &mut Value::Table(ref mut t) => Ok(t.insert(ident, value)), + _ => Err(Error::NoIdentifierInArray(ident.clone())), }, - Token::Index { idx , .. } => { - match val { - &mut Value::Array(ref mut a) => { - if a.len() > idx { - a.insert(idx, value); - Ok(None) - } else { - a.push(value); - Ok(None) - } - }, - _ => Err(Error::NoIndexInTable(idx)) + Token::Index { idx, .. } => match val { + &mut Value::Array(ref mut a) => { + if a.len() > idx { + a.insert(idx, value); + Ok(None) + } else { + a.push(value); + Ok(None) + } } + _ => Err(Error::NoIndexInTable(idx)), }, } } - } #[cfg(test)] mod test { use super::*; - use toml::Value; use toml::from_str as toml_from_str; + use toml::Value; #[test] fn test_insert_one_token() { @@ -161,20 +160,27 @@ mod test { assert!(!t.is_empty()); let val = t.get("value"); - assert!(val.is_some(), "'value' from table {:?} should be Some(_), is None", t); + assert!( + val.is_some(), + "'value' from table {:?} should be Some(_), is None", + t + ); let val = val.unwrap(); assert!(is_match!(val, &Value::Integer(1)), "Is not one: {:?}", val); - }, + } _ => panic!("What just happenend?"), } } #[test] fn test_insert_with_seperator_into_table() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" [table] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.insert_with_seperator(&String::from("table.a"), '.', Value::Integer(1)); @@ -202,10 +208,10 @@ mod test { let a = a.unwrap(); assert!(is_match!(a, &Value::Integer(1))); - }, + } _ => panic!("What just happenend?"), } - }, + } _ => panic!("What just happenend?"), } } @@ -214,9 +220,12 @@ mod test { fn test_insert_with_seperator_into_array() { use std::ops::Index; - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = [] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.insert_with_seperator(&String::from("array.[0]"), '.', Value::Integer(1)); @@ -239,19 +248,22 @@ mod test { &Value::Array(ref a) => { assert!(!a.is_empty()); assert!(is_match!(a.index(0), &Value::Integer(1))); - }, + } _ => panic!("What just happenend?"), } - }, + } _ => panic!("What just happenend?"), } } #[test] fn test_insert_with_seperator_into_nested_table() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" [a.b.c] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.insert_with_seperator(&String::from("a.b.c.d"), '.', Value::Integer(1)); @@ -296,25 +308,28 @@ mod test { let d = d.unwrap(); assert!(is_match!(d, &Value::Integer(1))); - }, + } _ => panic!("What just happenend?"), } - }, + } _ => panic!("What just happenend?"), } - }, + } _ => panic!("What just happenend?"), } - }, + } _ => panic!("What just happened?"), } } #[test] fn test_insert_with_seperator_into_table_where_array_is() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" table = [] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.insert_with_seperator(&String::from("table.a"), '.', Value::Integer(1)); @@ -326,9 +341,12 @@ mod test { #[test] fn test_insert_with_seperator_into_array_where_table_is() { - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" [table] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.insert_with_seperator(&String::from("table.[0]"), '.', Value::Integer(1)); @@ -342,9 +360,12 @@ mod test { fn test_insert_with_seperator_into_array_between_values() { use std::ops::Index; - let mut toml : Value = toml_from_str(r#" + let mut toml: Value = toml_from_str( + r#" array = [1, 2, 3, 4, 5] - "#).unwrap(); + "#, + ) + .unwrap(); let res = toml.insert_with_seperator(&String::from("array.[2]"), '.', Value::Integer(6)); @@ -372,18 +393,21 @@ mod test { assert!(is_match!(a.index(3), &Value::Integer(3))); assert!(is_match!(a.index(4), &Value::Integer(4))); assert!(is_match!(a.index(5), &Value::Integer(5))); - }, + } _ => panic!("What just happenend?"), } - }, + } _ => panic!("What just happenend?"), } } #[test] fn test_insert_with_seperator_into_table_with_nonexisting_keys() { - let mut toml : Value = toml_from_str(r#" - "#).unwrap(); + let mut toml: Value = toml_from_str( + r#" + "#, + ) + .unwrap(); let res = toml.insert_with_seperator(&String::from("table.a"), '.', Value::Integer(1)); @@ -411,13 +435,12 @@ mod test { let a = a.unwrap(); assert!(is_match!(a, &Value::Integer(1))); - }, + } _ => panic!("What just happenend?"), } - }, + } _ => panic!("What just happenend?"), } } } - diff --git a/src/lib.rs b/src/lib.rs index 43b6b4b..cb658de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,15 +8,19 @@ // external crates -#[macro_use] extern crate is_match; -#[macro_use] extern crate lazy_static; -#[macro_use] extern crate failure_derive; +#[macro_use] +extern crate is_match; +#[macro_use] +extern crate lazy_static; +#[macro_use] +extern crate failure_derive; extern crate failure; extern crate regex; extern crate toml; #[cfg(feature = "log")] -#[macro_use] extern crate log; +#[macro_use] +extern crate log; #[cfg(feature = "typed")] extern crate serde; @@ -26,28 +30,29 @@ extern crate serde; extern crate serde_derive; #[cfg(test)] -#[macro_use] extern crate quickcheck; +#[macro_use] +extern crate quickcheck; // public modules #[cfg(not(feature = "log"))] -#[macro_use] pub mod log; +#[macro_use] +pub mod log; extern crate toml_query_derive; #[doc(hidden)] pub use toml_query_derive::*; +pub mod delete; pub mod error; +pub mod insert; pub mod read; pub mod set; -pub mod insert; -pub mod delete; -pub mod value; mod util; +pub mod value; // private modules -mod tokenizer; mod resolver; - +mod tokenizer; diff --git a/src/log.rs b/src/log.rs index 1ff777e..92cf6cb 100644 --- a/src/log.rs +++ b/src/log.rs @@ -7,8 +7,8 @@ #[cfg(not(feature = "logging"))] #[macro_export] macro_rules! debug { - (target: $target:expr, $($arg:tt)*) => { }; - ($($arg:tt)*) => { }; + (target: $target:expr, $($arg:tt)*) => {}; + ($($arg:tt)*) => {}; } /// This macro is defined if the `logging` feature is _not_ compiled into the library @@ -17,8 +17,8 @@ macro_rules! debug { #[cfg(not(feature = "logging"))] #[macro_export] macro_rules! error { - (target: $target:expr, $($arg:tt)*) => { }; - ($($arg:tt)*) => { }; + (target: $target:expr, $($arg:tt)*) => {}; + ($($arg:tt)*) => {}; } /// This macro is defined if the `logging` feature is _not_ compiled into the library @@ -27,8 +27,8 @@ macro_rules! error { #[cfg(not(feature = "logging"))] #[macro_export] macro_rules! info { - (target: $target:expr, $($arg:tt)*) => { }; - ($($arg:tt)*) => { }; + (target: $target:expr, $($arg:tt)*) => {}; + ($($arg:tt)*) => {}; } /// This macro is defined if the `logging` feature is _not_ compiled into the library @@ -37,8 +37,8 @@ macro_rules! info { #[cfg(not(feature = "logging"))] #[macro_export] macro_rules! log { - (target: $target:expr, $($arg:tt)*) => { }; - ($($arg:tt)*) => { }; + (target: $target:expr, $($arg:tt)*) => {}; + ($($arg:tt)*) => {}; } /// This macro is defined if the `logging` feature is _not_ compiled into the library @@ -47,8 +47,8 @@ macro_rules! log { #[cfg(not(feature = "logging"))] #[macro_export] macro_rules! trace { - (target: $target:expr, $($arg:tt)*) => { }; - ($($arg:tt)*) => { }; + (target: $target:expr, $($arg:tt)*) => {}; + ($($arg:tt)*) => {}; } /// This macro is defined if the `logging` feature is _not_ compiled into the library @@ -57,7 +57,6 @@ macro_rules! trace { #[cfg(not(feature = "logging"))] #[macro_export] macro_rules! warn { - (target: $target:expr, $($arg:tt)*) => { }; - ($($arg:tt)*) => { }; + (target: $target:expr, $($arg:tt)*) => {}; + ($($arg:tt)*) => {}; } - diff --git a/src/read.rs b/src/read.rs index 3df289a..356769e 100644 --- a/src/read.rs +++ b/src/read.rs @@ -4,21 +4,24 @@ use std::fmt::Debug; #[cfg(feature = "typed")] -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; use toml::Value; -use crate::tokenizer::tokenize_with_seperator; use crate::error::{Error, Result}; +use crate::tokenizer::tokenize_with_seperator; pub trait TomlValueReadExt<'doc> { - /// Extension function for reading a value from the current toml::Value document /// using a custom seperator fn read_with_seperator(&'doc self, query: &str, sep: char) -> Result>; /// Extension function for reading a value from the current toml::Value document mutably /// using a custom seperator - fn read_mut_with_seperator(&'doc mut self, query: &str, sep: char) -> Result>; + fn read_mut_with_seperator( + &'doc mut self, + query: &str, + sep: char, + ) -> Result>; /// Extension function for reading a value from the current toml::Value document fn read(&'doc self, query: &str) -> Result> { @@ -39,7 +42,7 @@ pub trait TomlValueReadExt<'doc> { let deserialized = value.clone().try_into().map_err(Error::TomlDeserialize)?; Ok(Some(deserialized)) } - None => Ok(None) + None => Ok(None), } } @@ -59,28 +62,29 @@ pub trait Partial<'a> { type Output: Serialize + Deserialize<'a> + Debug; } - impl<'doc> TomlValueReadExt<'doc> for Value { - fn read_with_seperator(&'doc self, query: &str, sep: char) -> Result> { use crate::resolver::non_mut_resolver::resolve; tokenize_with_seperator(query, sep).and_then(move |tokens| resolve(self, &tokens, false)) } - fn read_mut_with_seperator(&'doc mut self, query: &str, sep: char) -> Result> { + fn read_mut_with_seperator( + &'doc mut self, + query: &str, + sep: char, + ) -> Result> { use crate::resolver::mut_resolver::resolve; tokenize_with_seperator(query, sep).and_then(move |tokens| resolve(self, &tokens, false)) } - } -pub trait TomlValueReadTypeExt<'doc> : TomlValueReadExt<'doc> { +pub trait TomlValueReadTypeExt<'doc>: TomlValueReadExt<'doc> { fn read_string(&'doc self, query: &str) -> Result>; - fn read_int(&'doc self, query: &str) -> Result>; - fn read_float(&'doc self, query: &str) -> Result>; - fn read_bool(&'doc self, query: &str) -> Result>; + fn read_int(&'doc self, query: &str) -> Result>; + fn read_float(&'doc self, query: &str) -> Result>; + fn read_bool(&'doc self, query: &str) -> Result>; } macro_rules! make_type_getter { @@ -96,7 +100,8 @@ macro_rules! make_type_getter { } impl<'doc, T> TomlValueReadTypeExt<'doc> for T - where T: TomlValueReadExt<'doc> +where + T: TomlValueReadExt<'doc>, { make_type_getter!(read_string, String, "String", Some(&Value::String(ref obj)) => obj.clone()); make_type_getter!(read_int, i64, "Integer", Some(&Value::Integer(obj)) => obj); @@ -111,9 +116,9 @@ mod test { #[test] fn test_read_empty() { - let toml : Value = toml_from_str("").unwrap(); + let toml: Value = toml_from_str("").unwrap(); - let val = toml.read_with_seperator(&String::from("a"), '.'); + let val = toml.read_with_seperator(&String::from("a"), '.'); assert!(val.is_ok()); let val = val.unwrap(); @@ -123,11 +128,14 @@ mod test { #[test] fn test_read_table() { - let toml : Value = toml_from_str(r#" + let toml: Value = toml_from_str( + r#" [table] - "#).unwrap(); + "#, + ) + .unwrap(); - let val = toml.read_with_seperator(&String::from("table"), '.'); + let val = toml.read_with_seperator(&String::from("table"), '.'); assert!(val.is_ok()); let val = val.unwrap(); @@ -144,12 +152,15 @@ mod test { #[test] fn test_read_table_value() { - let toml : Value = toml_from_str(r#" + let toml: Value = toml_from_str( + r#" [table] a = 1 - "#).unwrap(); + "#, + ) + .unwrap(); - let val = toml.read_with_seperator(&String::from("table.a"), '.'); + let val = toml.read_with_seperator(&String::from("table.a"), '.'); assert!(val.is_ok()); let val = val.unwrap(); @@ -162,11 +173,14 @@ mod test { #[test] fn test_read_empty_table_value() { - let toml : Value = toml_from_str(r#" + let toml: Value = toml_from_str( + r#" [table] - "#).unwrap(); + "#, + ) + .unwrap(); - let val = toml.read_with_seperator(&String::from("table.a"), '.'); + let val = toml.read_with_seperator(&String::from("table.a"), '.'); assert!(val.is_ok()); let val = val.unwrap(); @@ -175,11 +189,14 @@ mod test { #[test] fn test_read_table_index() { - let toml : Value = toml_from_str(r#" + let toml: Value = toml_from_str( + r#" [table] - "#).unwrap(); + "#, + ) + .unwrap(); - let val = toml.read_with_seperator(&String::from("table.[0]"), '.'); + let val = toml.read_with_seperator(&String::from("table.[0]"), '.'); assert!(val.is_err()); let err = val.unwrap_err(); @@ -194,9 +211,9 @@ mod test { #[test] fn test_read_empty_without_seperator() { - let toml : Value = toml_from_str("").unwrap(); + let toml: Value = toml_from_str("").unwrap(); - let val = toml.read(&String::from("a")); + let val = toml.read(&String::from("a")); assert!(val.is_ok()); let val = val.unwrap(); @@ -205,11 +222,14 @@ mod test { #[test] fn test_read_table_without_seperator() { - let toml : Value = toml_from_str(r#" + let toml: Value = toml_from_str( + r#" [table] - "#).unwrap(); + "#, + ) + .unwrap(); - let val = toml.read(&String::from("table")); + let val = toml.read(&String::from("table")); assert!(val.is_ok()); let val = val.unwrap(); @@ -226,12 +246,15 @@ mod test { #[test] fn test_read_table_value_without_seperator() { - let toml : Value = toml_from_str(r#" + let toml: Value = toml_from_str( + r#" [table] a = 1 - "#).unwrap(); + "#, + ) + .unwrap(); - let val = toml.read(&String::from("table.a")); + let val = toml.read(&String::from("table.a")); assert!(val.is_ok()); let val = val.unwrap(); @@ -244,11 +267,14 @@ mod test { #[test] fn test_read_empty_table_value_without_seperator() { - let toml : Value = toml_from_str(r#" + let toml: Value = toml_from_str( + r#" [table] - "#).unwrap(); + "#, + ) + .unwrap(); - let val = toml.read(&String::from("table.a")); + let val = toml.read(&String::from("table.a")); assert!(val.is_ok()); let val = val.unwrap(); @@ -257,11 +283,14 @@ mod test { #[test] fn test_read_table_index_without_seperator() { - let toml : Value = toml_from_str(r#" + let toml: Value = toml_from_str( + r#" [table] - "#).unwrap(); + "#, + ) + .unwrap(); - let val = toml.read(&String::from("table.[0]")); + let val = toml.read(&String::from("table.[0]")); assert!(val.is_err()); let err = val.unwrap_err(); @@ -277,10 +306,13 @@ mod high_level_fn_test { #[test] fn test_read_table_value() { - let toml : Value = toml_from_str(r#" + let toml: Value = toml_from_str( + r#" [table] a = 1 - "#).unwrap(); + "#, + ) + .unwrap(); let val = toml.read_int("table.a").unwrap(); @@ -290,10 +322,13 @@ mod high_level_fn_test { #[cfg(feature = "typed")] #[test] fn test_name() { - let toml : Value = toml_from_str(r#" + let toml: Value = toml_from_str( + r#" [table] a = 1 - "#).unwrap(); + "#, + ) + .unwrap(); let val: u32 = toml.read_deserialized("table.a").unwrap().unwrap(); @@ -303,9 +338,9 @@ mod high_level_fn_test { #[cfg(feature = "typed")] #[test] fn test_deser() { - use toml::map::Map; use crate::insert::TomlValueInsertExt; use crate::read::TomlValueReadExt; + use toml::map::Map; #[derive(Serialize, Deserialize, Debug)] struct Test { @@ -314,13 +349,16 @@ mod high_level_fn_test { } let mut toml = Value::Table(Map::new()); - let test = Test { + let test = Test { a: 15, s: String::from("Helloworld"), }; - assert!(toml.insert_serialized("table.value", test).unwrap().is_none()); - let _ : Test = toml.read_deserialized("table.value").unwrap().unwrap(); + assert!(toml + .insert_serialized("table.value", test) + .unwrap() + .is_none()); + let _: Test = toml.read_deserialized("table.value").unwrap().unwrap(); assert!(true); } @@ -341,7 +379,7 @@ mod partial_tests { impl<'a> Partial<'a> for TestObj { const LOCATION: &'static str = "foo"; - type Output = Self; + type Output = Self; } #[test] @@ -356,7 +394,7 @@ mod partial_tests { Value::Table(tbl) }; - let obj : TestObj = tbl.read_partial::().unwrap().unwrap(); + let obj: TestObj = tbl.read_partial::().unwrap().unwrap(); assert_eq!(obj.value, "foobar"); } diff --git a/src/resolver/mod.rs b/src/resolver/mod.rs index c628f9b..3a597ee 100644 --- a/src/resolver/mod.rs +++ b/src/resolver/mod.rs @@ -1,3 +1,3 @@ -pub mod mut_resolver; pub mod mut_creating_resolver; +pub mod mut_resolver; pub mod non_mut_resolver; diff --git a/src/resolver/mut_creating_resolver.rs b/src/resolver/mut_creating_resolver.rs index fc09152..5e025be 100644 --- a/src/resolver/mut_creating_resolver.rs +++ b/src/resolver/mut_creating_resolver.rs @@ -1,11 +1,9 @@ +use crate::error::{Error, Result}; +use crate::tokenizer::Token; /// The query resolver that operates on the AST and the TOML object - use toml::{map::Map, Value}; -use crate::tokenizer::Token; -use crate::error::{Error, Result}; pub fn resolve<'doc>(toml: &'doc mut Value, tokens: &Token) -> Result<&'doc mut Value> { - // Cases: // // 1. Identifier, toml: table, ident present -> traverse @@ -21,29 +19,27 @@ pub fn resolve<'doc>(toml: &'doc mut Value, tokens: &Token) -> Result<&'doc mut // then traverse match *tokens { - Token::Identifier { ref ident, .. } => { - match toml { - &mut Value::Table(ref mut t) => { - if t.contains_key(ident) { - match tokens.next() { - Some(next) => resolve(t.get_mut(ident).unwrap(), next), - None => t.get_mut(ident).ok_or_else(|| unreachable!()), - } - } else { - match tokens.next() { - Some(next) => { - let subdoc = t.entry(ident.clone()).or_insert(Value::Table(Map::new())); - resolve(subdoc, next) - }, - None => Ok(t.entry(ident.clone()).or_insert(Value::Table(Map::new()))), + Token::Identifier { ref ident, .. } => match toml { + &mut Value::Table(ref mut t) => { + if t.contains_key(ident) { + match tokens.next() { + Some(next) => resolve(t.get_mut(ident).unwrap(), next), + None => t.get_mut(ident).ok_or_else(|| unreachable!()), + } + } else { + match tokens.next() { + Some(next) => { + let subdoc = t.entry(ident.clone()).or_insert(Value::Table(Map::new())); + resolve(subdoc, next) } + None => Ok(t.entry(ident.clone()).or_insert(Value::Table(Map::new()))), } - }, - &mut Value::Array(_) => Err(Error::NoIdentifierInArray(ident.clone())), - _ => unimplemented!() + } } - } - Token::Index { idx , .. } => { + &mut Value::Array(_) => Err(Error::NoIdentifierInArray(ident.clone())), + _ => unimplemented!(), + }, + Token::Index { idx, .. } => { match toml { &mut Value::Table(_) => Err(Error::NoIndexInTable(idx)), &mut Value::Array(ref mut ary) => { @@ -57,7 +53,7 @@ pub fn resolve<'doc>(toml: &'doc mut Value, tokens: &Token) -> Result<&'doc mut match **next { Token::Identifier { .. } => { ary.push(Value::Table(Map::new())); - }, + } Token::Index { .. } => { ary.push(Value::Array(vec![])); } @@ -69,7 +65,7 @@ pub fn resolve<'doc>(toml: &'doc mut Value, tokens: &Token) -> Result<&'doc mut } } } - _ => unimplemented!() + _ => unimplemented!(), } } } @@ -77,15 +73,18 @@ pub fn resolve<'doc>(toml: &'doc mut Value, tokens: &Token) -> Result<&'doc mut #[cfg(test)] mod test { + use super::resolve; + use crate::tokenizer::*; use toml::from_str as toml_from_str; use toml::Value; - use crate::tokenizer::*; - use super::resolve; macro_rules! do_resolve { ( $toml:ident => $query:expr ) => { - resolve(&mut $toml, &tokenize_with_seperator(&String::from($query), '.').unwrap()) - } + resolve( + &mut $toml, + &tokenize_with_seperator(&String::from($query), '.').unwrap(), + ) + }; } #[test] @@ -164,7 +163,7 @@ mod test { &mut Value::Array(ref ary) => { assert_eq!(ary[0], Value::Boolean(true)); assert_eq!(ary[1], Value::Boolean(false)); - }, + } _ => panic!("What just happened?"), } } @@ -182,7 +181,7 @@ mod test { &mut Value::Array(ref ary) => { assert_eq!(ary[0], Value::Integer(1)); assert_eq!(ary[1], Value::Integer(1337)); - }, + } _ => panic!("What just happened?"), } } @@ -202,7 +201,7 @@ mod test { assert_eq!(ary[0].as_float(), Some(1.0)); assert!(is_match!(ary[1], Value::Float(_))); assert_eq!(ary[1].as_float(), Some(133.25)); - }, + } _ => panic!("What just happened?"), } } @@ -231,10 +230,13 @@ mod test { #[test] fn test_resolve_table_element_query() { - let mut toml = toml_from_str(r#" + let mut toml = toml_from_str( + r#" [table] value = 42 - "#).unwrap(); + "#, + ) + .unwrap(); let result = do_resolve!(toml => "table.value"); assert!(result.is_ok()); @@ -245,14 +247,17 @@ mod test { #[test] fn test_resolve_table_with_many_elements_element_query() { - let mut toml = toml_from_str(r#" + let mut toml = toml_from_str( + r#" [table] value1 = 42 value2 = 43 value3 = 44 value4 = 45 value5 = 46 - "#).unwrap(); + "#, + ) + .unwrap(); let result = do_resolve!(toml => "table.value1"); assert!(result.is_ok()); @@ -263,10 +268,13 @@ mod test { #[test] fn test_resolve_table_array_query() { - let mut toml = toml_from_str(r#" + let mut toml = toml_from_str( + r#" [table] value1 = [ 42.0, 50.0 ] - "#).unwrap(); + "#, + ) + .unwrap(); let result = do_resolve!(toml => "table.value1"); assert!(result.is_ok()); @@ -279,17 +287,20 @@ mod test { assert_eq!(ary[0].as_float(), Some(42.0)); assert!(is_match!(ary[1], Value::Float(_))); assert_eq!(ary[1].as_float(), Some(50.0)); - }, + } _ => panic!("What just happened?"), } } #[test] fn test_resolve_table_array_element_query() { - let mut toml = toml_from_str(r#" + let mut toml = toml_from_str( + r#" [table] value1 = [ 42 ] - "#).unwrap(); + "#, + ) + .unwrap(); let result = do_resolve!(toml => "table.value1.[0]"); assert!(result.is_ok()); @@ -300,7 +311,8 @@ mod test { #[test] fn test_resolve_multi_table_query() { - let mut toml = toml_from_str(r#" + let mut toml = toml_from_str( + r#" [table0] value = [ 1 ] [table1] @@ -309,7 +321,9 @@ mod test { value = [ 42.0 ] [table3] value = [ true ] - "#).unwrap(); + "#, + ) + .unwrap(); let result = do_resolve!(toml => "table1.value.[0]"); assert!(result.is_ok()); @@ -322,7 +336,7 @@ mod test { } } - static FRUIT_TABLE : &'static str = r#" + static FRUIT_TABLE: &'static str = r#" [[fruit.blah]] name = "apple" @@ -372,7 +386,7 @@ mod test { Some(&Value::String(ref s)) => assert_eq!("round", s), _ => assert!(false), } - }, + } _ => panic!("What just happened?"), } } @@ -400,9 +414,12 @@ mod test { #[test] fn test_resolve_query_empty_table() { - let mut toml = toml_from_str(r#" + let mut toml = toml_from_str( + r#" [example] - "#).unwrap(); + "#, + ) + .unwrap(); let result = do_resolve!(toml => "example"); assert!(result.is_ok()); @@ -425,7 +442,7 @@ mod test { assert!(is_match!(result, &mut Value::Table(_))); match result { &mut Value::Table(ref t) => assert!(t.is_empty()), - _ => panic!("What just happened?"), + _ => panic!("What just happened?"), } } @@ -457,7 +474,7 @@ mod test { match result { &mut Value::Table(ref t) => assert!(t.is_empty()), - _ => panic!("What just happened?"), + _ => panic!("What just happened?"), } } @@ -471,7 +488,7 @@ mod test { match result { &mut Value::Table(ref t) => assert!(t.is_empty()), - _ => panic!("What just happened?"), + _ => panic!("What just happened?"), } } @@ -493,4 +510,3 @@ mod test { } } - diff --git a/src/resolver/mut_resolver.rs b/src/resolver/mut_resolver.rs index dc8d7d4..3119766 100644 --- a/src/resolver/mut_resolver.rs +++ b/src/resolver/mut_resolver.rs @@ -1,10 +1,9 @@ /// The query resolver that operates on the AST and the TOML object - use std::ops::IndexMut; -use toml::Value; -use crate::tokenizer::Token; use crate::error::{Error, Result}; +use crate::tokenizer::Token; +use toml::Value; /// Resolves the path in the passed document recursively /// @@ -12,61 +11,61 @@ use crate::error::{Error, Result}; /// /// If error_if_not_found is set to true, this function does not return Ok(None) in any case. /// -pub fn resolve<'doc>(toml: &'doc mut Value, tokens: &Token, error_if_not_found: bool) -> Result> { +pub fn resolve<'doc>( + toml: &'doc mut Value, + tokens: &Token, + error_if_not_found: bool, +) -> Result> { match toml { - &mut Value::Table(ref mut t) => { - match tokens { - &Token::Identifier { ref ident, .. } => { - match t.get_mut(ident) { - None => if error_if_not_found { - return Err(Error::IdentifierNotFoundInDocument(ident.to_owned())) - } else { - Ok(None) - }, - Some(sub_document) => match tokens.next() { - Some(next) => resolve(sub_document, next, error_if_not_found), - None => Ok(Some(sub_document)), - }, + &mut Value::Table(ref mut t) => match tokens { + &Token::Identifier { ref ident, .. } => match t.get_mut(ident) { + None => { + if error_if_not_found { + return Err(Error::IdentifierNotFoundInDocument(ident.to_owned())); + } else { + Ok(None) } + } + Some(sub_document) => match tokens.next() { + Some(next) => resolve(sub_document, next, error_if_not_found), + None => Ok(Some(sub_document)), }, + }, - &Token::Index { idx, .. } => Err(Error::NoIndexInTable(idx)), - } + &Token::Index { idx, .. } => Err(Error::NoIndexInTable(idx)), }, - &mut Value::Array(ref mut ary) => { - match tokens { - &Token::Index { idx, .. } => { - match tokens.next() { - Some(next) => resolve(ary.get_mut(idx).unwrap(), next, error_if_not_found), - None => Ok(Some(ary.index_mut(idx))), - } - }, - &Token::Identifier { ref ident, .. } => { - Err(Error::NoIdentifierInArray(ident.clone())) - }, - } + &mut Value::Array(ref mut ary) => match tokens { + &Token::Index { idx, .. } => match tokens.next() { + Some(next) => resolve(ary.get_mut(idx).unwrap(), next, error_if_not_found), + None => Ok(Some(ary.index_mut(idx))), + }, + &Token::Identifier { ref ident, .. } => Err(Error::NoIdentifierInArray(ident.clone())), }, _ => match tokens { &Token::Identifier { ref ident, .. } => Err(Error::QueryingValueAsTable(ident.clone())), - &Token::Index { idx, .. } => Err(Error::QueryingValueAsArray(idx)), - } + &Token::Index { idx, .. } => Err(Error::QueryingValueAsArray(idx)), + }, } } #[cfg(test)] mod test { + use super::resolve; + use crate::error::*; + use crate::tokenizer::*; use toml::from_str as toml_from_str; use toml::Value; - use crate::tokenizer::*; - use crate::error::*; - use super::resolve; macro_rules! do_resolve { ( $toml:ident => $query:expr ) => { - resolve(&mut $toml, &tokenize_with_seperator(&String::from($query), '.').unwrap(), true) - } + resolve( + &mut $toml, + &tokenize_with_seperator(&String::from($query), '.').unwrap(), + true, + ) + }; } #[test] @@ -151,7 +150,7 @@ mod test { &mut Value::Array(ref ary) => { assert_eq!(ary[0], Value::Boolean(true)); assert_eq!(ary[1], Value::Boolean(false)); - }, + } _ => panic!("What just happened?"), } } @@ -172,7 +171,7 @@ mod test { &mut Value::Array(ref ary) => { assert_eq!(ary[0], Value::Integer(1)); assert_eq!(ary[1], Value::Integer(1337)); - }, + } _ => panic!("What just happened?"), } } @@ -195,7 +194,7 @@ mod test { assert_eq!(ary[0].as_float(), Some(1.0)); assert!(is_match!(ary[1], Value::Float(_))); assert_eq!(ary[1].as_float(), Some(133.25)); - }, + } _ => panic!("What just happened?"), } } @@ -230,10 +229,13 @@ mod test { #[test] fn test_resolve_table_element_query() { - let mut toml = toml_from_str(r#" + let mut toml = toml_from_str( + r#" [table] value = 42 - "#).unwrap(); + "#, + ) + .unwrap(); let result = do_resolve!(toml => "table.value"); assert!(result.is_ok()); @@ -247,14 +249,17 @@ mod test { #[test] fn test_resolve_table_with_many_elements_element_query() { - let mut toml = toml_from_str(r#" + let mut toml = toml_from_str( + r#" [table] value1 = 42 value2 = 43 value3 = 44 value4 = 45 value5 = 46 - "#).unwrap(); + "#, + ) + .unwrap(); let result = do_resolve!(toml => "table.value1"); assert!(result.is_ok()); @@ -268,10 +273,13 @@ mod test