devela/text/parse/
error.rs1use crate::{_impl_init, InvalidUtf8, TextCursor, impl_trait};
7
8#[doc = crate::_tags!(text parser error)]
9#[doc = crate::_doc_meta!{location("text/parse")}]
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
12#[non_exhaustive]
13pub enum TextParseErrorKind {
14 UnexpectedEof,
17 UnexpectedByte {
19 expected: u8,
21 found: Option<u8>,
23 },
24 InvalidUtf8(InvalidUtf8),
27 InvalidDigit,
29 InvalidEscape,
31
32 BufferTooSmall,
35 Overflow,
37
38 UnterminatedQuote,
41 UnexpectedAfterQuote,
43}
44
45_impl_init![Self::UnexpectedEof => TextParseErrorKind];
46impl_trait![fmt::Display for TextParseErrorKind |self, f| {
47 use TextParseErrorKind as K;
48 match self {
49 K::UnexpectedEof => f.write_str("unexpected EOF"),
50 K::UnexpectedByte { expected, found } => match found {
51 Some(found) => write!(f, "unexpected byte: found {found:?}, expected {expected:?}"),
52 None => write!(f, "unexpected EOF: expected byte {expected:?}"),
53 },
54 K::InvalidUtf8(err) => write!(f, "invalid UTF-8 ({err})"),
55 K::InvalidDigit => f.write_str("invalid digit"),
56 K::BufferTooSmall => f.write_str("buffer too small"),
57 K::Overflow => f.write_str("overflow"),
58 K::UnterminatedQuote => f.write_str("unterminated quoted string"),
60 K::UnexpectedAfterQuote => f.write_str("unexpected data after closing quote"),
61 K::InvalidEscape => f.write_str("invalid escape"),
62 }
63}];
64
65#[doc = crate::_tags!(text parser error)]
66#[doc = crate::_doc_meta!{location("text/parse")}]
68#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
69pub struct TextParseError {
70 pub at: TextCursor,
72 pub kind: TextParseErrorKind,
74}
75
76_impl_init![Self { at: TextCursor::INIT, kind: TextParseErrorKind::INIT } => TextParseError];
77impl_trait![fmt::Display+Error for TextParseError |self, f|
78 write!(f, "text parse error at: {:?}, {}", self.at.index, self.kind)
79];
80
81impl TextParseError {
82 pub const fn new(at: TextCursor, kind: TextParseErrorKind) -> Self {
84 Self { at, kind }
85 }
86
87 pub const fn unexpected_eof(at: TextCursor) -> Self {
91 Self::new(at, TextParseErrorKind::UnexpectedEof)
92 }
93 pub const fn unexpected_byte(at: TextCursor, expected: u8, found: Option<u8>) -> Self {
95 Self::new(at, TextParseErrorKind::UnexpectedByte { expected, found })
96 }
97
98 pub const fn invalid_utf8(at: TextCursor, err: InvalidUtf8) -> Self {
105 Self::new(at, TextParseErrorKind::InvalidUtf8(err))
106 }
107 pub const fn invalid_digit(at: TextCursor) -> Self {
109 Self::new(at, TextParseErrorKind::InvalidDigit)
110 }
111 pub const fn invalid_escape(at: TextCursor) -> Self {
113 Self::new(at, TextParseErrorKind::InvalidEscape)
114 }
115
116 pub const fn buffer_too_small(at: TextCursor) -> Self {
120 Self::new(at, TextParseErrorKind::BufferTooSmall)
121 }
122 pub const fn overflow(at: TextCursor) -> Self {
124 Self::new(at, TextParseErrorKind::Overflow)
125 }
126
127 pub const fn unterminated_quote(at: TextCursor) -> Self {
131 Self::new(at, TextParseErrorKind::UnterminatedQuote)
132 }
133 pub const fn unexpected_after_quote(at: TextCursor) -> Self {
135 Self::new(at, TextParseErrorKind::UnexpectedAfterQuote)
136 }
137}