dzn_rs/
error.rs

1use std::fmt::Display;
2
3use thiserror::Error;
4
5/// Errors the parser can encounter.
6#[derive(Debug, Error)]
7pub enum DznParseError {
8    #[error("failed to read from source: {0}")]
9    Io(#[from] std::io::Error),
10    #[error("failed to decode to UTF-8 string: {0}")]
11    InvalidEncoding(#[from] std::str::Utf8Error),
12    #[error("failed to parse DZN: expected '{expected}' but got '{actual}'")]
13    InvalidSyntax {
14        expected: SyntaxElement,
15        actual: String,
16    },
17}
18
19/// Components of a parameter definition.
20#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
21pub enum SyntaxElement {
22    /// The name of the parameter.
23    Identifier,
24    /// The value or array to bind to the parameter.
25    Value,
26    /// The equality between identifier and value.
27    Equals,
28    /// The semicolon terminating the definition.
29    SemiColon,
30}
31
32impl Display for SyntaxElement {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        match self {
35            SyntaxElement::Identifier => write!(f, "identifier"),
36            SyntaxElement::Value => write!(f, "value"),
37            SyntaxElement::Equals => write!(f, "="),
38            SyntaxElement::SemiColon => write!(f, ";"),
39        }
40    }
41}