fix(expression): make TryInto to TryFrom trait
This commit is contained in:
parent
eb2cda854f
commit
73065fc808
@ -56,10 +56,10 @@ impl From<(Expression, Expression)> for Expression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryInto<i64> for Expression {
|
impl TryFrom<Expression> for i64 {
|
||||||
type Error = EvalError;
|
type Error = EvalError;
|
||||||
fn try_into(self) -> Result<i64, Self::Error> {
|
fn try_from(value: Expression) -> Result<i64, Self::Error> {
|
||||||
match self {
|
match value {
|
||||||
Expression::Integer(i) => Ok(i),
|
Expression::Integer(i) => Ok(i),
|
||||||
_ => Err(EvalError::TypeError(
|
_ => Err(EvalError::TypeError(
|
||||||
"Expression is not an Integer".to_string(),
|
"Expression is not an Integer".to_string(),
|
||||||
@ -68,10 +68,10 @@ impl TryInto<i64> for Expression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryInto<f64> for Expression {
|
impl TryFrom<Expression> for f64 {
|
||||||
type Error = EvalError;
|
type Error = EvalError;
|
||||||
fn try_into(self) -> Result<f64, Self::Error> {
|
fn try_from(value: Expression) -> Result<f64, Self::Error> {
|
||||||
match self {
|
match value {
|
||||||
Expression::Float(f) => Ok(f),
|
Expression::Float(f) => Ok(f),
|
||||||
_ => Err(EvalError::TypeError(
|
_ => Err(EvalError::TypeError(
|
||||||
"Expression is not a Float".to_string(),
|
"Expression is not a Float".to_string(),
|
||||||
@ -80,10 +80,10 @@ impl TryInto<f64> for Expression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryInto<String> for Expression {
|
impl TryFrom<Expression> for String {
|
||||||
type Error = EvalError;
|
type Error = EvalError;
|
||||||
fn try_into(self) -> Result<String, Self::Error> {
|
fn try_from(value: Expression) -> Result<String, Self::Error> {
|
||||||
match self {
|
match value {
|
||||||
Expression::String(s) => Ok(s),
|
Expression::String(s) => Ok(s),
|
||||||
_ => Err(EvalError::TypeError(
|
_ => Err(EvalError::TypeError(
|
||||||
"Expression is not a String".to_string(),
|
"Expression is not a String".to_string(),
|
||||||
@ -92,19 +92,35 @@ impl TryInto<String> for Expression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryInto<Vec<Expression>> for Expression {
|
impl TryFrom<Expression> for Vec<Expression> {
|
||||||
type Error = EvalError;
|
type Error = EvalError;
|
||||||
|
|
||||||
fn try_into(self) -> Result<Vec<Expression>, Self::Error> {
|
fn try_from(value: Expression) -> Result<Vec<Expression>, Self::Error> {
|
||||||
CellIterator::new(self).collect()
|
CellIterator::new(value).collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> TryInto<[Expression; N]> for Expression {
|
impl<ToExpr> TryFrom<Expression> for Vec<ToExpr>
|
||||||
|
where
|
||||||
|
ToExpr: TryFrom<Expression, Error = EvalError>,
|
||||||
|
{
|
||||||
type Error = EvalError;
|
type Error = EvalError;
|
||||||
|
|
||||||
fn try_into(self) -> Result<[Expression; N], Self::Error> {
|
fn try_from(value: Expression) -> Result<Vec<ToExpr>, Self::Error> {
|
||||||
let buf: Vec<Expression> = self.try_into()?;
|
CellIterator::new(value)
|
||||||
|
.map(|x| x?.try_into() as Result<ToExpr, EvalError>)
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<ToExpr, const N: usize> TryFrom<Expression> for [ToExpr; N]
|
||||||
|
where
|
||||||
|
ToExpr: TryFrom<Expression, Error = EvalError>,
|
||||||
|
{
|
||||||
|
type Error = EvalError;
|
||||||
|
|
||||||
|
fn try_from(value: Expression) -> Result<[ToExpr; N], Self::Error> {
|
||||||
|
let buf: Vec<ToExpr> = value.try_into()?;
|
||||||
let n = buf.len();
|
let n = buf.len();
|
||||||
|
|
||||||
buf.try_into()
|
buf.try_into()
|
||||||
@ -112,10 +128,22 @@ impl<const N: usize> TryInto<[Expression; N]> for Expression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryInto<(Expression, Expression)> for Expression {
|
impl<const N: usize> TryFrom<Expression> for [Expression; N] {
|
||||||
type Error = EvalError;
|
type Error = EvalError;
|
||||||
fn try_into(self) -> Result<(Expression, Expression), Self::Error> {
|
|
||||||
match self {
|
fn try_from(value: Expression) -> Result<[Expression; N], Self::Error> {
|
||||||
|
let buf: Vec<Expression> = value.try_into()?;
|
||||||
|
let n = buf.len();
|
||||||
|
|
||||||
|
buf.try_into()
|
||||||
|
.map_err(|_| EvalError::ArgumentError(format!("Expected {} arguments, got {}", N, n)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<Expression> for (Expression, Expression) {
|
||||||
|
type Error = EvalError;
|
||||||
|
fn try_from(value: Expression) -> Result<(Expression, Expression), Self::Error> {
|
||||||
|
match value {
|
||||||
Expression::Cell(a, b) => Ok((*a, *b)),
|
Expression::Cell(a, b) => Ok((*a, *b)),
|
||||||
_ => Err(EvalError::TypeError(
|
_ => Err(EvalError::TypeError(
|
||||||
"Expression must be a Cell".to_string(),
|
"Expression must be a Cell".to_string(),
|
||||||
|
|||||||
@ -79,7 +79,7 @@ pub fn prelude_div(env: &Environment, expr: Expression) -> Result<Expression, Ev
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn prelude_lambda(_env: &Environment, expr: Expression) -> Result<Expression, EvalError> {
|
pub fn prelude_lambda(_env: &Environment, expr: Expression) -> Result<Expression, EvalError> {
|
||||||
let [args, body] = expr.try_into()?;
|
let [args, body]: [Expression; 2] = expr.try_into()?;
|
||||||
let mut arg_exprs: Vec<Expression> = args.try_into()?;
|
let mut arg_exprs: Vec<Expression> = args.try_into()?;
|
||||||
let argument_symbols: Vec<String> = arg_exprs
|
let argument_symbols: Vec<String> = arg_exprs
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user