Skip to main content

ferroui_base/data/
binding_chain_exception.rs

1use std::fmt;
2
3/// An error that signals that a binding chain is broken: the value could not
4/// be read at some point of the binding path.
5#[derive(Clone, Debug, Default)]
6pub struct BindingChainException {
7    message: Option<String>,
8    expression: Option<String>,
9    expression_error_point: Option<String>,
10}
11
12impl BindingChainException {
13    pub fn new() -> Self {
14        Self::default()
15    }
16
17    pub fn with_message(message: impl Into<String>) -> Self {
18        Self { message: Some(message.into()), expression: None, expression_error_point: None }
19    }
20
21    pub fn with_expression(
22        message: impl Into<String>,
23        expression: impl Into<String>,
24        error_point: impl Into<String>,
25    ) -> Self {
26        Self {
27            message: Some(message.into()),
28            expression: Some(expression.into()),
29            expression_error_point: Some(error_point.into()),
30        }
31    }
32
33    /// The expression that could not be evaluated.
34    pub fn expression(&self) -> Option<&str> {
35        self.expression.as_deref()
36    }
37
38    /// Sets the expression that could not be evaluated (the protected setter of `Expression`).
39    pub fn set_expression(&mut self, value: Option<String>) {
40        self.expression = value;
41    }
42
43    /// The point in the expression at which the error occurred.
44    pub fn expression_error_point(&self) -> Option<&str> {
45        self.expression_error_point.as_deref()
46    }
47
48    /// Sets the point in the expression at which the error occurred (the protected setter of
49    /// `ExpressionErrorPoint`).
50    pub fn set_expression_error_point(&mut self, value: Option<String>) {
51        self.expression_error_point = value;
52    }
53
54    /// The full message, including the expression and error point.
55    pub fn message(&self) -> String {
56        self.to_string()
57    }
58}
59
60impl fmt::Display for BindingChainException {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        match (&self.expression, &self.expression_error_point, &self.message) {
63            (Some(expression), Some(point), Some(message)) => {
64                write!(f, "An error occurred binding to '{expression}' at '{point}': '{message}'")
65            }
66            (Some(expression), Some(point), None) => {
67                write!(f, "An error occurred binding to '{expression}' at '{point}'.")
68            }
69            (_, _, Some(message)) => write!(f, "An error occurred in a binding: '{message}'"),
70            _ => f.write_str("An error occurred in a binding."),
71        }
72    }
73}
74
75impl std::error::Error for BindingChainException {}