Implement the libstd error trait for errors

This commit is contained in:
Alex Crichton 2014-11-04 11:56:11 -08:00
parent 902b3c6f8f
commit 78acb5081d
3 changed files with 41 additions and 2 deletions

View file

@ -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};

View file

@ -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};