Skip to main content

andiskaz/string/
error.rs

1use std::{error::Error, fmt};
2
3/// Error generated when validating a `TermString` or a grapheme
4/// ([`TermGrapheme`](crate::string::TermGrapheme)) and the string starts with a
5/// diacrtic.
6#[derive(Debug, Clone, Default)]
7pub struct DiacriticAtStart;
8
9impl Error for DiacriticAtStart {}
10
11impl fmt::Display for DiacriticAtStart {
12    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
13        write!(fmt, "The given string with a diacritic")
14    }
15}
16
17/// Error generated when validating a grapheme
18/// ([`TermGrapheme`](crate::string::TermGrapheme)) and the string does not
19/// containing exactly one grapheme cluster
20/// ([`TermGrapheme`](crate::string::TermGrapheme)).
21#[derive(Debug, Clone, Default)]
22pub struct NotAGrapheme;
23
24impl Error for NotAGrapheme {}
25
26impl fmt::Display for NotAGrapheme {
27    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
28        write!(fmt, "The given string is not made of only one grapheme cluster",)
29    }
30}
31
32/// Error generated when validating a `TermString` and the string contains a
33/// control byte.
34#[derive(Debug, Clone)]
35pub struct InvalidControl {
36    /// The position in bytes of the invalid character.
37    pub position: usize,
38}
39
40impl Error for InvalidControl {}
41
42impl fmt::Display for InvalidControl {
43    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
44        write!(
45            fmt,
46            "The string contains a control character in position {}",
47            self.position
48        )
49    }
50}
51
52/// Possible errors when creating a
53/// [`TermGrapheme`](crate::string::TermGrapheme).
54#[derive(Debug, Clone)]
55pub enum TermGraphemeError {
56    /// Invalid control character found in the given input string.
57    InvalidControl(InvalidControl),
58    /// The given input string starts with a diacritic.
59    DiacriticAtStart(DiacriticAtStart),
60    /// The given input is not made of only one grapheme
61    /// ([`TermGrapheme`](crate::string::TermGrapheme)).
62    NotAGrapheme(NotAGrapheme),
63}
64
65impl Error for TermGraphemeError {}
66
67impl fmt::Display for TermGraphemeError {
68    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
69        match self {
70            Self::InvalidControl(error) => write!(fmt, "{}", error),
71            Self::DiacriticAtStart(error) => write!(fmt, "{}", error),
72            Self::NotAGrapheme(error) => write!(fmt, "{}", error),
73        }
74    }
75}
76
77impl From<InvalidControl> for TermGraphemeError {
78    fn from(error: InvalidControl) -> Self {
79        TermGraphemeError::InvalidControl(error)
80    }
81}
82
83impl From<DiacriticAtStart> for TermGraphemeError {
84    fn from(error: DiacriticAtStart) -> Self {
85        TermGraphemeError::DiacriticAtStart(error)
86    }
87}
88
89impl From<NotAGrapheme> for TermGraphemeError {
90    fn from(error: NotAGrapheme) -> Self {
91        TermGraphemeError::NotAGrapheme(error)
92    }
93}
94
95impl From<TermStringError> for TermGraphemeError {
96    fn from(error: TermStringError) -> Self {
97        match error {
98            TermStringError::InvalidControl(error) => {
99                TermGraphemeError::InvalidControl(error)
100            },
101            TermStringError::DiacriticAtStart(error) => {
102                TermGraphemeError::DiacriticAtStart(error)
103            },
104        }
105    }
106}
107
108/// Possible errors when creating a
109/// [`TermGrapheme`](crate::string::TermGrapheme).
110#[derive(Debug, Clone)]
111pub enum TermStringError {
112    /// Invalid control character found in the given input string.
113    InvalidControl(InvalidControl),
114    /// The given input string starts with a diacritic.
115    DiacriticAtStart(DiacriticAtStart),
116}
117
118impl Error for TermStringError {}
119
120impl fmt::Display for TermStringError {
121    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
122        match self {
123            Self::InvalidControl(error) => write!(fmt, "{}", error),
124            Self::DiacriticAtStart(error) => write!(fmt, "{}", error),
125        }
126    }
127}
128
129impl From<InvalidControl> for TermStringError {
130    fn from(error: InvalidControl) -> Self {
131        TermStringError::InvalidControl(error)
132    }
133}
134
135impl From<DiacriticAtStart> for TermStringError {
136    fn from(error: DiacriticAtStart) -> Self {
137        TermStringError::DiacriticAtStart(error)
138    }
139}