use std::any::TypeId;
use std::fmt;
use std::hash::{Hash, Hasher};
pub trait ErrorCode: Copy + Send + Sync + 'static {
fn to_i32(self) -> i32;
fn from_i32(value: i32) -> Option<Self>;
fn name(self) -> &'static str;
}
impl<T: buffa::Enumeration + Send + Sync + 'static> ErrorCode for T {
fn to_i32(self) -> i32 {
buffa::Enumeration::to_i32(&self)
}
fn from_i32(value: i32) -> Option<T> {
<T as buffa::Enumeration>::from_i32(value)
}
fn name(self) -> &'static str {
buffa::Enumeration::proto_name(&self)
}
}
#[derive(Clone, Copy)]
pub struct Code {
kind: TypeId,
value: i32,
name: &'static str,
}
impl Code {
#[must_use]
pub fn of<C: ErrorCode>(code: C) -> Code {
Code {
kind: TypeId::of::<C>(),
value: code.to_i32(),
name: code.name(),
}
}
#[must_use]
pub fn get<C: ErrorCode>(self) -> Option<C> {
if self.kind == TypeId::of::<C>() {
C::from_i32(self.value)
} else {
None
}
}
#[must_use]
pub fn is<C: ErrorCode>(self, code: C) -> bool {
self == Code::of(code)
}
#[must_use]
pub fn value(self) -> i32 {
self.value
}
#[must_use]
pub fn name(self) -> &'static str {
self.name
}
}
impl<C: ErrorCode> From<C> for Code {
fn from(code: C) -> Code {
Code::of(code)
}
}
impl PartialEq for Code {
fn eq(&self, other: &Code) -> bool {
self.kind == other.kind && self.value == other.value
}
}
impl Eq for Code {}
impl Hash for Code {
fn hash<H: Hasher>(&self, state: &mut H) {
self.kind.hash(state);
self.value.hash(state);
}
}
impl fmt::Debug for Code {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Code({}={})", self.name, self.value)
}
}
impl fmt::Display for Code {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RuntimeCode {
Panicked = 1,
CannotBlock = 2,
Cancelled = 3,
}
impl ErrorCode for RuntimeCode {
fn to_i32(self) -> i32 {
self as i32
}
fn from_i32(value: i32) -> Option<RuntimeCode> {
match value {
1 => Some(RuntimeCode::Panicked),
2 => Some(RuntimeCode::CannotBlock),
3 => Some(RuntimeCode::Cancelled),
_ => None,
}
}
fn name(self) -> &'static str {
match self {
RuntimeCode::Panicked => "PANICKED",
RuntimeCode::CannotBlock => "CANNOT_BLOCK",
RuntimeCode::Cancelled => "CANCELLED",
}
}
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use super::{Code, ErrorCode, RuntimeCode};
#[derive(Clone, Copy, Debug, PartialEq)]
enum Other {
One = 1,
}
impl ErrorCode for Other {
fn to_i32(self) -> i32 {
self as i32
}
fn from_i32(value: i32) -> Option<Other> {
(value == 1).then_some(Other::One)
}
fn name(self) -> &'static str {
"ONE"
}
}
#[test]
fn runtime_codes_round_trip() {
for code in [
RuntimeCode::Panicked,
RuntimeCode::CannotBlock,
RuntimeCode::Cancelled,
] {
assert_eq!(RuntimeCode::from_i32(code.to_i32()), Some(code));
assert_eq!(Code::of(code).get::<RuntimeCode>(), Some(code));
}
assert_eq!(RuntimeCode::from_i32(0), None);
}
#[test]
fn same_number_of_another_type_is_another_code() {
let runtime = Code::of(RuntimeCode::Panicked);
let other = Code::of(Other::One);
assert_eq!(runtime.value(), other.value());
assert_ne!(runtime, other);
assert_eq!(other.get::<RuntimeCode>(), None);
assert!(!other.is(RuntimeCode::Panicked));
let set: HashSet<Code> = [runtime, other, Code::of(Other::One)].into();
assert_eq!(set.len(), 2);
}
#[test]
fn display_and_debug_show_the_name() {
let code = Code::from(RuntimeCode::CannotBlock);
assert_eq!(code.to_string(), "CANNOT_BLOCK");
assert_eq!(format!("{code:?}"), "Code(CANNOT_BLOCK=2)");
}
}