use core::fmt;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Type {
Int,
Float,
Bool,
Unit,
}
impl Type {
#[must_use]
pub const fn is_numeric(self) -> bool {
matches!(self, Type::Int | Type::Float)
}
}
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match self {
Type::Int => "int",
Type::Float => "float",
Type::Bool => "bool",
Type::Unit => "unit",
};
f.write_str(name)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_type_is_numeric_classifies_each_variant() {
assert!(Type::Int.is_numeric());
assert!(Type::Float.is_numeric());
assert!(!Type::Bool.is_numeric());
assert!(!Type::Unit.is_numeric());
}
#[test]
fn test_type_display_matches_lowercase_name() {
assert_eq!(Type::Int.to_string(), "int");
assert_eq!(Type::Float.to_string(), "float");
assert_eq!(Type::Bool.to_string(), "bool");
assert_eq!(Type::Unit.to_string(), "unit");
}
}