pactum_annotations/
error.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8pub struct FieldIssue {
9 pub field: String,
11 pub code: String,
13 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
39#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
41pub struct HostError {
42 pub code: String,
43 pub reason: String,
44}
45
46impl HostError {
47 pub fn new(code: impl Into<String>, reason: impl Into<String>) -> Self {
48 Self {
49 code: code.into(),
50 reason: reason.into(),
51 }
52 }
53}
54
55impl std::fmt::Display for HostError {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 write!(f, "{}: {}", self.code, self.reason)
58 }
59}
60
61impl std::error::Error for HostError {}
62
63#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
65pub enum AsyncValidationError {
66 Issues(Vec<FieldIssue>),
67 Host(HostError),
68}
69
70impl From<Vec<FieldIssue>> for AsyncValidationError {
71 fn from(issues: Vec<FieldIssue>) -> Self {
72 Self::Issues(issues)
73 }
74}
75
76impl From<HostError> for AsyncValidationError {
77 fn from(err: HostError) -> Self {
78 Self::Host(err)
79 }
80}
81
82impl std::fmt::Display for AsyncValidationError {
83 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84 match self {
85 Self::Issues(issues) => {
86 if let Some(first) = issues.first() {
87 write!(f, "{first}")
88 } else {
89 write!(f, "validation failed")
90 }
91 }
92 Self::Host(err) => write!(f, "{err}"),
93 }
94 }
95}
96
97impl std::error::Error for AsyncValidationError {}
98
99impl crate::traits::Normalize for FieldIssue {
100 fn normalize(&mut self) {}
101}
102
103impl crate::traits::Validate for FieldIssue {
104 fn validate_with(&self, _ctx: &crate::context::ValidationContext) -> Result<(), Vec<FieldIssue>> {
105 Ok(())
106 }
107}
108
109impl<S> crate::traits::ScopedValidate<S> for FieldIssue {
110 fn validate_scoped_with(
111 &self,
112 _ctx: &crate::context::ValidationContext,
113 ) -> Result<(), Vec<FieldIssue>> {
114 Ok(())
115 }
116}
117
118#[cfg_attr(not(target_arch = "wasm32"), ::async_trait::async_trait)]
119#[cfg_attr(target_arch = "wasm32", ::async_trait::async_trait(?Send))]
120impl<R: ?Sized + Sync, S> crate::traits::AsyncValidate<R, S> for FieldIssue {
121 async fn validate_async_with(
122 &self,
123 _ctx: &crate::context::ValidationContext,
124 _resolver: &R,
125 ) -> Result<(), AsyncValidationError> {
126 Ok(())
127 }
128}