Skip to main content

graphrecords_query/error/
structure.rs

1use crate::{Diagnostic, OwnedIndex};
2use graphrecords_core::graphrecord::GraphRecordAttribute;
3use std::{
4    error::Error,
5    fmt::{self, Display, Formatter},
6};
7
8#[derive(Debug)]
9pub struct MissingAttribute {
10    attribute: GraphRecordAttribute,
11}
12
13impl MissingAttribute {
14    #[must_use]
15    pub const fn new(attribute: GraphRecordAttribute) -> Self {
16        Self { attribute }
17    }
18
19    #[must_use]
20    pub const fn attribute(&self) -> &GraphRecordAttribute {
21        &self.attribute
22    }
23}
24
25impl Display for MissingAttribute {
26    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
27        write!(formatter, "no attribute `{}`", self.attribute)
28    }
29}
30
31impl Error for MissingAttribute {}
32
33impl Diagnostic for MissingAttribute {
34    fn name() -> &'static str {
35        "MissingAttribute"
36    }
37
38    fn help(&self) -> Option<String> {
39        Some(
40            "filter the elements using `has_attribute(...)` first or handle missing attributes with `on_error(...)`"
41                .to_string(),
42        )
43    }
44}
45
46#[derive(Debug)]
47pub struct MissingTraversedAttribute<T: OwnedIndex> {
48    attribute: GraphRecordAttribute,
49    entity: T,
50}
51
52impl<T: OwnedIndex> MissingTraversedAttribute<T> {
53    #[must_use]
54    pub const fn new(attribute: GraphRecordAttribute, entity: T) -> Self {
55        Self { attribute, entity }
56    }
57
58    #[must_use]
59    pub const fn attribute(&self) -> &GraphRecordAttribute {
60        &self.attribute
61    }
62
63    #[must_use]
64    pub const fn entity(&self) -> &T {
65        &self.entity
66    }
67}
68
69impl<T: OwnedIndex> Display for MissingTraversedAttribute<T> {
70    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
71        write!(
72            formatter,
73            "no attribute `{}` on the traversed element `{}`",
74            self.attribute, self.entity
75        )
76    }
77}
78
79impl<T: OwnedIndex> Error for MissingTraversedAttribute<T> {}
80
81impl<T: OwnedIndex> Diagnostic for MissingTraversedAttribute<T> {
82    fn name() -> &'static str {
83        "MissingTraversedAttribute"
84    }
85
86    fn help(&self) -> Option<String> {
87        Some(
88            "filter the elements using `has_attribute(...)` first or handle missing attributes with `on_error(...)`"
89                .to_string(),
90        )
91    }
92}