Skip to main content

graphrecords_query/error/
numeric.rs

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