pub struct ResponseCode(/* private fields */);Expand description
Wire-format responseCode.
Construct via ResponseCode::parse (defensive — never fails) or via
ResponseCode::from_parts (validated). The raw string is preserved
verbatim even when malformed.
Implementations§
Source§impl ResponseCode
impl ResponseCode
Sourcepub fn parse(s: impl Into<String>) -> Self
pub fn parse(s: impl Into<String>) -> Self
Wrap an arbitrary string. Never fails — the value is preserved as-is.
Sourcepub fn from_parts(http: u16, service: ServiceCode, case: u8) -> Self
pub fn from_parts(http: u16, service: ServiceCode, case: u8) -> Self
Construct from validated (http, service, case) parts. Panics if http
is outside 100..=999 or case is outside 0..=99 — either would widen
the canonical 7-digit HHHSSCC wire form (e.g. case = 100 formats as
three digits, yielding an 8-char code that every reader then rejects).
Use ResponseCode::parse when you cannot guarantee the inputs.
Sourcepub fn success(service: ServiceCode, sub: u8) -> Self
pub fn success(service: ServiceCode, sub: u8) -> Self
Construct a 2-prefix success code (2HH SSCC with HH=00).
Sourcepub fn raw(&self) -> &str
pub fn raw(&self) -> &str
The verbatim wire string.
Examples found in repository?
23fn main() -> Result<(), Box<dyn std::error::Error>> {
24 let wires = [
25 r#"{"responseCode":"2001100","responseMessage":"Successful","accountNo":"1234","currentBalance":"99000.00"}"#,
26 r#"{"responseCode":"4011100","responseMessage":"Unauthorized. Invalid token"}"#,
27 r#"{"responseCode":"4031114","responseMessage":"Insufficient Funds"}"#,
28 // BRI's 6-digit source-doc defect — still parseable, raw preserved.
29 r#"{"responseCode":"500000","responseMessage":"General Error"}"#,
30 ];
31
32 for wire in wires {
33 println!("---");
34 println!("wire: {wire}");
35
36 let resp: SnapResponse<BalancePayload> = serde_json::from_str(wire)?;
37 println!("raw responseCode: {}", resp.envelope.response_code.raw());
38
39 match resp.envelope.response_code.classify() {
40 Some(err) => {
41 println!("classified error: {err}");
42 println!("category: {}", err.category());
43 let action = match err.category() {
44 Category::Business => "do NOT retry (business rule violation)",
45 Category::System => "retry with backoff",
46 Category::Message => "fix client request before retry",
47 Category::Success => "no action",
48 _ => "review manually",
49 };
50 println!("policy: {action}");
51 }
52 None => {
53 if let Some(payload) = resp.payload() {
54 println!("success! account {} balance {}", payload.account_no, payload.current_balance);
55 } else {
56 println!("unrecognized code with no payload — log and escalate");
57 }
58 }
59 }
60 }
61 Ok(())
62}Sourcepub fn http(&self) -> Option<StatusCode>
pub fn http(&self) -> Option<StatusCode>
Extract the leading HTTP status, defensively. None if the code is not
in the canonical 7-digit form or the leading 3 digits don’t form a valid
HTTP status.
Sourcepub fn service(&self) -> Option<ServiceCode>
pub fn service(&self) -> Option<ServiceCode>
Extract the service code (positions 4..5). None for malformed codes.
Sourcepub fn case(&self) -> Option<u8>
pub fn case(&self) -> Option<u8>
Extract the case code (positions 6..7). None for malformed codes.
Sourcepub fn classify(&self) -> Option<Error>
pub fn classify(&self) -> Option<Error>
Inverse classifier: given a received wire code, return the matching
Error variant (with empty-string payload where the variant carries
one). None for unknown HTTP+case pairs and for malformed wire forms.
Designed for client-side use — receiving a response and reasoning about it typed.
Examples found in repository?
23fn main() -> Result<(), Box<dyn std::error::Error>> {
24 let wires = [
25 r#"{"responseCode":"2001100","responseMessage":"Successful","accountNo":"1234","currentBalance":"99000.00"}"#,
26 r#"{"responseCode":"4011100","responseMessage":"Unauthorized. Invalid token"}"#,
27 r#"{"responseCode":"4031114","responseMessage":"Insufficient Funds"}"#,
28 // BRI's 6-digit source-doc defect — still parseable, raw preserved.
29 r#"{"responseCode":"500000","responseMessage":"General Error"}"#,
30 ];
31
32 for wire in wires {
33 println!("---");
34 println!("wire: {wire}");
35
36 let resp: SnapResponse<BalancePayload> = serde_json::from_str(wire)?;
37 println!("raw responseCode: {}", resp.envelope.response_code.raw());
38
39 match resp.envelope.response_code.classify() {
40 Some(err) => {
41 println!("classified error: {err}");
42 println!("category: {}", err.category());
43 let action = match err.category() {
44 Category::Business => "do NOT retry (business rule violation)",
45 Category::System => "retry with backoff",
46 Category::Message => "fix client request before retry",
47 Category::Success => "no action",
48 _ => "review manually",
49 };
50 println!("policy: {action}");
51 }
52 None => {
53 if let Some(payload) = resp.payload() {
54 println!("success! account {} balance {}", payload.account_no, payload.current_balance);
55 } else {
56 println!("unrecognized code with no payload — log and escalate");
57 }
58 }
59 }
60 }
61 Ok(())
62}Trait Implementations§
Source§impl Clone for ResponseCode
impl Clone for ResponseCode
Source§fn clone(&self) -> ResponseCode
fn clone(&self) -> ResponseCode
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ResponseCode
impl Debug for ResponseCode
Source§impl<'de> Deserialize<'de> for ResponseCode
impl<'de> Deserialize<'de> for ResponseCode
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for ResponseCode
impl Display for ResponseCode
impl Eq for ResponseCode
Source§impl Hash for ResponseCode
impl Hash for ResponseCode
Source§impl PartialEq for ResponseCode
impl PartialEq for ResponseCode
Source§fn eq(&self, other: &ResponseCode) -> bool
fn eq(&self, other: &ResponseCode) -> bool
self and other values to be equal, and is used by ==.Source§impl Serialize for ResponseCode
impl Serialize for ResponseCode
impl StructuralPartialEq for ResponseCode
Auto Trait Implementations§
impl Freeze for ResponseCode
impl RefUnwindSafe for ResponseCode
impl Send for ResponseCode
impl Sync for ResponseCode
impl Unpin for ResponseCode
impl UnsafeUnpin for ResponseCode
impl UnwindSafe for ResponseCode
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.