rfc5545_types/request_status.rs
1//! Types for describing request statuses.
2
3/// A value of the REQUEST-STATUS property (RFC 5545 ยง3.8.8.3).
4#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub struct RequestStatus {
6 /// The hierarchical status code.
7 pub code: StatusCode,
8 /// A human-readable description of the status.
9 pub description: Box<str>,
10 /// Optional additional data about the status.
11 pub exception_data: Option<Box<str>>,
12}
13
14impl std::fmt::Display for RequestStatus {
15 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16 write!(f, "{};{}", self.code, self.description)?;
17 if let Some(data) = &self.exception_data {
18 write!(f, ";{data}")?;
19 }
20 Ok(())
21 }
22}
23
24/// A hierarchical status code (e.g. `2.0`, `3.1.2`).
25#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
26pub struct StatusCode {
27 /// The status class (1โ5).
28 pub class: Class,
29 /// The major status number within the class.
30 pub major: u8,
31 /// The optional minor status number.
32 pub minor: Option<u8>,
33}
34
35impl std::fmt::Display for StatusCode {
36 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37 write!(f, "{}.{}", self.class.as_u8(), self.major)?;
38 if let Some(minor) = self.minor {
39 write!(f, ".{minor}")?;
40 }
41 Ok(())
42 }
43}
44
45/// The class of a [`StatusCode`].
46#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
47pub enum Class {
48 /// Preliminary success.
49 ///
50 /// The request has been processed but completion is pending.
51 C1,
52 /// Complete success.
53 ///
54 /// The request has been completed successfully, although based on the specific status code a
55 /// fallback may have been taken.
56 C2,
57 /// Client error.
58 ///
59 /// The request was not successful, due to a syntactic or semantic error in the
60 /// client-formatted request. The request should not be retried until the error condition in
61 /// the request has been corrected.
62 C3,
63 /// Scheduling error.
64 ///
65 /// The request was not successful due to a semantic issue with the calendaring and scheduling
66 /// service not directly related to the request itself.
67 C4,
68 /// Service error.
69 ///
70 /// The request was not successful because a service either did not exist, was not available,
71 /// or was unsupported.
72 C5,
73}
74
75impl Class {
76 /// Returns the numeric value of this class (1โ5).
77 pub const fn as_u8(self) -> u8 {
78 match self {
79 Class::C1 => 1,
80 Class::C2 => 2,
81 Class::C3 => 3,
82 Class::C4 => 4,
83 Class::C5 => 5,
84 }
85 }
86
87 /// Creates a `Class` from a numeric value (1โ5), returning `None` if out of range.
88 pub const fn from_u8(value: u8) -> Option<Self> {
89 match value {
90 1 => Some(Class::C1),
91 2 => Some(Class::C2),
92 3 => Some(Class::C3),
93 4 => Some(Class::C4),
94 5 => Some(Class::C5),
95 _ => None,
96 }
97 }
98}