Skip to main content

radiate_expr/
from.rs

1use crate::{Expr, ExprNode};
2use radiate_error::{RadiateError, radiate_err};
3
4impl TryFrom<Expr> for f32 {
5    type Error = RadiateError;
6
7    fn try_from(value: Expr) -> Result<Self, Self::Error> {
8        if let ExprNode::Literal(lit) = value.node {
9            let extracted = lit.extract::<f32>();
10            match extracted {
11                Some(val) => Ok(val),
12                None => Err(radiate_err!(Expr: "Failed to extract f32 from literal")),
13            }
14        } else {
15            Err(radiate_err!(Expr: "Cannot convert Expr to f32: Expr is not a literal"))
16        }
17    }
18}