syntree/error.rs
1use core::convert::Infallible;
2use core::fmt;
3
4/// Errors raised while building a tree.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum Error<E = Infallible> {
8 /// Error raised by [Builder::close][crate::Builder::close] if there
9 /// currently is no node being built.
10 ///
11 /// # Examples
12 ///
13 /// ```
14 /// use syntree::{Builder, Error};
15 ///
16 /// let mut tree = Builder::new();
17 ///
18 /// tree.open("root")?;
19 /// tree.close()?;
20 ///
21 /// // Syntax::Root and Syntax::Child is left open.
22 /// assert_eq!(tree.close(), Err(Error::CloseError));
23 /// # Ok::<_, Box<dyn core::error::Error>>(())
24 /// ```
25 CloseError,
26 /// Error raised by [Builder::build][crate::Builder::build] if the
27 /// tree isn't correctly balanced.
28 ///
29 /// # Examples
30 ///
31 /// ```
32 /// use syntree::{Builder, Error};
33 ///
34 /// let mut tree = Builder::new();
35 ///
36 /// tree.open("number")?;
37 /// tree.token("lit", 3)?;
38 /// tree.close()?;
39 ///
40 /// tree.open("number")?;
41 ///
42 /// // Syntax::Number is left open.
43 /// assert_eq!(tree.build(), Err(Error::BuildError));
44 /// # Ok::<_, Box<dyn core::error::Error>>(())
45 /// ```
46 BuildError,
47 /// Error raised by [Builder::close_at][crate::Builder::close_at] if
48 /// we're not trying to close at a sibling node.
49 ///
50 /// # Examples
51 ///
52 /// ```
53 /// use syntree::{Builder, Error};
54 ///
55 /// let mut tree = Builder::new();
56 ///
57 /// let c = tree.checkpoint()?;
58 ///
59 /// tree.open("child")?;
60 /// tree.token("token", 3)?;
61 ///
62 /// assert_eq!(tree.close_at(c, "operation"), Err(Error::CloseAtError));
63 /// # Ok::<_, Box<dyn core::error::Error>>(())
64 /// ```
65 CloseAtError,
66 /// Numerical overflow.
67 ///
68 /// This happens when the tree has more nodes than the flavor's
69 /// [`Width`][crate::Flavor::Width] can address, or when a span would exceed
70 /// its [`Index`][crate::Flavor::Index]. A narrow width such as `u8` makes
71 /// this easy to reach.
72 ///
73 /// # Examples
74 ///
75 /// This is an example where we're trying to build a really small tree using
76 /// u8 pointers:
77 ///
78 /// ```
79 /// use syntree::{Builder, Error};
80 ///
81 /// syntree::flavor! {
82 /// struct CustomFlavor {
83 /// type Index = u32;
84 /// type Width = u8;
85 /// }
86 /// }
87 ///
88 /// let mut tree: Builder<_, CustomFlavor> = Builder::new_with();
89 ///
90 /// for d in 0..u8::MAX - 1 {
91 /// tree.token(d, 1)?;
92 /// }
93 ///
94 /// assert_eq!(tree.token(255, 1), Err(Error::Overflow));
95 /// # Ok::<_, Box<dyn core::error::Error>>(())
96 /// ```
97 Overflow,
98 /// A token's span starts before the end of the token before it, or ends
99 /// before it starts. Tokens are stored in source order, so they can't
100 /// overlap, but an explicit span may leave a gap after the previous
101 /// token.
102 ///
103 /// # Examples
104 ///
105 /// ```
106 /// use syntree::{Builder, Error, Span};
107 ///
108 /// let mut tree = Builder::new();
109 ///
110 /// tree.token("a", 3)?;
111 /// assert_eq!(tree.token_with("b", Span::new(2, 4)), Err(Error::TokenOrder));
112 /// assert_eq!(tree.token_with("b", Span::new(5, 4)), Err(Error::TokenOrder));
113 /// tree.token_with("b", Span::new(5, 6))?;
114 /// # Ok::<_, Box<dyn core::error::Error>>(())
115 /// ```
116 TokenOrder,
117 /// The node of the given id is missing.
118 ///
119 /// This happens when the flavor's storage does not keep what is pushed into
120 /// it, such as [`EmptyVec`][crate::EmptyVec].
121 MissingNode(usize),
122 /// Error raised by [Builder::close_at][crate::Builder::close_at] if the
123 /// checkpoint of the given index was not handed out by this builder.
124 ///
125 /// # Examples
126 ///
127 /// ```
128 /// use syntree::{Builder, Error};
129 ///
130 /// let mut a = Builder::new();
131 /// let mut b = Builder::new();
132 ///
133 /// b.token("lit", 1)?;
134 /// let c = b.checkpoint()?;
135 ///
136 /// assert_eq!(a.close_at(c, "operation"), Err(Error::MissingCheckpoint(0)));
137 /// # Ok::<_, Box<dyn core::error::Error>>(())
138 /// ```
139 MissingCheckpoint(usize),
140 /// An error raised by the particular [Flavor] in use.
141 ///
142 /// [Flavor]: crate::Flavor
143 Flavor(E),
144}
145
146impl<E> From<E> for Error<E> {
147 #[inline]
148 fn from(error: E) -> Self {
149 Error::Flavor(error)
150 }
151}
152
153impl<E> core::error::Error for Error<E>
154where
155 E: 'static + core::error::Error,
156{
157 #[inline]
158 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
159 match self {
160 Error::Flavor(error) => Some(error),
161 _ => None,
162 }
163 }
164}
165
166impl<E> fmt::Display for Error<E>
167where
168 E: fmt::Display,
169{
170 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171 match self {
172 Error::CloseError => {
173 write!(f, "no node being built")
174 }
175 Error::BuildError => {
176 write!(f, "tree is currently being built")
177 }
178 Error::CloseAtError => {
179 write!(
180 f,
181 "trying to close a node which is not a sibling of the checkpoint being closed"
182 )
183 }
184 Error::TokenOrder => {
185 write!(
186 f,
187 "token starts before the end of the previous token, or ends before it starts"
188 )
189 }
190 Error::Overflow => {
191 write!(f, "numerical overflow")
192 }
193 Error::MissingNode(p) => {
194 write!(f, "missing node with id `{p}`")
195 }
196 Error::MissingCheckpoint(index) => {
197 write!(f, "missing checkpoint with index `{index}`")
198 }
199 Error::Flavor(error) => error.fmt(f),
200 }
201 }
202}