From e791ad57bcf2cb4460e254b458a631b5a5a48351 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 25 Oct 2021 00:22:25 -0700 Subject: crate-local Result alias for less verbose type signatures --- src/cexpr.rs | 43 ++++++++++++++++++++----------------------- src/lib.rs | 2 ++ 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/cexpr.rs b/src/cexpr.rs index 357caff..5a171bd 100644 --- a/src/cexpr.rs +++ b/src/cexpr.rs @@ -5,6 +5,7 @@ */ use crate::sexpr::SExpr; +use crate::Result; use std::fmt; #[derive(Debug, Clone, PartialEq, PartialOrd)] @@ -46,7 +47,7 @@ pub enum CNumber { } impl CNumber { - pub fn to_sexpr(&self) -> Result { + pub fn to_sexpr(&self) -> Result { match self { CNumber::Integer(v) => Ok(SExpr::SInteger(*v)), CNumber::Rational(a, b) => Ok(SExpr::SList(vec![ @@ -123,7 +124,7 @@ fn compute_factorial(n: i64) -> i64 { } impl CExpr { - pub fn from_sexpr(sexpr: &SExpr) -> Result { + pub fn from_sexpr(sexpr: &SExpr) -> Result { // not all cases are handled; some atoms are covered trivialy match sexpr { SExpr::SNull => Err("null not handled".to_string()), @@ -141,7 +142,7 @@ impl CExpr { } } - pub fn from_sexpr_list(list: &Vec) -> Result { + pub fn from_sexpr_list(list: &Vec) -> Result { use CExpr::*; use CNumber::*; use SExpr::*; @@ -200,20 +201,16 @@ impl CExpr { } }, // TODO: how to make range unbounded? or less bounded? - ("+", 2..=5000) => { - CExpr::new_sum(rest.iter().map(|v| CExpr::from_sexpr(v)).collect::, - String, - >>( - )?) - } - ("*", 2..=5000) => { - CExpr::new_product(rest.iter().map(|v| CExpr::from_sexpr(v)).collect::, - String, - >>( - )?) - } + ("+", 2..=5000) => CExpr::new_sum( + rest.iter() + .map(|v| CExpr::from_sexpr(v)) + .collect::>>()?, + ), + ("*", 2..=5000) => CExpr::new_product( + rest.iter() + .map(|v| CExpr::from_sexpr(v)) + .collect::>>()?, + ), ("-", 2) => { let a = CExpr::from_sexpr(&rest[0])?; let b = CExpr::from_sexpr(&rest[1])?; @@ -236,7 +233,7 @@ impl CExpr { } } - pub fn new_sum(list: Vec) -> Result { + pub fn new_sum(list: Vec) -> Result { use CExpr::*; use CNumber::*; let (numeric, mut rest): (Vec, Vec) = list @@ -273,7 +270,7 @@ impl CExpr { } } - pub fn new_product(list: Vec) -> Result { + pub fn new_product(list: Vec) -> Result { use CExpr::*; use CNumber::*; let (numeric, mut rest): (Vec, Vec) = list @@ -310,7 +307,7 @@ impl CExpr { } } - pub fn to_sexpr(&self) -> Result { + pub fn to_sexpr(&self) -> Result { match self { CExpr::Symbol(s) => Ok(SExpr::SIdentifier(s.to_string())), CExpr::Number(n) => n.to_sexpr(), @@ -319,7 +316,7 @@ impl CExpr { let mut list = l .iter() .map(|v| v.to_sexpr()) - .collect::, String>>()?; + .collect::>>()?; list.insert(0, SExpr::SIdentifier("+".to_string())); if let Some(num) = n { list.insert(1, num.to_sexpr()?); @@ -330,7 +327,7 @@ impl CExpr { let mut list = l .iter() .map(|v| v.to_sexpr()) - .collect::, String>>()?; + .collect::>>()?; list.insert(0, SExpr::SIdentifier("*".to_string())); if let Some(num) = n { list.insert(1, num.to_sexpr()?); @@ -353,7 +350,7 @@ impl CExpr { } } - pub fn from_str(raw: &str) -> Result { + pub fn from_str(raw: &str) -> Result { let ast = SExpr::from_str(raw)?; CExpr::from_sexpr(&ast) } diff --git a/src/lib.rs b/src/lib.rs index 950329b..4137840 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,8 @@ mod sexpr; pub use cexpr::{CExpr, CNumber}; pub use sexpr::{sexpr_parse_file, SExpr}; +pub type Result = std::result::Result; + pub fn repl(_verbose: bool) { let stdin = io::stdin(); let mut stdout = io::stdout(); -- cgit v1.2.3