batch_mode_batch_schema/
error_type.rs

1// ---------------- [ File: batch-mode-batch-schema/src/error_type.rs ]
2crate::ix!();
3
4#[derive(Debug,Clone,Eq,Hash)]
5pub enum ErrorType {
6    InsufficientQuota,
7    InvalidRequest,
8    // Add other known error types
9    Unknown(String),
10}
11
12// Changed PartialEq implementation from `PartialEq<str>` to `PartialEq<&str>`
13// so that comparisons like `my_error_type == "some_string"` will compile properly:
14impl PartialEq for ErrorType {
15    fn eq(&self, other: &Self) -> bool {
16        self.as_str() == other.as_str()
17    }
18}
19
20// NEW: Implement PartialEq<&str> so that `ErrorType == "insufficient_quota"` works.
21impl PartialEq<&str> for ErrorType {
22    fn eq(&self, other: &&str) -> bool {
23        self.as_str() == *other
24    }
25}
26
27// If you also want `ErrorType == str` (owned), you can add:
28impl PartialEq<str> for ErrorType {
29    fn eq(&self, other: &str) -> bool {
30        self.as_str() == other
31    }
32}
33
34impl ErrorType {
35    pub fn as_str(&self) -> &str {
36        match self {
37            ErrorType::InvalidRequest => "invalid_request",
38            ErrorType::InsufficientQuota => "insufficient_quota",
39            ErrorType::Unknown(s) => s.as_str(),
40        }
41    }
42}
43
44impl<'de> Deserialize<'de> for ErrorType {
45    fn deserialize<D>(deserializer: D) -> Result<ErrorType, D::Error>
46    where
47        D: serde::Deserializer<'de>,
48    {
49        let s = String::deserialize(deserializer)?;
50        match s.as_str() {
51            "insufficient_quota" => Ok(ErrorType::InsufficientQuota),
52            "invalid_request" => Ok(ErrorType::InvalidRequest),
53            // Handle other known types
54            other => Ok(ErrorType::Unknown(other.to_string())),
55        }
56    }
57}
58
59impl Serialize for ErrorType {
60    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
61    where
62        S: serde::Serializer,
63    {
64        let s = match self {
65            ErrorType::InsufficientQuota => "insufficient_quota",
66            ErrorType::InvalidRequest => "invalid_request",
67            ErrorType::Unknown(other) => other.as_str(),
68        };
69        serializer.serialize_str(s)
70    }
71}
72
73#[cfg(test)]
74mod error_type_tests {
75    use super::*;
76    
77    use std::io::Write;
78    
79    
80
81    #[traced_test]
82    fn should_deserialize_known_error_type_insufficient_quota() {
83        info!("Testing deserialization of 'insufficient_quota' into ErrorType.");
84        let json_str = r#""insufficient_quota""#;
85        let et: ErrorType = serde_json::from_str(json_str).expect("Failed to deserialize known error type.");
86        pretty_assert_eq!(et, ErrorType::InsufficientQuota);
87        debug!("Deserialized to {:?}", et);
88    }
89
90    #[traced_test]
91    fn should_deserialize_known_error_type_invalid_request() {
92        info!("Testing deserialization of 'invalid_request' into ErrorType.");
93        let json_str = r#""invalid_request""#;
94        let et: ErrorType = serde_json::from_str(json_str).expect("Failed to deserialize known error type.");
95        pretty_assert_eq!(et, ErrorType::InvalidRequest);
96        debug!("Deserialized to {:?}", et);
97    }
98
99    #[traced_test]
100    fn should_deserialize_unknown_error_type() {
101        info!("Testing deserialization of an unknown error type string.");
102        let json_str = r#""some_unknown_error""#;
103        let et: ErrorType = serde_json::from_str(json_str).expect("Failed to deserialize unknown error type.");
104        match et {
105            ErrorType::Unknown(s) => {
106                pretty_assert_eq!(s, "some_unknown_error");
107                trace!("Unknown variant holds the correct string: {:?}", s);
108            }
109            _ => panic!("Expected Unknown variant for an unrecognized string."),
110        }
111    }
112
113    #[traced_test]
114    fn should_serialize_insufficient_quota() {
115        info!("Testing serialization of InsufficientQuota variant.");
116        let et = ErrorType::InsufficientQuota;
117        let serialized = serde_json::to_string(&et).expect("Failed to serialize InsufficientQuota.");
118        pretty_assert_eq!(serialized, r#""insufficient_quota""#);
119        debug!("Serialized to {:?}", serialized);
120    }
121
122    #[traced_test]
123    fn should_serialize_invalid_request() {
124        info!("Testing serialization of InvalidRequest variant.");
125        let et = ErrorType::InvalidRequest;
126        let serialized = serde_json::to_string(&et).expect("Failed to serialize InvalidRequest.");
127        pretty_assert_eq!(serialized, r#""invalid_request""#);
128        debug!("Serialized to {:?}", serialized);
129    }
130
131    #[traced_test]
132    fn should_serialize_unknown() {
133        info!("Testing serialization of Unknown variant.");
134        let et = ErrorType::Unknown("fancy_weird_error".to_string());
135        let serialized = serde_json::to_string(&et).expect("Failed to serialize Unknown variant.");
136        pretty_assert_eq!(serialized, r#""fancy_weird_error""#);
137        debug!("Serialized to {:?}", serialized);
138    }
139
140    #[traced_test]
141    fn should_match_error_type_with_partial_eq_str() {
142        info!("Testing PartialEq for ErrorType with &str.");
143
144        let eq_insufficient = ErrorType::InsufficientQuota;
145        assert!(eq_insufficient == "insufficient_quota",
146            "Should match the correct string for InsufficientQuota."
147        );
148
149        let eq_invalid = ErrorType::InvalidRequest;
150        assert!(eq_invalid == "invalid_request",
151            "Should match the correct string for InvalidRequest."
152        );
153
154        let unknown = ErrorType::Unknown("my_unknown".to_string());
155        assert!(unknown == "my_unknown",
156            "Should match the correct string for Unknown variant."
157        );
158
159        warn!("PartialEq checks passed for known and unknown error types.");
160    }
161
162    #[traced_test]
163    fn should_return_correct_as_str() {
164        info!("Testing as_str() method on ErrorType.");
165
166        let e_insuf = ErrorType::InsufficientQuota;
167        pretty_assert_eq!(e_insuf.as_str(), "insufficient_quota");
168
169        let e_invalid = ErrorType::InvalidRequest;
170        pretty_assert_eq!(e_invalid.as_str(), "invalid_request");
171
172        let e_unknown = ErrorType::Unknown("some_error".to_string());
173        pretty_assert_eq!(e_unknown.as_str(), "some_error");
174        trace!("All as_str checks passed.");
175    }
176}