diff --git a/src/lib.rs b/src/lib.rs index a9b42b7..f33568c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,7 +47,7 @@ use std::collections::TreeMap; use std::from_str::FromStr; use std::string; -pub use parser::{Parser,ParserError}; +pub use parser::{Parser, ParserError}; pub use serialization::{Encoder, encode, encode_str}; pub use serialization::{Decoder, decode, decode_str}; pub use serialization::{Error, NeedsKey, NoValue}; diff --git a/src/parser.rs b/src/parser.rs index ce3571e..e1d784d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,5 +1,6 @@ use std::char; use std::collections::{TreeMap, HashSet}; +use std::error::Error; use std::num::FromStrRadix; use std::str; @@ -760,6 +761,11 @@ impl<'a> Parser<'a> { } } +impl Error for ParserError { + fn description(&self) -> &str { "TOML parse error" } + fn detail(&self) -> Option { Some(self.desc.clone()) } +} + #[cfg(test)] mod tests { use {Table, Parser}; diff --git a/src/serialization.rs b/src/serialization.rs index 0ee527d..3c5eb87 100644 --- a/src/serialization.rs +++ b/src/serialization.rs @@ -1,6 +1,7 @@ use std::collections::TreeMap; use std::mem; use std::fmt; +use std::error::Error as StdError; use serialize; use {Value, Table, Array, Integer, Float, Boolean, Parser, TomlTable}; @@ -56,7 +57,6 @@ pub struct Decoder { /// Enumeration of errors which can occur while encoding a rust value into a /// TOML value. -#[deriving(Show)] pub enum Error { /// Indication that a key was needed when a value was emitted, but no key /// was previously emitted. @@ -762,6 +762,39 @@ impl fmt::Show for DecodeError { } } +impl StdError for DecodeError { + fn description(&self) -> &str { + match self.kind { + ApplicationError(ref s) => s.as_slice(), + ExpectedField(..) => "expected a field", + ExpectedType(..) => "expected a type", + ExpectedMapKey(..) => "expected a map key", + ExpectedMapElement(..) => "expected a map element", + NoEnumVariants => "no enum variants to decode to", + NilTooLong => "nonzero length string representing nil", + } + } + fn detail(&self) -> Option { Some(self.to_string()) } +} + +impl fmt::Show for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + NeedsKey => write!(f, "need a key to encode"), + NoValue => write!(f, "not value to emit for a previous key"), + InvalidMapKeyLocation => write!(f, "a map cannot be emitted at \ + this location"), + InvalidMapKeyType => write!(f, "only strings can be used as \ + key types"), + } + } +} + +impl StdError for Error { + fn description(&self) -> &str { "TOML encoding error" } + fn detail(&self) -> Option { Some(self.to_string()) } +} + #[cfg(test)] mod tests { use std::collections::{TreeMap, HashSet};