use crate::wire_schema::{DescribeWire, EnumRepresentation, VariantBody, WireSchema, WireVariant};
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Clock {
#[default]
Real,
Simulated,
}
impl Clock {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Real => "real",
Self::Simulated => "simulated",
}
}
const ALL: [Self; 2] = [Self::Real, Self::Simulated];
}
impl DescribeWire for Clock {
fn wire_schema() -> WireSchema {
WireSchema::enumeration(
EnumRepresentation::ExternallyTagged,
Clock::ALL.map(|clock| WireVariant::new(clock.as_str(), VariantBody::Unit)),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_declared_shape_is_the_shape_serde_writes() {
for clock in Clock::ALL {
let json = serde_json::to_value(clock).expect("a clock domain serializes");
assert_eq!(json, serde_json::Value::from(clock.as_str()));
assert_eq!(Clock::wire_schema().conforms(&json), Ok(()));
}
}
}