1use std::fmt;
2use std::error::{Error};
3
4use base::{FromAscii};
5
6#[derive(Debug, Clone, PartialEq)]
8pub struct ParseBoolError {
9 kind: BoolErrorKind
10}
11
12#[derive(Debug, Clone, PartialEq)]
13enum BoolErrorKind {
14 BoolErrorKind
15}
16
17impl Error for ParseBoolError {
18 fn description(&self) -> &str {
19 "failed to parse bool"
20 }
21}
22
23impl fmt::Display for ParseBoolError {
24 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25 "provided string was not `true` or `false`".fmt(f)
26 }
27}
28
29impl FromAscii for bool {
30 type Err = ParseBoolError;
31
32 #[inline]
33 fn from_ascii(s: &[u8]) -> Result<bool, ParseBoolError> {
34 match s {
35 b"true" => Ok(true),
36 b"false" => Ok(false),
37 _ => Err(ParseBoolError { kind: BoolErrorKind::BoolErrorKind }),
38 }
39 }
40}
41
42#[cfg(test)]
43mod test {
44 use super::super::base::FromAscii;
45
46 #[test]
47 fn test_parse_bool() {
48 assert_eq!(bool::from_ascii(b"true"), Ok(true));
49 assert_eq!(bool::from_ascii(b"false"), Ok(false));
50 assert!(bool::from_ascii(b"").is_err());
51 assert!(bool::from_ascii(b"true ").is_err());
52 assert!(bool::from_ascii(b" false").is_err());
53 }
54}