1use std::{error::Error, fmt};
2
3#[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#[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#[derive(Debug, Clone)]
35pub struct InvalidControl {
36 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#[derive(Debug, Clone)]
55pub enum TermGraphemeError {
56 InvalidControl(InvalidControl),
58 DiacriticAtStart(DiacriticAtStart),
60 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#[derive(Debug, Clone)]
111pub enum TermStringError {
112 InvalidControl(InvalidControl),
114 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}