Skip to main content

ferroui_base/data/core/
expression_parse_exception.rs

1//! The error reported when an expression string cannot be parsed.
2
3use std::fmt;
4
5/// Error returned when the provided binding expression string could not be parsed.
6#[derive(Clone, Debug, PartialEq, Eq)]
7pub struct ExpressionParseException {
8    column: i32,
9    message: String,
10}
11
12impl ExpressionParseException {
13    /// Initializes a new instance of the [`ExpressionParseException`] type.
14    ///
15    /// * `column` - The column position of the error.
16    /// * `message` - The error message.
17    pub fn new(column: i32, message: impl Into<String>) -> Self {
18        Self {
19            column,
20            message: message.into(),
21        }
22    }
23
24    /// Gets the column position at which the error occurred.
25    #[inline]
26    pub fn column(&self) -> i32 {
27        self.column
28    }
29
30    /// Gets the error message.
31    #[inline]
32    pub fn message(&self) -> &str {
33        &self.message
34    }
35}
36
37impl fmt::Display for ExpressionParseException {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        f.write_str(&self.message)
40    }
41}
42
43impl std::error::Error for ExpressionParseException {}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn exposes_column_and_message() {
51        let e = ExpressionParseException::new(3, "Expected ')'.");
52        assert_eq!(e.column(), 3);
53        assert_eq!(e.message(), "Expected ')'.");
54        assert_eq!(e.to_string(), "Expected ')'.");
55        let _: &dyn std::error::Error = &e;
56    }
57}