diff options
Diffstat (limited to 'deepcrow/src/error.rs')
| -rw-r--r-- | deepcrow/src/error.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/deepcrow/src/error.rs b/deepcrow/src/error.rs new file mode 100644 index 0000000..8fe21f0 --- /dev/null +++ b/deepcrow/src/error.rs | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | |||
| 2 | use core::error::Error; | ||
| 3 | use std::fmt; | ||
| 4 | |||
| 5 | use crate::position::*; | ||
| 6 | |||
| 7 | #[derive(Debug)] | ||
| 8 | pub struct DeepCrowError { | ||
| 9 | position: Position, | ||
| 10 | what: String, | ||
| 11 | } | ||
| 12 | |||
| 13 | impl DeepCrowError { | ||
| 14 | pub fn parse( position: Position, what: String ) -> Self { | ||
| 15 | Self { | ||
| 16 | position, | ||
| 17 | what, | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | pub fn other(what: String) -> Self { | ||
| 22 | Self { | ||
| 23 | position: Position::none(), | ||
| 24 | what, | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | pub fn broken( position: Position, what: &str ) -> Self { | ||
| 29 | Self { | ||
| 30 | position, | ||
| 31 | what: format!("Broken parser? {}", what), | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | pub fn eos( context: &str ) -> Self { | ||
| 36 | Self { | ||
| 37 | position: Position::none(), | ||
| 38 | what: format!("Premature end of stream while looking for {}", context), | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | pub fn what(&self) -> &str { | ||
| 43 | &self.what | ||
| 44 | } | ||
| 45 | |||
| 46 | pub fn start(&self) -> &Position { | ||
| 47 | &self.position | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | impl fmt::Display for DeepCrowError { | ||
| 52 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| 53 | write!(f, "Error parsing input: {}", "yeah") | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | impl Error for DeepCrowError { } | ||
| 58 | |||
| 59 | pub type Result<T> = std::result::Result<T, DeepCrowError>; | ||
