Skip to main content

polyspec_crudui_generator/
error.rs

1use polyspec_crudui_validator::compose::ComposeLoadError;
2
3/// A failed form compilation, binding or instance operation.
4#[derive(Debug, Clone)]
5pub struct FormError {
6    /// Stable failure identifier.
7    pub code: String,
8    /// Description of the failed operation.
9    pub message: String,
10    /// Composition trace or form path.
11    pub at: String,
12    /// Original composition trace; empty for instance and binding failures.
13    pub trace: Vec<String>,
14}
15
16impl FormError {
17    pub(crate) fn input(message: impl Into<String>) -> Self {
18        Self {
19            code: "INVALID_FORM_INPUT".into(),
20            message: message.into(),
21            at: String::new(),
22            trace: Vec::new(),
23        }
24    }
25}
26
27impl std::fmt::Display for FormError {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        f.write_str(&self.message)
30    }
31}
32impl std::error::Error for FormError {}
33
34impl From<ComposeLoadError> for FormError {
35    fn from(error: ComposeLoadError) -> Self {
36        Self {
37            code: error.code.as_str().into(),
38            message: error.message,
39            at: error.trace.join("."),
40            trace: error.trace,
41        }
42    }
43}
44
45/// Result of a form operation.
46pub type FormResult<T> = Result<T, FormError>;