Skip to main content

switchback_traits/
response_severity.rs

1//! Outcome severity classes for operation and RPC responses.
2//!
3//! Parsers map family-specific status codes into these classes at populate time
4//! via protocol implementations in `switchback-protocols`. Renderers read the
5//! stored entity field rather than re-deriving transport semantics.
6
7/// Cross-family outcome severity for a response or RPC result.
8///
9/// Renderers may use this for grouping, color, or ordering without re-deriving
10/// transport-specific semantics from raw status strings.
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
12#[non_exhaustive]
13pub enum ResponseSeverity {
14    /// Severity was not determined or does not apply.
15    #[default]
16    Unspecified,
17    /// Informational outcome (HTTP 1xx; gRPC equivalents are rare).
18    Informational,
19    /// Successful outcome (HTTP 2xx; gRPC `OK`).
20    Success,
21    /// Redirection or alternate continuation (HTTP 3xx).
22    Redirection,
23    /// Client-side fault (HTTP 4xx; most gRPC non-OK codes except internal/unavailable).
24    ClientError,
25    /// Server-side fault (HTTP 5xx; gRPC `INTERNAL`, `UNAVAILABLE`, etc.).
26    ServerError,
27}
28
29impl ResponseSeverity {
30    /// Stable wire and log slug (`unspecified`, `informational`, …).
31    pub fn as_str(self) -> &'static str {
32        match self {
33            Self::Unspecified => "unspecified",
34            Self::Informational => "informational",
35            Self::Success => "success",
36            Self::Redirection => "redirection",
37            Self::ClientError => "client_error",
38            Self::ServerError => "server_error",
39        }
40    }
41
42    /// Parse a stored slug; unknown values map to [`Unspecified`].
43    pub fn parse_str(s: &str) -> Self {
44        match s {
45            "informational" => Self::Informational,
46            "success" => Self::Success,
47            "redirection" => Self::Redirection,
48            "client_error" => Self::ClientError,
49            "server_error" => Self::ServerError,
50            _ => Self::Unspecified,
51        }
52    }
53}