Hi
I am getting into a little bit of pickle with returning a trait object from a nom function.
I am not even sure what I want is possible.
I have a nom function like
fn scalar<'a>(i: &'a str) -> IResult<&'a str, Token, (&'a str, ErrorKind)> {
alt((ref, quoted_string, uri, boo,))(i)
}
This function returns a enum called Token which works fine.
What I want todo is to create a wrapper of type struct for each enum variant. All these wrappers will implement a trait called HVal.
Is it possible to define a nom function like below
fn val<'a>(i: &'a str) -> IResult<&'a str, Box<&dyn HVal>, (&'a str, ErrorKind)> {
map(
scalar,
|t: Token| {
let s: Scaler = Scaler::new(t); // I would have different structs here based on token
Box::new(&s)
}
)(i)
}
I can't seem to return traits here
expected trait object dyn hval::HVal
, found struct token::Scaler
|
= note: expected enum std::result::Result<(&'a str, std::boxed::Box<&'a (dyn hval::HVal + 'a)>), nom::internal::Err<(&'a str, nom::error::ErrorKind)>>
found enum std::result::Result<(&str, std::boxed::Box<&token::Scaler>), nom::internal::Err<(&str, nom::error::ErrorKind)>>
Is this possible ?
Thanks