use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ErrorCode {
code: &'static str,
status: u16,
}
impl ErrorCode {
pub const SUSPENDED: Self = Self {
code: "suspended",
status: 403,
};
pub const ARCHIVE_REQUIRED: Self = Self {
code: "archive_required",
status: 409,
};
pub const POD_DATA_PRESENT: Self = Self {
code: "pod_data_present",
status: 409,
};
pub const APP_FORBIDDEN: Self = Self {
code: "app_forbidden",
status: 403,
};
pub const RATE_LIMITED: Self = Self {
code: "rate_limited",
status: 429,
};
pub const ALL: [Self; 5] = [
Self::SUSPENDED,
Self::ARCHIVE_REQUIRED,
Self::POD_DATA_PRESENT,
Self::APP_FORBIDDEN,
Self::RATE_LIMITED,
];
pub const fn as_str(self) -> &'static str {
self.code
}
pub const fn status(self) -> u16 {
self.status
}
pub fn from_code(code: &str) -> Option<Self> {
Self::ALL.into_iter().find(|known| known.code == code)
}
}
impl fmt::Display for ErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.code)
}
}