use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum Kind {
#[default]
Plain,
AbsoluteTemperature,
TemperatureDifference,
}
impl Kind {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Plain => "plain",
Self::AbsoluteTemperature => "absolute_temperature",
Self::TemperatureDifference => "temperature_difference",
}
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
match name {
"plain" => Some(Self::Plain),
"absolute_temperature" => Some(Self::AbsoluteTemperature),
"temperature_difference" => Some(Self::TemperatureDifference),
_ => None,
}
}
}
impl fmt::Display for Kind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Shape {
Linear,
Affine,
}
impl Shape {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Linear => "linear",
Self::Affine => "affine",
}
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
match name {
"linear" => Some(Self::Linear),
"affine" => Some(Self::Affine),
_ => None,
}
}
}
impl fmt::Display for Shape {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Exactness {
Defined,
Measured,
}
impl Exactness {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Defined => "defined",
Self::Measured => "measured",
}
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
match name {
"defined" => Some(Self::Defined),
"measured" => Some(Self::Measured),
_ => None,
}
}
#[must_use]
pub const fn combine(self, other: Self) -> Self {
match (self, other) {
(Self::Defined, Self::Defined) => Self::Defined,
_ => Self::Measured,
}
}
}
impl fmt::Display for Exactness {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[cfg(test)]
mod tests {
use super::*;
const ALL: [Kind; 3] = [
Kind::Plain,
Kind::AbsoluteTemperature,
Kind::TemperatureDifference,
];
#[test]
fn names_round_trip() {
for k in ALL {
assert_eq!(Kind::from_name(k.name()), Some(k));
}
assert_eq!(Kind::from_name("nope"), None);
assert_eq!(Shape::from_name("affine"), Some(Shape::Affine));
assert_eq!(Exactness::from_name("measured"), Some(Exactness::Measured));
}
#[test]
fn exactness_combine() {
assert_eq!(
Exactness::Defined.combine(Exactness::Defined),
Exactness::Defined
);
assert_eq!(
Exactness::Defined.combine(Exactness::Measured),
Exactness::Measured
);
}
}