bible_io_references/
error.rs1use core::fmt;
4
5#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
7#[non_exhaustive]
8pub enum ParseErrorKind {
9 Unknown,
11 EmptyReference,
13 PatternMismatch,
15 UnknownBook,
17 InvalidNumericToken,
19 NonPositiveNumericToken,
21 NumericTokenOutOfRange,
23 EmptyBookToken,
25 AmbiguousBook,
27 UnsupportedLanguage,
29 SameBookRangeNotAscending,
31 CrossBookRangeNotAscending,
33 MissingNumericToken,
35}
36
37impl ParseErrorKind {
38 #[must_use]
40 pub const fn code(self) -> &'static str {
41 match self {
42 Self::Unknown => "unknown",
43 Self::EmptyReference => "empty_reference",
44 Self::PatternMismatch => "pattern_mismatch",
45 Self::UnknownBook => "unknown_book",
46 Self::InvalidNumericToken => "invalid_numeric_token",
47 Self::NonPositiveNumericToken => "non_positive_numeric_token",
48 Self::NumericTokenOutOfRange => "numeric_token_out_of_range",
49 Self::EmptyBookToken => "empty_book_token",
50 Self::AmbiguousBook => "ambiguous_book",
51 Self::UnsupportedLanguage => "unsupported_language",
52 Self::SameBookRangeNotAscending => "same_book_range_not_ascending",
53 Self::CrossBookRangeNotAscending => "cross_book_range_not_ascending",
54 Self::MissingNumericToken => "missing_numeric_token",
55 }
56 }
57}
58
59#[derive(Clone, Debug, Eq, PartialEq)]
61pub struct ParseError {
62 kind: ParseErrorKind,
63 details: String,
64}
65
66impl ParseError {
67 #[must_use]
69 pub fn new(kind: ParseErrorKind, details: impl Into<String>) -> Self {
70 Self {
71 kind,
72 details: details.into(),
73 }
74 }
75
76 #[must_use]
78 pub const fn kind(&self) -> ParseErrorKind {
79 self.kind
80 }
81
82 #[must_use]
84 pub const fn code(&self) -> &'static str {
85 self.kind.code()
86 }
87
88 #[must_use]
90 pub fn details(&self) -> &str {
91 &self.details
92 }
93}
94
95impl fmt::Display for ParseError {
96 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
97 write!(formatter, "{}: {}", self.code(), self.details)
98 }
99}
100
101impl std::error::Error for ParseError {}