Skip to main content

graphrecords_query/error/
string.rs

1use crate::Diagnostic;
2use regex::Error as RegexError;
3use std::{
4    error::Error,
5    fmt::{self, Debug, Display, Formatter},
6};
7
8#[derive(Debug)]
9pub struct EmptySplitDelimiter;
10
11impl Display for EmptySplitDelimiter {
12    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
13        write!(formatter, "split delimiter cannot be empty")
14    }
15}
16
17impl Error for EmptySplitDelimiter {}
18
19impl Diagnostic for EmptySplitDelimiter {
20    fn name() -> &'static str {
21        "EmptySplitDelimiter"
22    }
23}
24
25#[derive(Debug)]
26pub struct InvalidPaddingCharacter {
27    value: String,
28}
29
30impl InvalidPaddingCharacter {
31    #[must_use]
32    pub const fn new(value: String) -> Self {
33        Self { value }
34    }
35
36    #[must_use]
37    pub const fn value(&self) -> &str {
38        self.value.as_str()
39    }
40}
41
42impl Display for InvalidPaddingCharacter {
43    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
44        write!(
45            formatter,
46            "padding requires exactly one character, got `{}`",
47            self.value
48        )
49    }
50}
51
52impl Error for InvalidPaddingCharacter {}
53
54impl Diagnostic for InvalidPaddingCharacter {
55    fn name() -> &'static str {
56        "InvalidPaddingCharacter"
57    }
58}
59
60#[derive(Debug)]
61pub struct InvalidRegexPattern {
62    pattern: String,
63    error: RegexError,
64}
65
66impl InvalidRegexPattern {
67    #[must_use]
68    pub const fn new(pattern: String, error: RegexError) -> Self {
69        Self { pattern, error }
70    }
71
72    #[must_use]
73    pub const fn pattern(&self) -> &str {
74        self.pattern.as_str()
75    }
76
77    #[must_use]
78    pub const fn error(&self) -> &RegexError {
79        &self.error
80    }
81}
82
83impl Display for InvalidRegexPattern {
84    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
85        write!(
86            formatter,
87            "`{}` is not a valid regular expression: {}",
88            self.pattern, self.error
89        )
90    }
91}
92
93impl Error for InvalidRegexPattern {
94    fn source(&self) -> Option<&(dyn Error + 'static)> {
95        Some(&self.error)
96    }
97}
98
99impl Diagnostic for InvalidRegexPattern {
100    fn name() -> &'static str {
101        "InvalidRegexPattern"
102    }
103}
104
105#[derive(Debug)]
106pub struct InvalidStringSlice {
107    start: usize,
108    end: usize,
109    length: usize,
110}
111
112impl InvalidStringSlice {
113    #[must_use]
114    pub const fn new(start: usize, end: usize, length: usize) -> Self {
115        Self { start, end, length }
116    }
117
118    #[must_use]
119    pub const fn start(&self) -> usize {
120        self.start
121    }
122
123    #[must_use]
124    pub const fn end(&self) -> usize {
125        self.end
126    }
127
128    #[must_use]
129    pub const fn length(&self) -> usize {
130        self.length
131    }
132}
133
134impl Display for InvalidStringSlice {
135    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
136        write!(
137            formatter,
138            "character range {}..{} is invalid for a string of length {}",
139            self.start, self.end, self.length
140        )
141    }
142}
143
144impl Error for InvalidStringSlice {}
145
146impl Diagnostic for InvalidStringSlice {
147    fn name() -> &'static str {
148        "InvalidStringSlice"
149    }
150}
151
152#[derive(Debug)]
153pub struct NonStringValue<T> {
154    value: T,
155}
156
157impl<T> NonStringValue<T> {
158    #[must_use]
159    pub const fn new(value: T) -> Self {
160        Self { value }
161    }
162
163    #[must_use]
164    pub const fn value(&self) -> &T {
165        &self.value
166    }
167}
168
169impl<T: Display> Display for NonStringValue<T> {
170    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
171        write!(formatter, "`{}` is not a string value", self.value)
172    }
173}
174
175impl<T: Debug + Display> Error for NonStringValue<T> {}
176
177impl<T: Debug + Display + Send + Sync + 'static> Diagnostic for NonStringValue<T> {
178    fn name() -> &'static str {
179        "NonStringValue"
180    }
181}
182
183#[derive(Debug)]
184pub struct StringLengthOverflow {
185    length: usize,
186}
187
188impl StringLengthOverflow {
189    #[must_use]
190    pub const fn new(length: usize) -> Self {
191        Self { length }
192    }
193
194    #[must_use]
195    pub const fn length(&self) -> usize {
196        self.length
197    }
198}
199
200impl Display for StringLengthOverflow {
201    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
202        write!(
203            formatter,
204            "string character count `{}` does not fit in an integer value",
205            self.length
206        )
207    }
208}
209
210impl Error for StringLengthOverflow {}
211
212impl Diagnostic for StringLengthOverflow {
213    fn name() -> &'static str {
214        "StringLengthOverflow"
215    }
216}
217
218#[derive(Debug)]
219pub struct StringPaddingOverflow {
220    width: usize,
221}
222
223impl StringPaddingOverflow {
224    #[must_use]
225    pub const fn new(width: usize) -> Self {
226        Self { width }
227    }
228
229    #[must_use]
230    pub const fn width(&self) -> usize {
231        self.width
232    }
233}
234
235impl Display for StringPaddingOverflow {
236    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
237        write!(
238            formatter,
239            "padded string width `{}` exceeds the supported capacity",
240            self.width
241        )
242    }
243}
244
245impl Error for StringPaddingOverflow {}
246
247impl Diagnostic for StringPaddingOverflow {
248    fn name() -> &'static str {
249        "StringPaddingOverflow"
250    }
251}