diff options
author | Alex Crichton <alex@alexcrichton.com> | 2014-11-04 11:56:11 -0800 |
---|---|---|
committer | Alex Crichton <alex@alexcrichton.com> | 2014-11-04 11:56:11 -0800 |
commit | 78acb5081d57bec85fd51ecb596506b14d17d430 (patch) | |
tree | 159fdd2e4e92060b53895218bdca8fb59b70f9fa /src | |
parent | 902b3c6f8f6a5b23f7f6824a12d951527cb11d86 (diff) | |
download | milf-rs-78acb5081d57bec85fd51ecb596506b14d17d430.tar.gz milf-rs-78acb5081d57bec85fd51ecb596506b14d17d430.zip |
Implement the libstd error trait for errors
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/parser.rs | 6 | ||||
-rw-r--r-- | src/serialization.rs | 35 |
3 files changed, 41 insertions, 2 deletions
@@ -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<String> { 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<String> { 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<String> { Some(self.to_string()) } +} + #[cfg(test)] mod tests { use std::collections::{TreeMap, HashSet}; |