Skip to content

Commit

Permalink
Add fmt::Display to ResponseStatusCode.
Browse files Browse the repository at this point in the history
Dirt simple PR to implement displaying of response codes.
  • Loading branch information
RobbieMcKinstry committed Oct 24, 2024
1 parent b55bebf commit 8e07a98
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::stats::EnumerableCategory;
use std::fmt;

/// [ResponseStatusCode] groups HTTP response status codes according
/// to five general categories. This type is used as the dependent
Expand All @@ -17,8 +18,42 @@ pub enum ResponseStatusCode {
_5XX,
}

impl fmt::Display for ResponseStatusCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::_1XX => write!(f, "1XX"),
Self::_2XX => write!(f, "2XX"),
Self::_3XX => write!(f, "3XX"),
Self::_4XX => write!(f, "4XX"),
Self::_5XX => write!(f, "5XX"),
}
}
}

impl EnumerableCategory for ResponseStatusCode {
fn groups() -> Box<dyn Iterator<Item = Self>> {
Box::new([Self::_1XX, Self::_2XX, Self::_3XX, Self::_4XX, Self::_5XX].into_iter())
}
}

#[cfg(test)]
mod tests {
use super::ResponseStatusCode;
use pretty_assertions::assert_str_eq;

#[test]
fn fmt_response_status_code() {
let test_cases = [
(ResponseStatusCode::_1XX, "1XX"),
(ResponseStatusCode::_2XX, "2XX"),
(ResponseStatusCode::_3XX, "3XX"),
(ResponseStatusCode::_4XX, "4XX"),
(ResponseStatusCode::_5XX, "5XX"),
];

for (input, expected) in test_cases {
let observed = input.to_string();
assert_str_eq!(expected, observed);
}
}
}

0 comments on commit 8e07a98

Please sign in to comment.