Skip to main content

jql_parser/
errors.rs

1use thiserror::Error;
2
3fn display_content(content: &str) -> String {
4    if content.is_empty() {
5        String::new()
6    } else {
7        format!(" after {content}")
8    }
9}
10
11/// Error type returned by the parser.
12///
13/// Marked `#[non_exhaustive]`: new variants are added as the parser grows, and
14/// downstream code must not break when one appears.
15#[derive(Debug, Error, PartialEq)]
16#[non_exhaustive]
17pub enum JqlParserError {
18    /// Empty input error.
19    #[error("Empty input")]
20    EmptyInputError,
21
22    /// Parsing error.
23    #[error("Unable to parse input {unparsed}{}", display_content(tokens))]
24    ParsingError {
25        /// Tokens found while parsing.
26        tokens: String,
27        /// Unparsed content.
28        unparsed: String,
29    },
30
31    /// Truncate error.
32    #[error("Truncate operator found as non last element or multiple times in {0}")]
33    TruncateError(String),
34
35    /// Unknown error.
36    #[error("Unknown error")]
37    UnknownError,
38}
39
40#[cfg(test)]
41mod tests {
42
43    use super::display_content;
44
45    #[test]
46    fn check_display_content() {
47        assert_eq!(display_content("some"), " after some");
48        assert_eq!(display_content(""), "");
49    }
50}