ast_from_str

Macro ast_from_str 

Source
macro_rules! ast_from_str {
    ($($ast:ty),*) => { ... };
}
Expand description

A convenience macro to implement Ast for a type which implements FromStr.

Note that FromStr has no associated lifetime, so such an implementation cannot borrow from the original string.

ยงExample

enum Size {
    Small,
    Big,
}

impl std::str::FromStr for Size {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "small" => Ok(Self::Small),
            "big" => Ok(Self::Big),
            s => Err(format!("Invalid size: {s}")),
        }
    }
}

mufmt::types::ast_from_str!(Size);
// Now, `Size` implements `Ast`.