| 1 | #[cfg(not(feature = "std"))] |
| 2 | use alloc::{ |
| 3 | format, |
| 4 | string::{String, ToString}, |
| 5 | }; |
| 6 | #[cfg(not(feature = "std"))] |
| 7 | use core::fmt::Display; |
| 8 | #[cfg(feature = "std")] |
| 9 | use std::fmt::Display; |
| 10 | |
| 11 | use thiserror::Error; |
| 12 | |
| 13 | /// An encoding or decoding error |
| 14 | #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Error)] |
| 15 | pub enum StructureError { |
| 16 | /// Wrong type of token detected. |
| 17 | #[error("Saw the wrong type of token: {state}")] |
| 18 | InvalidState { state: String }, |
| 19 | |
| 20 | /// Keys were not sorted. |
| 21 | #[error("Keys were not sorted")] |
| 22 | UnsortedKeys, |
| 23 | |
| 24 | /// EOF reached to early. |
| 25 | #[error("Reached EOF in the middle of a message")] |
| 26 | UnexpectedEof, |
| 27 | |
| 28 | /// Unexpected characters detected. |
| 29 | #[error("Malformed number of unexpected character: {unexpected}")] |
| 30 | SyntaxError { unexpected: String }, |
| 31 | |
| 32 | /// Exceeded the recursion limit. |
| 33 | #[error("Maximum nesting depth exceeded")] |
| 34 | NestingTooDeep, |
| 35 | } |
| 36 | |
| 37 | impl StructureError { |
| 38 | pub fn unexpected(expected: impl Display, got: char, offset: usize) -> Self { |
| 39 | StructureError::SyntaxError { |
| 40 | unexpected: format!("Expected {expected}, got {got:?} at offset {offset}"), |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | pub fn invalid_state(expected: impl Display) -> Self { |
| 45 | StructureError::InvalidState { |
| 46 | state: expected.to_string(), |
| 47 | } |
| 48 | } |
| 49 | } |