pub mod codes;
use std::fmt::{self, Debug, Display};
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct DiagnosticCode(u16);
impl DiagnosticCode {
#[must_use]
pub const fn from_raw(raw: u16) -> Self {
Self(raw)
}
#[must_use]
pub const fn raw(self) -> u16 {
self.0
}
}
impl Debug for DiagnosticCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(self, f)
}
}
impl Display for DiagnosticCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "E{}", self.0)
}
}
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct RegisteredDiagnosticCode(u16);
impl RegisteredDiagnosticCode {
#[must_use]
pub const fn raw_code(self) -> DiagnosticCode {
DiagnosticCode(self.0)
}
}
impl Debug for RegisteredDiagnosticCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(self, f)
}
}
impl Display for RegisteredDiagnosticCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.raw_code(), f)
}
}
pub(in crate::diagnostics) const fn registered(raw: u16) -> RegisteredDiagnosticCode {
assert!(raw != 0, "diagnostic code zero is not allocatable");
RegisteredDiagnosticCode(raw)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn raw_and_registered_formatting_is_compact_and_numeric() {
let raw = DiagnosticCode::from_raw(65_000);
let registered = codes::ACCESS_UNAVAILABLE;
assert_eq!(raw.raw(), 65_000);
assert_eq!(raw.to_string(), "E65000");
assert_eq!(format!("{raw:?}"), "E65000");
assert_eq!(registered.raw_code().raw(), 1);
assert_eq!(registered.to_string(), "E1");
assert_eq!(format!("{registered:?}"), "E1");
}
}