| 1 | // (c) 2016-2017 Productize SPRL <[email protected]> |
| 2 | |
| 3 | use std::string; |
| 4 | use std::io; |
| 5 | use std::num; |
| 6 | use std::error; |
| 7 | use std::fmt; |
| 8 | |
| 9 | /// errors that can happen in this library |
| 10 | #[derive(Debug)] |
| 11 | pub enum SexpError { |
| 12 | /// parse error |
| 13 | Parse(ParseError), |
| 14 | /// other error |
| 15 | Other(String), |
| 16 | /// IO Error |
| 17 | Io(io::Error), |
| 18 | /// Utf8 Error parsing error |
| 19 | FromUtf8(string::FromUtf8Error), |
| 20 | /// floating point parsing error |
| 21 | Float(num::ParseFloatError), |
| 22 | /// integer parsing error |
| 23 | Int(num::ParseIntError), |
| 24 | } |
| 25 | |
| 26 | // TODO: get rid of this again later |
| 27 | impl fmt::Display for SexpError { |
| 28 | fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 29 | match *self { |
| 30 | SexpError::Parse(ref pe) => write!(f, "symbolic expression parse error: {:?}", pe), |
| 31 | SexpError::Other(ref s) => write!(f, "symbolic expression other error: {}", s), |
| 32 | SexpError::Io(ref e) => e.fmt(f), |
| 33 | SexpError::FromUtf8(ref e) => e.fmt(f), |
| 34 | SexpError::Float(ref e) => e.fmt(f), |
| 35 | SexpError::Int(ref e) => e.fmt(f), |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // TODO: get rid of this again later |
| 41 | impl error::Error for SexpError { |
| 42 | fn description(&self) -> &str { |
| 43 | "symbolic expressions error" |
| 44 | } |
| 45 | |
| 46 | fn cause(&self) -> Option<&error::Error> { |
| 47 | match *self { |
| 48 | SexpError::Parse(_) | SexpError::Other(_) => None, |
| 49 | SexpError::Io(ref e) => Some(e), |
| 50 | SexpError::FromUtf8(ref e) => Some(e), |
| 51 | SexpError::Float(ref e) => Some(e), |
| 52 | SexpError::Int(ref e) => Some(e), |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// detailed symbolic-expression parse error information |
| 58 | #[derive(Debug)] |
| 59 | pub struct ParseError { |
| 60 | msg: String, |
| 61 | line: usize, |
| 62 | col: usize, |
| 63 | } |
| 64 | |
| 65 | impl From<io::Error> for SexpError { |
| 66 | fn from(e: io::Error) -> SexpError { |
| 67 | SexpError::Io(e) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl From<String> for SexpError { |
| 72 | fn from(e: String) -> SexpError { |
| 73 | SexpError::Other(e) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl<'a> From<&'a str> for SexpError { |
| 78 | fn from(e: &'a str) -> SexpError { |
| 79 | SexpError::Other(e.into()) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl From<string::FromUtf8Error> for SexpError { |
| 84 | fn from(e: string::FromUtf8Error) -> SexpError { |
| 85 | SexpError::FromUtf8(e) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl From<num::ParseFloatError> for SexpError { |
| 90 | fn from(e: num::ParseFloatError) -> SexpError { |
| 91 | SexpError::Float(e) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | impl From<num::ParseIntError> for SexpError { |
| 96 | fn from(e: num::ParseIntError) -> SexpError { |
| 97 | SexpError::Int(e) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /// utility function that creates a symbolic-expressions Error Result for a parser error |
| 102 | pub fn parse_error<T>(line: usize, col: usize, msg: String) -> Result<T, SexpError> { |
| 103 | let pe = ParseError { |
| 104 | msg: msg, |
| 105 | line: line, |
| 106 | col: col, |
| 107 | }; |
| 108 | Err(SexpError::Parse(pe)) |
| 109 | } |