Skip to main content

graphrecords_query/error/
index.rs

1use crate::{Diagnostic, IndexDomain};
2use std::{
3    error::Error,
4    fmt::{self, Debug, Display, Formatter},
5};
6
7pub struct DuplicateIndex<I: IndexDomain> {
8    index: I::Owned,
9}
10
11impl<I: IndexDomain> DuplicateIndex<I> {
12    #[must_use]
13    pub const fn new(index: I::Owned) -> Self {
14        Self { index }
15    }
16
17    #[must_use]
18    pub const fn index(&self) -> &I::Owned {
19        &self.index
20    }
21}
22
23impl<I: IndexDomain> Debug for DuplicateIndex<I> {
24    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
25        formatter
26            .debug_struct("DuplicateIndex")
27            .field("index", &self.index)
28            .finish()
29    }
30}
31
32impl<I: IndexDomain> Display for DuplicateIndex<I> {
33    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
34        write!(
35            formatter,
36            "index `{}` occurs more than once in one indexed operand",
37            self.index
38        )
39    }
40}
41
42impl<I: IndexDomain> Error for DuplicateIndex<I> {}
43
44impl<I: IndexDomain> Diagnostic for DuplicateIndex<I> {
45    fn name() -> &'static str {
46        "DuplicateIndex"
47    }
48
49    fn help(&self) -> Option<String> {
50        Some("construct each index at most once in one indexed operand".to_string())
51    }
52}
53
54pub struct DuplicateExpandedChildIndex<C: IndexDomain> {
55    index: C::Owned,
56}
57
58impl<C: IndexDomain> DuplicateExpandedChildIndex<C> {
59    #[must_use]
60    pub const fn new(index: C::Owned) -> Self {
61        Self { index }
62    }
63
64    #[must_use]
65    pub const fn index(&self) -> &C::Owned {
66        &self.index
67    }
68}
69
70impl<C: IndexDomain> Debug for DuplicateExpandedChildIndex<C> {
71    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
72        formatter
73            .debug_struct("DuplicateExpandedChildIndex")
74            .field("index", &self.index)
75            .finish()
76    }
77}
78
79impl<C: IndexDomain> Display for DuplicateExpandedChildIndex<C> {
80    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
81        write!(
82            formatter,
83            "expanded child index `{}` occurs more than once under one parent",
84            self.index
85        )
86    }
87}
88
89impl<C: IndexDomain> Error for DuplicateExpandedChildIndex<C> {}
90
91impl<C: IndexDomain> Diagnostic for DuplicateExpandedChildIndex<C> {
92    fn name() -> &'static str {
93        "DuplicateExpandedChildIndex"
94    }
95
96    fn help(&self) -> Option<String> {
97        Some("emit each child index at most once for one expanded parent".to_string())
98    }
99}
100
101pub struct NoChildIndex<P: IndexDomain> {
102    parent: P::Owned,
103}
104
105impl<P: IndexDomain> NoChildIndex<P> {
106    #[must_use]
107    pub const fn new(parent: P::Owned) -> Self {
108        Self { parent }
109    }
110
111    #[must_use]
112    pub const fn parent_index(&self) -> &P::Owned {
113        &self.parent
114    }
115}
116
117impl<P: IndexDomain> Debug for NoChildIndex<P> {
118    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
119        formatter
120            .debug_struct("NoChildIndex")
121            .field("parent", &self.parent)
122            .finish()
123    }
124}
125
126impl<P: IndexDomain> Display for NoChildIndex<P> {
127    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
128        write!(
129            formatter,
130            "address `source({})` has no child component",
131            self.parent
132        )
133    }
134}
135
136impl<P: IndexDomain> Error for NoChildIndex<P> {}
137
138impl<P: IndexDomain> Diagnostic for NoChildIndex<P> {
139    fn name() -> &'static str {
140        "NoChildIndex"
141    }
142
143    fn help(&self) -> Option<String> {
144        Some(
145            "source(...) addresses mark parents whose expansion failed; handle those elements with on_error before projecting child indices"
146                .to_string(),
147        )
148    }
149}