1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
use std::{fmt, convert::TryFrom, str::FromStr, error::Error}; use enquote::unquote; use crate::AscesisError; #[derive(Clone, PartialEq, Eq, Debug)] pub enum Literal { Size(u64), Infinity, Name(String), } impl Literal { pub(crate) fn from_digits(digits: &str) -> Result<Self, Box<dyn Error>> { Ok(Literal::Size(u64::from_str(digits)?)) } #[inline] pub(crate) fn infinity() -> Self { Literal::Infinity } pub(crate) fn from_quoted_str(quoted: &str) -> Result<Self, Box<dyn Error>> { Ok(Literal::Name(unquote(quoted)?)) } } impl TryFrom<Literal> for u64 { type Error = AscesisError; fn try_from(lit: Literal) -> Result<Self, Self::Error> { if let Literal::Size(size) = lit { Ok(size) } else { Err(AscesisError::ExpectedSizeLiteral) } } } impl TryFrom<Literal> for String { type Error = &'static str; fn try_from(lit: Literal) -> Result<Self, Self::Error> { if let Literal::Name(name) = lit { Ok(name) } else { Err("Bad literal, not a string") } } } #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum BinOp { Add, ThinTx, ThinRx, FatTx, FatRx, FatDx, } impl fmt::Display for BinOp { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { BinOp::Add => '+'.fmt(f), BinOp::ThinTx => "->".fmt(f), BinOp::ThinRx => "<-".fmt(f), BinOp::FatTx => "=>".fmt(f), BinOp::FatRx => "<=".fmt(f), BinOp::FatDx => "<=>".fmt(f), } } }