diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2018-11-09 10:46:27 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2018-11-09 10:46:27 +0100 |
commit | 7af6ebea381c311fb6ba0318913552849fe025af (patch) | |
tree | 2a205b31e09bed88355b5385260739a71be953ea /src | |
parent | 7545c65568747a9711309ff729df2ea4ab8578bb (diff) | |
parent | 9e11f131718aa090278a1aae4a0b65ffd1f023a1 (diff) |
Merge branch 'failure'
Diffstat (limited to 'src')
-rw-r--r-- | src/delete.rs | 88 | ||||
-rw-r--r-- | src/error.rs | 161 | ||||
-rw-r--r-- | src/insert.rs | 12 | ||||
-rw-r--r-- | src/lib.rs | 3 | ||||
-rw-r--r-- | src/read.rs | 12 | ||||
-rw-r--r-- | src/resolver/mut_creating_resolver.rs | 27 | ||||
-rw-r--r-- | src/resolver/mut_resolver.rs | 55 | ||||
-rw-r--r-- | src/resolver/non_mut_resolver.rs | 50 | ||||
-rw-r--r-- | src/set.rs | 36 | ||||
-rw-r--r-- | src/tokenizer.rs | 32 |
10 files changed, 193 insertions, 283 deletions
diff --git a/src/delete.rs b/src/delete.rs index d09da85..11b3b3a 100644 --- a/src/delete.rs +++ b/src/delete.rs @@ -4,7 +4,7 @@ use toml::Value; use tokenizer::Token; use tokenizer::tokenize_with_seperator; -use error::*; +use error::{Error, Result}; pub trait TomlValueDeleteExt { @@ -90,16 +90,13 @@ impl TomlValueDeleteExt for Value { Ok(tab.remove(&ident)) } else { if is_table(tab.get(&ident)) { - let kind = ErrorKind::CannotDeleteNonEmptyTable(Some(ident.clone())); - Err(Error::from(kind)) + Err(Error::CannotDeleteNonEmptyTable(Some(ident.clone()))) } else if is_array(tab.get(&ident)) { - let kind = ErrorKind::CannotDeleteNonEmptyArray(Some(ident.clone())); - Err(Error::from(kind)) + Err(Error::CannotDeleteNonEmptyArray(Some(ident.clone()))) } else { let act = name_of_val(tab.get(&ident)); let tbl = "table"; - let k = ErrorKind::CannotAccessBecauseTypeMismatch(tbl, act); - Err(Error::from(k)) + Err(Error::CannotAccessBecauseTypeMismatch(tbl, act)) } } }, @@ -108,25 +105,19 @@ impl TomlValueDeleteExt for Value { }, &mut Value::Array(ref mut arr) => { match tokens { - Token::Identifier { ident, .. } => { - let ek = ErrorKind::NoIdentifierInArray(ident); - Err(Error::from(ek)) - }, + 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))) { - let kind = ErrorKind::CannotDeleteNonEmptyTable(None); - Err(Error::from(kind)) + Err(Error::CannotDeleteNonEmptyTable(None)) } else if is_array(Some(arr.index(idx))) { - let kind = ErrorKind::CannotDeleteNonEmptyArray(None); - Err(Error::from(kind)) + Err(Error::CannotDeleteNonEmptyArray(None)) } else { let act = name_of_val(Some(arr.index(idx))); let tbl = "table"; - let k = ErrorKind::CannotAccessBecauseTypeMismatch(tbl, act); - Err(Error::from(k)) + Err(Error::CannotAccessBecauseTypeMismatch(tbl, act)) } } }, @@ -134,8 +125,8 @@ impl TomlValueDeleteExt for Value { }, _ => { let kind = match tokens { - Token::Identifier { ident, .. } => ErrorKind::QueryingValueAsTable(ident), - Token::Index { idx , .. } => ErrorKind::QueryingValueAsArray(idx), + Token::Identifier { ident, .. } => Error::QueryingValueAsTable(ident), + Token::Index { idx , .. } => Error::QueryingValueAsArray(idx), }; Err(Error::from(kind)) } @@ -152,50 +143,37 @@ impl TomlValueDeleteExt for Value { Ok(tab.remove(ident)) } else { if is_table(tab.get(ident)) { - let kind = ErrorKind::CannotDeleteNonEmptyTable(Some(ident.clone())); - Err(Error::from(kind)) + Err(Error::CannotDeleteNonEmptyTable(Some(ident.clone()))) } else if is_array(tab.get(ident)) { - let kind = ErrorKind::CannotDeleteNonEmptyArray(Some(ident.clone())); - Err(Error::from(kind)) + Err(Error::CannotDeleteNonEmptyArray(Some(ident.clone()))) } else { let act = name_of_val(tab.get(ident)); let tbl = "table"; - let k = ErrorKind::CannotAccessBecauseTypeMismatch(tbl, act); - Err(Error::from(k)) + Err(Error::CannotAccessBecauseTypeMismatch(tbl, act)) } } }, - Token::Index { idx, .. } => { - let kind = ErrorKind::NoIndexInTable(idx); - Err(Error::from(kind)) - }, + Token::Index { idx, .. } => Err(Error::NoIndexInTable(idx)), } }, &mut Value::Array(ref mut arr) => { match *last_token { - Token::Identifier { ident, .. } => { - let kind = ErrorKind::NoIdentifierInArray(ident); - Err(Error::from(kind)) - }, + Token::Identifier { ident, .. } => Err(Error::NoIdentifierInArray(ident)), Token::Index { idx, .. } => { if idx > arr.len() { - let kind = ErrorKind::ArrayIndexOutOfBounds(idx, arr.len()); - return Err(Error::from(kind)) + 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))) { - let kind = ErrorKind::CannotDeleteNonEmptyTable(None); - Err(Error::from(kind)) + Err(Error::CannotDeleteNonEmptyTable(None)) } else if is_array(Some(&arr.index(idx))) { - let kind = ErrorKind::CannotDeleteNonEmptyArray(None); - Err(Error::from(kind)) + Err(Error::CannotDeleteNonEmptyArray(None)) } else { let act = name_of_val(Some(arr.index(idx))); let tbl = "table"; - let k = ErrorKind::CannotAccessBecauseTypeMismatch(tbl, act); - Err(Error::from(k)) + Err(Error::CannotAccessBecauseTypeMismatch(tbl, act)) } } }, @@ -203,8 +181,8 @@ impl TomlValueDeleteExt for Value { }, _ => { let kind = match *last_token { - Token::Identifier { ident, .. } => ErrorKind::QueryingValueAsTable(ident), - Token::Index { idx, .. } => ErrorKind::QueryingValueAsArray(idx), + Token::Identifier { ident, .. } => Error::QueryingValueAsTable(ident), + Token::Index { idx, .. } => Error::QueryingValueAsArray(idx), }; Err(Error::from(kind)) } @@ -430,7 +408,7 @@ mod test { assert!(res.is_err()); let res = res.unwrap_err(); - assert!(is_match!(res.kind(), &ErrorKind::CannotDeleteNonEmptyTable(_))); + assert!(is_match!(res, Error::CannotDeleteNonEmptyTable(_))); } #[test] @@ -444,7 +422,7 @@ mod test { assert!(res.is_err()); let res = res.unwrap_err(); - assert!(is_match!(res.kind(), &ErrorKind::CannotDeleteNonEmptyArray(_))); + assert!(is_match!(res, Error::CannotDeleteNonEmptyArray(_))); } #[test] @@ -551,7 +529,7 @@ mod test { assert!(res.is_err()); let res = res.unwrap_err(); - assert!(is_match!(res.kind(), &ErrorKind::QueryingValueAsTable(_))); + assert!(is_match!(res, Error::QueryingValueAsTable(_))); } #[test] @@ -568,7 +546,7 @@ mod test { assert!(res.is_err()); let res = res.unwrap_err(); - assert!(is_match!(res.kind(), &ErrorKind::QueryingValueAsArray(_))); + assert!(is_match!(res, Error::QueryingValueAsArray(_))); } #[test] @@ -582,7 +560,7 @@ mod test { assert!(res.is_err()); let res = res.unwrap_err(); - assert!(is_match!(res.kind(), &ErrorKind::NoIndexInTable(0))); + assert!(is_match!(res, Error::NoIndexInTable(0))); } #[test] @@ -596,7 +574,7 @@ mod test { assert!(res.is_err()); let res = res.unwrap_err(); - assert!(is_match!(res.kind(), &ErrorKind::NoIdentifierInArray(_))); + assert!(is_match!(res, Error::NoIdentifierInArray(_))); } #[test] @@ -610,7 +588,7 @@ mod test { assert!(res.is_err()); let res = res.unwrap_err(); - assert!(is_match!(res.kind(), &ErrorKind::ArrayIndexOutOfBounds(22, 3))); + assert!(is_match!(res, Error::ArrayIndexOutOfBounds(22, 3))); } #[test] @@ -624,7 +602,7 @@ mod test { assert!(res.is_err()); let res = res.unwrap_err(); - assert!(is_match!(res.kind(), &ErrorKind::CannotDeleteNonEmptyArray(None))); + assert!(is_match!(res, Error::CannotDeleteNonEmptyArray(None))); } #[test] @@ -638,7 +616,7 @@ mod test { assert!(res.is_err()); let res = res.unwrap_err(); - assert!(is_match!(res.kind(), &ErrorKind::CannotDeleteNonEmptyTable(None))); + assert!(is_match!(res, Error::CannotDeleteNonEmptyTable(None))); } #[test] @@ -655,7 +633,7 @@ mod test { assert!(res.is_err()); let res = res.unwrap_err(); - assert!(is_match!(res.kind(), &ErrorKind::CannotDeleteNonEmptyTable(None))); + assert!(is_match!(res, Error::CannotDeleteNonEmptyTable(None))); } #[test] @@ -669,7 +647,7 @@ mod test { assert!(res.is_err()); let res = res.unwrap_err(); - assert!(is_match!(res.kind(), &ErrorKind::QueryingValueAsTable(_))); + assert!(is_match!(res, Error::QueryingValueAsTable(_))); } #[test] @@ -683,7 +661,7 @@ mod test { assert!(res.is_err()); let res = res.unwrap_err(); - assert!(is_match!(res.kind(), &ErrorKind::QueryingValueAsArray(0))); + assert!(is_match!(res, Error::QueryingValueAsArray(0))); } } diff --git a/src/error.rs b/src/error.rs index f4378db..7f80efe 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,101 +1,68 @@ /// Error types -error_chain! { - types { - Error, ErrorKind, ResultExt, Result; - } - - foreign_links { - TomlSerialize(::toml::ser::Error) #[cfg(feature = "typed")]; - TomlDeserialize(::toml::de::Error) #[cfg(feature = "typed")]; - } - - errors { - - // Errors for tokenizer - - QueryParsingError(query: String) { - description("parsing the query failed") - display("Parsing the query '{}' failed", query) - } - - EmptyQueryError { - description("the query is empty") - display("The query on the TOML is empty") - } - - EmptyIdentifier { - description("Query an empty identifier: ''") - display("The passed query has an empty identifier") - } - - ArrayAccessWithoutIndex { - description("trying to access array without index") - display("The passed query tries to access an array but does not specify the index") - } - - ArrayAccessWithInvalidIndex { - description("trying to pass an invalid index") - display("The passed query tries to access an array but does not specify a valid index") - } - - // Errors for Resolver - - IdentifierNotFoundInDocument(ident: String) { - description("Identifier missing in document") - display("The identfier '{}' is not present in the document", ident) - } - - NoIndexInTable(i: usize) { - description("Cannot deref index from table") - display("Got an index query '[{}]' but have table", i) - } - - NoIdentifierInArray(s: String) { - description("Cannot query identifier in array") - display("Got an identifier query '{}' but have array", s) - } - - QueryingValueAsTable(s: String) { - description("Querying a table where a value is") - display("Got an identifier query '{}' but have value", s) - } - - QueryingValueAsArray(i: usize) { - description("Querying a table where a value is") - display("Got an index query '{}' but have value", i) - } - - CannotDeleteNonEmptyTable(tabname: Option<String>) { - description("Cannot delete Table that is not empty") - display("Cannot delete table '{:?}' which is not empty", tabname) - } - - CannotDeleteNonEmptyArray(arrname: Option<String>) { - description("Cannot delete Array that is not empty") - display("Cannot delete array '{:?}' which is not empty", arrname) - } - - CannotAccessBecauseTypeMismatch(expected: &'static str, actual: &'static str) { - description("Cannot access value because of type mismatch") - display("Cannot access {} because expected {}", actual, expected) - } - - ArrayIndexOutOfBounds(idx: usize, arrlen: usize) { - description("Delete index out of bounds") - display("Cannot delete in array at {}, array has length {}", idx, arrlen) - } - - TypeError(requested: &'static str, got: &'static str) { - description("Type error") - display("Type Error. Requested {}, but got {}", requested, got) - } - - NotAvailable(query: String) { - description("Value missing error") - display("Value at '{}' not there", query) - } - - } +pub type Result<T> = ::std::result::Result<T, Error>; + +#[derive(Debug, Fail)] +pub enum Error { + #[cfg(feature = "typed")] + #[fail(display = "{}", _0)] + TomlSerialize(#[cause] ::toml::ser::Error), + + #[cfg(feature = "typed")] + #[fail(display = "{}", _0)] + TomlDeserialize(#[cause] ::toml::de::Error), + + // Errors for tokenizer + + #[fail(display = "Parsing the query '{}' failed", _0)] + QueryParsingError(String), + + #[fail(display = "The query on the TOML is empty")] + EmptyQueryError, + + #[fail(display = "The passed query has an empty identifier")] + EmptyIdentifier, + + #[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")] + ArrayAccessWithInvalidIndex, + + // Errors for Resolver + + #[fail(display = "The identfier '{}' is not present in the document", _0)] + IdentifierNotFoundInDocument(String), + + #[fail(display = "Got an index query '[{}]' but have table", _0)] + NoIndexInTable(usize), + + #[fail(display = "Got an identifier query '{}' but have array", _0)] + NoIdentifierInArray(String), + + #[fail(display = "Got an identifier query '{}' but have value", _0)] + QueryingValueAsTable(String), + + #[fail(display = "Got an index query '{}' but have value", _0)] + QueryingValueAsArray(usize), + + #[fail(display = "Cannot delete table '{:?}' which is not empty", _0)] + CannotDeleteNonEmptyTable(Option<String>), + + #[fail(display = "Cannot delete array '{:?}' which is not empty", _0)] + CannotDeleteNonEmptyArray(Option<String>), + + #[fail(display = "Cannot access {} because expected {}", _0, _1)] + CannotAccessBecauseTypeMismatch(&'static str, &'static str), + + #[fail(display = "Cannot delete in array at {}, array has length {}", _0, _1)] + 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 9641b45..5fe3f76 100644 --- a/src/insert.rs +++ b/src/insert.rs @@ -6,7 +6,7 @@ use toml::Value; use tokenizer::Token; use tokenizer::tokenize_with_seperator; -use error::*; +use error::{Error, Result}; pub trait TomlValueInsertExt { @@ -90,7 +90,7 @@ pub trait TomlValueInsertExt { /// A convenience method for inserting any arbitrary serializable value. #[cfg(feature = "typed")] fn insert_serialized<S: Serialize>(&mut self, query: &str, value: S) -> Result<Option<Value>> { - let value = Value::try_from(value)?; + let value = Value::try_from(value).map_err(Error::TomlSerialize)?; self.insert(query, value) } @@ -114,7 +114,7 @@ impl TomlValueInsertExt for Value { &mut Value::Table(ref mut t) => { Ok(t.insert(ident, value)) }, - _ => Err(Error::from(ErrorKind::NoIdentifierInArray(ident.clone()))) + _ => Err(Error::NoIdentifierInArray(ident.clone())) } }, @@ -129,7 +129,7 @@ impl TomlValueInsertExt for Value { Ok(None) } }, - _ => Err(Error::from(ErrorKind::NoIndexInTable(idx))) + _ => Err(Error::NoIndexInTable(idx)) } }, } @@ -321,7 +321,7 @@ mod test { assert!(res.is_err()); let err = res.unwrap_err(); - assert!(is_match!(err.kind(), &ErrorKind::NoIdentifierInArray(_))); + assert!(is_match!(err, Error::NoIdentifierInArray(_))); } #[test] @@ -335,7 +335,7 @@ mod test { assert!(res.is_err()); let err = res.unwrap_err(); - assert!(is_match!(err.kind(), &ErrorKind::NoIndexInTable(_))); + assert!(is_match!(err, Error::NoIndexInTable(_))); } #[test] @@ -8,9 +8,10 @@ // external crates -#[macro_use] extern crate error_chain; #[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; diff --git a/src/read.rs b/src/read.rs index e7fd635..7d2392d 100644 --- a/src/read.rs +++ b/src/read.rs @@ -5,7 +5,7 @@ use serde::Deserialize; use toml::Value; use tokenizer::tokenize_with_seperator; -use error::*; +use error::{Error, Result}; pub trait TomlValueReadExt<'doc> { @@ -33,9 +33,9 @@ pub trait TomlValueReadExt<'doc> { match raw { Some(value) => { - let deserialized = value.clone().try_into()?; + let deserialized = value.clone().try_into().map_err(Error::TomlDeserialize)?; Ok(Some(deserialized)) - } + } None => Ok(None) } } @@ -69,7 +69,7 @@ macro_rules! make_type_getter { fn $fnname(&'doc self, query: &str) -> Result<Option<$rettype>> { self.read_with_seperator(query, '.').and_then(|o| match o { $matcher => Ok(Some($implementation)), - Some(o) => Err(ErrorKind::TypeError($typename, ::util::name_of_val(&o)).into()), + Some(o) => Err(Error::TypeError($typename, ::util::name_of_val(&o)).into()), None => Ok(None), }) } @@ -164,7 +164,7 @@ mod test { assert!(val.is_err()); let err = val.unwrap_err(); - assert!(is_match!(err.kind(), &ErrorKind::NoIndexInTable(_))); + assert!(is_match!(err, Error::NoIndexInTable(_))); } /// @@ -246,7 +246,7 @@ mod test { assert!(val.is_err()); let err = val.unwrap_err(); - assert!(is_match!(err.kind(), &ErrorKind::NoIndexInTable(_))); + assert!(is_match!(err, Error::NoIndexInTable(_))); } } diff --git a/src/resolver/mut_creating_resolver.rs b/src/resolver/mut_creating_resolver.rs index 9b0f8d8..1f9c6a5 100644 --- a/src/resolver/mut_creating_resolver.rs +++ b/src/resolver/mut_creating_resolver.rs @@ -4,7 +4,7 @@ use std::collections::BTreeMap; use toml::Value; use tokenizer::Token; -use error::*; +use error::{Error, Result}; pub fn resolve<'doc>(toml: &'doc mut Value, tokens: &Token) -> Result<&'doc mut Value> { @@ -41,19 +41,13 @@ pub fn resolve<'doc>(toml: &'doc mut Value, tokens: &Token) -> Result<&'doc mut } } }, - &mut Value::Array(_) => { - let kind = ErrorKind::NoIdentifierInArray(ident.clone()); - Err(Error::from_kind(kind)) - } + &mut Value::Array(_) => Err(Error::NoIdentifierInArray(ident.clone())), _ => unimplemented!() } } Token::Index { idx , .. } => { match toml { - &mut Value::Table(_) => { - let kind = ErrorKind::NoIndexInTable(idx); - Err(Error::from_kind(kind)) - }, + &mut Value::Table(_) => Err(Error::NoIndexInTable(idx)), &mut Value::Array(ref mut ary) => { if ary.len() > idx { match tokens.next() { @@ -140,7 +134,8 @@ mod test { assert!(result.is_ok()); let result = result.unwrap(); - assert!(is_match!(result, &mut Value::Float(1.0))); + assert!(is_match!(result, &mut Value::Float(_))); + assert_eq!(result.as_float(), Some(1.0)); } #[test] @@ -205,8 +200,10 @@ mod test { assert!(is_match!(result, &mut Value::Array(_))); match result { &mut Value::Array(ref ary) => { - assert_eq!(ary[0], Value::Float(1.0)); - assert_eq!(ary[1], Value::Float(133.25)); + assert!(is_match!(ary[0], Value::Float(_))); + 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?"), } @@ -280,8 +277,10 @@ mod test { assert!(is_match!(result, &mut Value::Array(_))); match result { &mut Value::Array(ref ary) => { - assert_eq!(ary[0], Value::Float(42.0)); - assert_eq!(ary[1], Value::Float(50.0)); + assert!(is_match!(ary[0], Value::Float(_))); + 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?"), } diff --git a/src/resolver/mut_resolver.rs b/src/resolver/mut_resolver.rs index 09fcfa4..1e5eeac 100644 --- a/src/resolver/mut_resolver.rs +++ b/src/resolver/mut_resolver.rs @@ -4,7 +4,7 @@ use std::ops::IndexMut; use toml::Value; use tokenizer::Token; -use error::*; +use error::{Error, Result}; /// Resolves the path in the passed document recursively /// @@ -19,8 +19,7 @@ pub fn resolve<'doc>(toml: &'doc mut Value, tokens: &Token, error_if_not_found: &Token::Identifier { ref ident, .. } => { match t.get_mut(ident) { None => if error_if_not_found { - let err = ErrorKind::IdentifierNotFoundInDocument(ident.to_owned()); - return Err(Error::from(err)) + return Err(Error::IdentifierNotFoundInDocument(ident.to_owned())) } else { Ok(None) }, @@ -31,10 +30,7 @@ pub fn resolve<'doc>(toml: &'doc mut Value, tokens: &Token, error_if_not_found: } }, - &Token::Index { idx, .. } => { - let kind = ErrorKind::NoIndexInTable(idx); - Err(Error::from(kind)) - }, + &Token::Index { idx, .. } => Err(Error::NoIndexInTable(idx)), } }, @@ -47,20 +43,14 @@ pub fn resolve<'doc>(toml: &'doc mut Value, tokens: &Token, error_if_not_found: } }, &Token::Identifier { ref ident, .. } => { - let kind = ErrorKind::NoIdentifierInArray(ident.clone()); - Err(Error::from(kind)) + Err(Error::NoIdentifierInArray(ident.clone())) }, } }, _ => match tokens { - &Token::Identifier { ref ident, .. } => { - Err(Error::from(ErrorKind::QueryingValueAsTable(ident.clone()))) - }, - - &Token::Index { idx, .. } => { - Err(Error::from(ErrorKind::QueryingValueAsArray(idx))) - }, + &Token::Identifier { ref ident, .. } => Err(Error::QueryingValueAsTable(ident.clone())), + &Token::Index { idx, .. } => Err(Error::QueryingValueAsArray(idx)), } } } @@ -87,8 +77,7 @@ mod test { assert!(result.is_err()); let result = result.unwrap_err(); - let errkind = result.kind(); - assert!(is_match!(errkind, &ErrorKind::IdentifierNotFoundInDocument { .. })); + assert!(is_match!(result, Error::IdentifierNotFoundInDocument { .. })); } #[test] @@ -124,7 +113,8 @@ mod test { assert!(result.is_some()); let result = result.unwrap(); - assert!(is_match!(result, &mut Value::Float(1.0))); + assert!(is_match!(result, &mut Value::Float(_))); + assert_eq!(result.as_float(), Some(1.0)) } #[test] @@ -201,8 +191,10 @@ mod test { assert!(is_match!(result, &mut Value::Array(_))); match result { &mut Value::Array(ref ary) => { - assert_eq!(ary[0], Value::Float(1.0)); - assert_eq!(ary[1], Value::Float(133.25)); + assert!(is_match!(ary[0], Value::Float(_))); + 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?"), } @@ -291,8 +283,10 @@ mod test { assert!(is_match!(result, &mut Value::Array(_))); match result { &mut Value::Array(ref ary) => { - assert_eq!(ary[0], Value::Float(42.0)); - assert_eq!(ary[1], Value::Float(50.0)); + assert!(is_match!(ary[0], Value::Float(_))); + 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?"), } @@ -460,8 +454,7 @@ mod test { assert!(result.is_err()); let result = result.unwrap_err(); - let errkind = result.kind(); - assert!(is_match!(errkind, &ErrorKind::IdentifierNotFoundInDocument { .. })); + assert!(is_match!(result, Error::IdentifierNotFoundInDocument { .. })); } #[test] @@ -474,8 +467,7 @@ mod test { assert!(result.is_err()); let result = result.unwrap_err(); - let errkind = result.kind(); - assert!(is_match!(errkind, &ErrorKind::NoIndexInTable { .. })); + assert!(is_match!(result, Error::NoIndexInTable { .. })); } #[test] @@ -489,8 +481,7 @@ mod test { assert!(result.is_err()); let result = result.unwrap_err(); - let errkind = result.kind(); - assert!(is_match!(errkind, &ErrorKind::NoIdentifierInArray { .. })); + assert!(is_match!(result, Error::NoIdentifierInArray { .. })); } #[test] @@ -504,8 +495,7 @@ mod test { assert!(result.is_err()); let result = result.unwrap_err(); - let errkind = result.kind(); - assert!(is_match!(errkind, &ErrorKind::QueryingValueAsTable { .. })); + assert!(is_match!(result, Error::QueryingValueAsTable { .. })); } #[test] @@ -519,8 +509,7 @@ mod test { assert!(result.is_err()); let result = result.unwrap_err(); - let errkind = result.kind(); - assert!(is_match!(errkind, &ErrorKind::QueryingValueAsArray { .. })); + assert!(is_match!(result, Error::QueryingValueAsArray { .. })); } } diff --git a/src/resolver/non_mut_resolver.rs b/src/resolver/non_mut_resolver.rs index a3d2b97..c60f4c2 100644 --- a/src/resolver/non_mut_resolver.rs +++ b/src/resolver/non_mut_resolver.rs @@ -4,7 +4,7 @@ use std::ops::Index; use toml::Value; use tokenizer::Token; -use error::*; +use error::{Error, Result}; /// Resolves the path in the passed document recursively /// @@ -19,8 +19,7 @@ pub fn resolve<'doc>(toml: &'doc Value, tokens: &Token, error_if_not_found: bool &Token::Identifier { ref ident, .. } => { match t.get(ident) { None => if error_if_not_found { - let err = ErrorKind::IdentifierNotFoundInDocument(ident.to_owned()); - return Err(Error::from(err)) + return Err(Error::IdentifierNotFoundInDocument(ident.to_owned())) } else { Ok(None) }, @@ -31,10 +30,7 @@ pub fn resolve<'doc>(toml: &'doc Value, tokens: &Token, error_if_not_found: bool } }, - &Token::Index { idx, .. } => { - let kind = ErrorKind::NoIndexInTable(idx); - Err(Error::from(kind)) - }, + &Token::Index { idx, .. } => Err(Error::NoIndexInTable(idx)), } }, @@ -47,19 +43,18 @@ pub fn resolve<'doc>(toml: &'doc Value, tokens: &Token, error_if_not_found: bool } }, &Token::Identifier { ref ident, .. } => { - let kind = ErrorKind::NoIdentifierInArray(ident.clone()); - Err(Error::from(kind)) + Err(Error::NoIdentifierInArray(ident.clone())) }, } }, _ => match tokens { &Token::Identifier { ref ident, .. } => { - Err(Error::from(ErrorKind::QueryingValueAsTable(ident.clone()))) + Err(Error::QueryingValueAsTable(ident.clone())) }, &Token::Index { idx, .. } => { - Err(Error::from(ErrorKind::QueryingValueAsArray(idx))) + Err(Error::QueryingValueAsArray(idx)) }, } } @@ -87,8 +82,7 @@ mod test { assert!(result.is_err()); let result = result.unwrap_err(); - let errkind = result.kind(); - assert!(is_match!(errkind, &ErrorKind::IdentifierNotFoundInDocument { .. })); + assert!(is_match!(result, Error::IdentifierNotFoundInDocument { .. })); } #[test] @@ -130,7 +124,8 @@ mod test { assert!(result.is_some()); let result = result.unwrap(); - assert!(is_match!(result, &Value::Float(1.0))); + assert!(is_match!(result, &Value::Float(_))); + assert_eq!(result.as_float(), Some(1.0)) } #[test] @@ -207,8 +202,10 @@ mod test { assert!(is_match!(result, &Value::Array(_))); match result { &Value::Array(ref ary) => { - assert_eq!(ary[0], Value::Float(1.0)); - assert_eq!(ary[1], Value::Float(133.25)); + assert!(is_match!(ary[0], Value::Float(_))); + 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?"), } @@ -297,8 +294,10 @@ mod test { assert!(is_match!(result, &Value::Array(_))); match result { &Value::Array(ref ary) => { - assert_eq!(ary[0], Value::Float(42.0)); - assert_eq!(ary[1], Value::Float(50.0)); + assert!(is_match!(ary[0], Value::Float(_))); + assert_eq!(ary[0].as_float(), Some(42.0)); + assert!(is_ma |