Skip to main content

pactum_annotations/
error.rs

1use serde::{Deserialize, Serialize};
2
3/// One field (or whole-model) invariant that failed.
4///
5/// The text field is `reason` so UniFFI Kotlin exceptions never collide with
6/// `Throwable.message`.
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8pub struct FieldIssue {
9    /// JSON / MessagePack key, or `"body"` for a whole-object failure.
10    pub field: String,
11    /// Stable machine code (`INVALID_EMAIL`, `STRING_TOO_LONG`, …).
12    pub code: String,
13    /// Human-readable explanation.
14    pub reason: String,
15}
16
17impl FieldIssue {
18    pub fn new(
19        field: impl Into<String>,
20        code: impl Into<String>,
21        reason: impl Into<String>,
22    ) -> Self {
23        Self {
24            field: field.into(),
25            code: code.into(),
26            reason: reason.into(),
27        }
28    }
29}
30
31impl std::fmt::Display for FieldIssue {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        write!(f, "{}: {} ({})", self.field, self.reason, self.code)
34    }
35}
36
37impl std::error::Error for FieldIssue {}
38
39impl crate::traits::Normalize for FieldIssue {
40    fn normalize(&mut self) {}
41}
42
43impl crate::traits::Validate for FieldIssue {
44    fn validate_with(&self, _ctx: &crate::context::ValidationContext) -> Result<(), Vec<FieldIssue>> {
45        Ok(())
46    }
47}