fix(expression): make TryInto to TryFrom trait

This commit is contained in:
Jonas Röger 2024-11-09 02:28:13 +01:00
parent eb2cda854f
commit 73065fc808
Signed by: jonas
GPG Key ID: 4000EB35E1AE0F07
2 changed files with 47 additions and 19 deletions

View File

@ -56,10 +56,10 @@ impl From<(Expression, Expression)> for Expression {
}
}
impl TryInto<i64> for Expression {
impl TryFrom<Expression> for i64 {
type Error = EvalError;
fn try_into(self) -> Result<i64, Self::Error> {
match self {
fn try_from(value: Expression) -> Result<i64, Self::Error> {
match value {
Expression::Integer(i) => Ok(i),
_ => Err(EvalError::TypeError(
"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;
fn try_into(self) -> Result<f64, Self::Error> {
match self {
fn try_from(value: Expression) -> Result<f64, Self::Error> {
match value {
Expression::Float(f) => Ok(f),
_ => Err(EvalError::TypeError(
"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;
fn try_into(self) -> Result<String, Self::Error> {
match self {
fn try_from(value: Expression) -> Result<String, Self::Error> {
match value {
Expression::String(s) => Ok(s),
_ => Err(EvalError::TypeError(
"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;
fn try_into(self) -> Result<Vec<Expression>, Self::Error> {
CellIterator::new(self).collect()
fn try_from(value: Expression) -> Result<Vec<Expression>, Self::Error> {
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;
fn try_into(self) -> Result<[Expression; N], Self::Error> {
let buf: Vec<Expression> = self.try_into()?;
fn try_from(value: Expression) -> Result<Vec<ToExpr>, Self::Error> {
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();
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;
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)),
_ => Err(EvalError::TypeError(
"Expression must be a Cell".to_string(),

View File

@ -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> {
let [args, body] = expr.try_into()?;
let [args, body]: [Expression; 2] = expr.try_into()?;
let mut arg_exprs: Vec<Expression> = args.try_into()?;
let argument_symbols: Vec<String> = arg_exprs
.iter_mut()