Skip to main content

graphrecords_query/error/
comparison.rs

1use crate::{Diagnostic, OwnedIndex};
2use std::{
3    error::Error,
4    fmt::{self, Debug, Display, Formatter},
5};
6
7#[derive(Debug)]
8pub struct IncomparableValues<T> {
9    first: T,
10    second: T,
11}
12
13impl<T> IncomparableValues<T> {
14    #[must_use]
15    pub const fn new(first: T, second: T) -> Self {
16        Self { first, second }
17    }
18
19    #[must_use]
20    pub const fn first(&self) -> &T {
21        &self.first
22    }
23
24    #[must_use]
25    pub const fn second(&self) -> &T {
26        &self.second
27    }
28}
29
30impl<T: Display> Display for IncomparableValues<T> {
31    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
32        write!(
33            formatter,
34            "cannot compare values `{}` and `{}`",
35            self.first, self.second
36        )
37    }
38}
39
40impl<T: Debug + Display> Error for IncomparableValues<T> {}
41
42impl<T: Debug + Display + Send + Sync + 'static> Diagnostic for IncomparableValues<T> {
43    fn name() -> &'static str {
44        "IncomparableValues"
45    }
46
47    fn help(&self) -> Option<String> {
48        Some(
49            "narrow the values down first using is_string(), is_int(), is_float(), is_bool(), is_datetime() or is_duration()"
50                .to_string(),
51        )
52    }
53}
54
55#[derive(Debug)]
56pub struct IncomparableValuesAt<V, E: OwnedIndex> {
57    first: V,
58    second: V,
59    first_element: E,
60    second_element: E,
61}
62
63impl<V, E: OwnedIndex> IncomparableValuesAt<V, E> {
64    #[must_use]
65    pub const fn new(first: V, second: V, first_element: E, second_element: E) -> Self {
66        Self {
67            first,
68            second,
69            first_element,
70            second_element,
71        }
72    }
73
74    #[must_use]
75    pub const fn first(&self) -> &V {
76        &self.first
77    }
78
79    #[must_use]
80    pub const fn second(&self) -> &V {
81        &self.second
82    }
83
84    #[must_use]
85    pub const fn first_element(&self) -> &E {
86        &self.first_element
87    }
88
89    #[must_use]
90    pub const fn second_element(&self) -> &E {
91        &self.second_element
92    }
93}
94
95impl<V: Display, E: OwnedIndex> Display for IncomparableValuesAt<V, E> {
96    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
97        write!(
98            formatter,
99            "cannot compare value `{}` at element `{}` with value `{}` at element `{}`",
100            self.first, self.first_element, self.second, self.second_element
101        )
102    }
103}
104
105impl<V: Debug + Display, E: OwnedIndex> Error for IncomparableValuesAt<V, E> {}
106
107impl<V: Debug + Display + Send + Sync + 'static, E: OwnedIndex> Diagnostic
108    for IncomparableValuesAt<V, E>
109{
110    fn name() -> &'static str {
111        "IncomparableValuesAt"
112    }
113
114    fn help(&self) -> Option<String> {
115        Some(
116            "narrow the values down first using is_string(), is_int(), is_float(), is_bool(), is_datetime() or is_duration()"
117                .to_string(),
118        )
119    }
120}