| 1 | use std::error::Error; |
| 2 | use std::io; |
| 3 | use std::fmt; |
| 4 | use ffi; |
| 5 | use ffi::YamlErrorType; |
| 6 | |
| 7 | #[derive(Debug, PartialEq, Clone, Copy)] |
| 8 | pub struct YamlMark { |
| 9 | pub index: usize, |
| 10 | pub line: usize, |
| 11 | pub column: usize |
| 12 | } |
| 13 | |
| 14 | impl YamlMark { |
| 15 | pub fn conv(mark: &ffi::yaml_mark_t) -> YamlMark { |
| 16 | YamlMark { |
| 17 | index: mark.index as usize, |
| 18 | line: mark.line as usize, |
| 19 | column: mark.column as usize |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | #[derive(Debug, PartialEq)] |
| 25 | pub struct YamlErrorContext { |
| 26 | pub byte_offset: usize, |
| 27 | pub problem_mark: YamlMark, |
| 28 | pub context: Option<String>, |
| 29 | pub context_mark: YamlMark |
| 30 | } |
| 31 | |
| 32 | #[derive(Debug)] |
| 33 | pub struct YamlError { |
| 34 | pub kind: YamlErrorType, |
| 35 | pub problem: Option<String>, |
| 36 | pub io_error: Option<io::Error>, |
| 37 | pub context: Option<YamlErrorContext> |
| 38 | } |
| 39 | |
| 40 | impl PartialEq for YamlError { |
| 41 | fn eq(&self, rhs: &YamlError) -> bool { |
| 42 | self.kind == rhs.kind |
| 43 | && self.problem == rhs.problem |
| 44 | && self.io_error.is_none() |
| 45 | && rhs.io_error.is_none() |
| 46 | && self.context == rhs.context |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl Error for YamlError { |
| 51 | fn description(&self) -> &str { |
| 52 | match self.kind { |
| 53 | YamlErrorType::YAML_NO_ERROR => "No error is produced", |
| 54 | YamlErrorType::YAML_MEMORY_ERROR => "Cannot allocate or reallocate a block of memory", |
| 55 | YamlErrorType::YAML_READER_ERROR => "Cannot read or decode the input stream", |
| 56 | YamlErrorType::YAML_SCANNER_ERROR => "Cannot scan the input stream", |
| 57 | YamlErrorType::YAML_PARSER_ERROR => "Cannot parse the input stream", |
| 58 | YamlErrorType::YAML_COMPOSER_ERROR => "Cannot compose a YAML document", |
| 59 | YamlErrorType::YAML_WRITER_ERROR => "Cannot write to the output stream", |
| 60 | YamlErrorType::YAML_EMITTER_ERROR => "Cannot emit a YAML stream", |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | fn cause(&self) -> Option<&Error> { |
| 65 | match self.io_error { |
| 66 | None => None, |
| 67 | Some(ref e) => Some(e as &Error) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl YamlError { |
| 73 | pub fn new(kind: YamlErrorType, problem: Option<String>) -> YamlError { |
| 74 | YamlError { |
| 75 | kind: kind, |
| 76 | problem: problem, |
| 77 | io_error: None, |
| 78 | context: None |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl fmt::Display for YamlError { |
| 84 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 85 | match self.problem { |
| 86 | None => return Ok(()), |
| 87 | Some(ref s) => s.fmt(f) |
| 88 | } |
| 89 | } |
| 90 | } |