You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.0 KiB
47 lines
1.0 KiB
4 years ago
|
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> = ConversionError;
|
||
|
|
||
|
fn convert(&self, input: &str) -> Result<Self::Output, Self::Error>;
|
||
|
}
|
||
|
|
||
|
#[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<Infallible> 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<str>, value: impl AsRef<str>) -> 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(())
|
||
|
}
|