#[derive(Debug, Copy, Clone)]
pub enum Error{
PrefixConvert
}
#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(u8)]
pub enum Prefix {
Quetta = b'Q',
Ronna = b'R',
Yotta = b'Y',
Zetta = b'Z',
Exa = b'E',
Peta = b'P',
Tera = b'T',
Giga = b'G',
Mega = b'M',
Kilo = b'k',
Hecto = b'h',
_Unit = b'\0',
Deci = b'd',
Centi = b'c',
Milli = b'm',
Micro = b'u',
Nano = b'n',
Pico = b'p',
Femto = b'f',
Atto = b'a',
Zepto = b'z',
Yocto = b'y',
Ronto = b'r',
Quecto = b'q',
}
impl Into<char> for Prefix {
#[inline]
fn into(self) -> char {
self as u8 as char
}
}
impl Into<f64> for Prefix {
#[inline]
fn into(self) -> f64 {
match self {
Prefix::Quetta=> 1e30,
Prefix::Ronna=> 1e27,
Prefix::Yotta=> 1e24,
Prefix::Zetta=> 1e21,
Prefix::Exa=> 1e18,
Prefix::Peta=> 1e15,
Prefix::Tera=> 1e12,
Prefix::Giga=> 1e9,
Prefix::Mega=> 1e6,
Prefix::Kilo=> 1e3,
Prefix::Hecto=> 1e2,
Prefix::_Unit=> 1e0,
Prefix::Deci=> 1e-1,
Prefix::Centi=> 1e-2,
Prefix::Milli=> 1e-3,
Prefix::Micro=> 1e-6,
Prefix::Nano=> 1e-9,
Prefix::Pico=> 1e-12,
Prefix::Femto=> 1e-15,
Prefix::Atto=> 1e-18,
Prefix::Zepto=> 1e-21,
Prefix::Yocto=> 1e-24,
Prefix::Ronto=> 1e-27,
Prefix::Quecto=> 1e-30,
}
}
}
impl Prefix {
#[inline]
pub fn from(c: char) -> Result<Prefix, Error> {
match c {
'Q' => Ok(Prefix::Quetta),
'R' => Ok(Prefix::Ronna),
'Y' => Ok(Prefix::Yotta),
'Z' => Ok(Prefix::Zetta),
'E' => Ok(Prefix::Exa),
'P' => Ok(Prefix::Peta),
'T' => Ok(Prefix::Tera),
'G' => Ok(Prefix::Giga),
'M' => Ok(Prefix::Mega),
'k' => Ok(Prefix::Kilo),
'h' => Ok(Prefix::Hecto),
'\0' => Ok(Prefix::_Unit),
'd' => Ok(Prefix::Deci),
'c' => Ok(Prefix::Centi),
'm' => Ok(Prefix::Milli),
'u' => Ok(Prefix::Micro),
'n' => Ok(Prefix::Nano),
'p' => Ok(Prefix::Pico),
'f' => Ok(Prefix::Femto),
'a' => Ok(Prefix::Atto),
'z' => Ok(Prefix::Zepto),
'y' => Ok(Prefix::Yocto),
'r' => Ok(Prefix::Ronto),
'q' => Ok(Prefix::Quecto),
_ => Err(Error::PrefixConvert),
}
}
}
impl Default for Prefix {
#[inline]
fn default() -> Self { Prefix::_Unit}
}
#[derive(Debug,Copy,Clone)]
#[allow(non_camel_case_types)]
pub enum Suffix {
}
#[derive(Debug,Copy,Clone)]
pub struct Unit {
pub scale: f64,
pub prefix: Prefix,
pub suffix: Suffix,
}
#[cfg(test)]
mod test {
mod prefix {
use std::slice::Iter;
use crate::unit::Prefix;
impl Prefix {
fn iter() -> Iter<'static, Prefix> {
static PREFIXS: [Prefix; 24] =
[
Prefix::Quetta,
Prefix::Ronna,
Prefix::Yotta,
Prefix::Zetta,
Prefix::Exa,
Prefix::Peta,
Prefix::Tera,
Prefix::Giga,
Prefix::Mega,
Prefix::Kilo,
Prefix::Hecto,
Prefix::_Unit,
Prefix::Deci,
Prefix::Centi,
Prefix::Milli,
Prefix::Micro,
Prefix::Nano,
Prefix::Pico,
Prefix::Femto,
Prefix::Atto,
Prefix::Zepto,
Prefix::Yocto,
Prefix::Ronto,
Prefix::Quecto,
];
PREFIXS.iter()
}
}
#[test]
fn convert() {
for prefix in Prefix::iter(){
let b: char = prefix.clone().into();
match Prefix::from(b) {
Ok(new_prefix) => { assert_eq!(new_prefix,prefix.clone()); }
Err(e) => { panic!("{:?}",e) }
}
}
}
}
}