Skip to main content

graphrecords_query/error/
conversion.rs

1use crate::Diagnostic;
2use graphrecords_core::graphrecord::datatypes::DataType;
3use std::{
4    error::Error,
5    fmt::{self, Debug, Display, Formatter},
6};
7
8#[derive(Debug)]
9pub struct InvalidCast<T> {
10    value: T,
11    target: DataType,
12}
13
14impl<T> InvalidCast<T> {
15    #[must_use]
16    pub const fn new(value: T, target: DataType) -> Self {
17        Self { value, target }
18    }
19
20    #[must_use]
21    pub const fn value(&self) -> &T {
22        &self.value
23    }
24
25    #[must_use]
26    pub const fn target(&self) -> &DataType {
27        &self.target
28    }
29}
30
31impl<T: Display> Display for InvalidCast<T> {
32    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
33        write!(formatter, "cannot cast `{}` to {}", self.value, self.target)
34    }
35}
36
37impl<T: Debug + Display> Error for InvalidCast<T> {}
38
39impl<T: Debug + Display + Send + Sync + 'static> Diagnostic for InvalidCast<T> {
40    fn name() -> &'static str {
41        "InvalidCast"
42    }
43}
44
45#[derive(Debug)]
46pub struct InvalidTransition<T> {
47    value: T,
48    target: &'static str,
49}
50
51impl<T> InvalidTransition<T> {
52    #[must_use]
53    pub const fn new(value: T, target: &'static str) -> Self {
54        Self { value, target }
55    }
56
57    #[must_use]
58    pub const fn value(&self) -> &T {
59        &self.value
60    }
61
62    #[must_use]
63    pub const fn target(&self) -> &'static str {
64        self.target
65    }
66}
67
68impl<T: Display> Display for InvalidTransition<T> {
69    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
70        write!(
71            formatter,
72            "cannot represent `{}` as {}",
73            self.value, self.target
74        )
75    }
76}
77
78impl<T: Debug + Display> Error for InvalidTransition<T> {}
79
80impl<T: Debug + Display + Send + Sync + 'static> Diagnostic for InvalidTransition<T> {
81    fn name() -> &'static str {
82        "InvalidTransition"
83    }
84}