tokio-gemini/src/status.rs

83 lines
2 KiB
Rust
Raw Normal View History

2024-07-31 21:14:29 +03:00
use crate::error::LibError;
use num_enum::{IntoPrimitive, TryFromPrimitive};
2024-08-01 09:32:55 +03:00
#[derive(Debug, Clone, Copy)]
2024-07-31 21:14:29 +03:00
pub struct Status {
status_code: StatusCode,
reply_type: ReplyType,
second_digit: u8,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
#[num_enum(error_type(name = LibError, constructor = LibError::status_out_of_range))]
#[repr(u8)]
pub enum ReplyType {
Input = 1,
Success,
Redirect,
TempFail,
PermFail,
Auth,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
#[num_enum(error_type(name = LibError, constructor = LibError::status_out_of_range))]
#[repr(u8)]
pub enum StatusCode {
Input = 10,
InputSensitive = 11,
Success = 20,
TempRedirect = 30,
PermRedirect = 31,
TempFail = 40,
ServerUnavailable = 41,
CgiError = 42,
ProxyError = 43,
SlowDown = 44,
PermFail = 50,
NotFound = 51,
Gone = 52,
ProxyRequestRefused = 53,
BadRequest = 59,
ClientCerts = 60,
CertNotAuthorized = 61,
CertNotValid = 62,
}
const ASCII_ZERO: u8 = 48; // '0'
impl Status {
pub fn parse_status(buf: &[u8]) -> Result<Self, LibError> {
2024-08-01 11:28:13 +03:00
// simple decimal digit conversion
// '2' - '0' = 50 - 48 = 2 (from byte '2' to uint 2)
2024-07-31 21:14:29 +03:00
let first = buf[0] - ASCII_ZERO;
let second = buf[1] - ASCII_ZERO;
Ok(Status {
2024-08-01 11:28:13 +03:00
// get enum item for 2-digit status code
2024-07-31 21:14:29 +03:00
status_code: StatusCode::try_from_primitive(first * 10 + second)?,
2024-08-01 11:28:13 +03:00
// get enum entry for first digit
2024-07-31 21:14:29 +03:00
reply_type: ReplyType::try_from_primitive(first)?,
2024-08-01 11:28:13 +03:00
// provide separate field for the 2nd digit
2024-07-31 21:14:29 +03:00
second_digit: second,
})
}
pub fn status_code(self: &Self) -> StatusCode {
self.status_code
}
pub fn reply_type(self: &Self) -> ReplyType {
self.reply_type
}
pub fn second_digit(self: &Self) -> u8 {
self.second_digit
}
}