aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2021-10-25 00:22:25 -0700
committerBryan Newbold <bnewbold@robocracy.org>2021-10-25 00:22:25 -0700
commite791ad57bcf2cb4460e254b458a631b5a5a48351 (patch)
treec3f2905435aa247847ac424f31232a0e58c41db7
parentb3141319b98199d75f1317ef3754c0b6b14b1f78 (diff)
downloadcasual-e791ad57bcf2cb4460e254b458a631b5a5a48351.tar.gz
casual-e791ad57bcf2cb4460e254b458a631b5a5a48351.zip
crate-local Result alias for less verbose type signatures
-rw-r--r--src/cexpr.rs43
-rw-r--r--src/lib.rs2
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<SExpr, String> {
+ pub fn to_sexpr(&self) -> Result<SExpr> {
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<CExpr, String> {
+ pub fn from_sexpr(sexpr: &SExpr) -> Result<CExpr> {
// 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<SExpr>) -> Result<CExpr, String> {
+ pub fn from_sexpr_list(list: &Vec<SExpr>) -> Result<CExpr> {
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::<Result<
- Vec<CExpr>,
- String,
- >>(
- )?)
- }
- ("*", 2..=5000) => {
- CExpr::new_product(rest.iter().map(|v| CExpr::from_sexpr(v)).collect::<Result<
- Vec<CExpr>,
- String,
- >>(
- )?)
- }
+ ("+", 2..=5000) => CExpr::new_sum(
+ rest.iter()
+ .map(|v| CExpr::from_sexpr(v))
+ .collect::<Result<Vec<CExpr>>>()?,
+ ),
+ ("*", 2..=5000) => CExpr::new_product(
+ rest.iter()
+ .map(|v| CExpr::from_sexpr(v))
+ .collect::<Result<Vec<CExpr>>>()?,
+ ),
("-", 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<CExpr>) -> Result<CExpr, String> {
+ pub fn new_sum(list: Vec<CExpr>) -> Result<CExpr> {
use CExpr::*;
use CNumber::*;
let (numeric, mut rest): (Vec<CExpr>, Vec<CExpr>) = list
@@ -273,7 +270,7 @@ impl CExpr {
}
}
- pub fn new_product(list: Vec<CExpr>) -> Result<CExpr, String> {
+ pub fn new_product(list: Vec<CExpr>) -> Result<CExpr> {
use CExpr::*;
use CNumber::*;
let (numeric, mut rest): (Vec<CExpr>, Vec<CExpr>) = list
@@ -310,7 +307,7 @@ impl CExpr {
}
}
- pub fn to_sexpr(&self) -> Result<SExpr, String> {
+ pub fn to_sexpr(&self) -> Result<SExpr> {
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::<Result<Vec<SExpr>, String>>()?;
+ .collect::<Result<Vec<SExpr>>>()?;
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::<Result<Vec<SExpr>, String>>()?;
+ .collect::<Result<Vec<SExpr>>>()?;
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<CExpr, String> {
+ pub fn from_str(raw: &str) -> Result<CExpr> {
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<T, E = String> = std::result::Result<T, E>;
+
pub fn repl(_verbose: bool) {
let stdin = io::stdin();
let mut stdout = io::stdout();