use std::error; use std::fmt; use std::convert::Infallible; pub mod bytes; pub trait Conversion { const UNIT: &'static str; type Output: fmt::Display; type Error: Into = ConversionError; fn convert(&self, input: &str) -> Result; } #[derive(Debug)] pub struct ConversionError(String); impl error::Error for ConversionError{} impl fmt::Display for ConversionError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "conversion failed: {}", self.0) } } impl From for ConversionError { fn from(from: Infallible) -> Self { match from {} } } /// Dispatch a conversion of `value` for type `unit` and print the result to stdout. pub fn dispatch_out(unit: impl AsRef, value: impl AsRef) -> Result<(), ConversionError> { match unit.as_ref() { bytes::Bytes::UNIT => println!("{}", bytes::Bytes.convert(value.as_ref())?), name => return Err(ConversionError(format!("Unknown unit {}", name))), } Ok(()) }