Ast

Trait Ast 

Source
pub trait Ast<'fmt>: Sized {
    type Error;

    // Required method
    fn from_expr(expr: &'fmt str) -> Result<Self, Self::Error>;
}
Expand description

A typed representation of an expression which does not interpret the contents.

Often, you can use a provided implementation.

The role of an Ast is to perform as much validation as possible, without any knowledge of the Manifest which may later use it. The correct balance here depends, of course, on the particular use-case.

§Example

A HashMap uses &str as its Ast implementation. An &str is not aware at all of the contents of an expression, except that it should interpret it as a string. In contrast, if we are aware that the keys must come from a specific list, we can check that this is the case when the template is compiled.

use mufmt::{Ast, BorrowedTemplate};

/// The permitted colours
enum Color {
    Red,
    Green,
    Blue,
}

struct InvalidColor(pub String);

/// An `Ast` implementation which requires a string matching one of the colors.
impl Ast<'_> for Color {
    type Error = InvalidColor;

    fn from_expr(expr: &str) -> Result<Self, Self::Error> {
        // expressions are whitespace trimmed, so we don't need to handle this here
        match expr {
            "red" => Ok(Self::Red),
            "green" => Ok(Self::Green),
            "blue" => Ok(Self::Blue),
            s => Err(InvalidColor(s.to_owned())),
        }
    }
}

assert!(BorrowedTemplate::<Color>::compile("My favourite colors are {blue} and { green }!").is_ok());
assert!(BorrowedTemplate::<Color>::compile("The sky is very {orange}!").is_err());

Required Associated Types§

Source

type Error

An error which may occur while parsing an expression.

Required Methods§

Source

fn from_expr(expr: &'fmt str) -> Result<Self, Self::Error>

Parse the Ast from the expression.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Ast<'_> for Infallible

Source§

type Error = ()

Source§

fn from_expr(_: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for IpAddr

Source§

type Error = <IpAddr as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for SocketAddr

Source§

type Error = <SocketAddr as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for bool

Source§

type Error = <bool as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for char

Source§

type Error = <char as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for f32

Source§

type Error = <f32 as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for f64

Source§

type Error = <f64 as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for i8

Source§

type Error = <i8 as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for i16

Source§

type Error = <i16 as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for i32

Source§

type Error = <i32 as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for i64

Source§

type Error = <i64 as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for i128

Source§

type Error = <i128 as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for isize

Source§

type Error = <isize as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for u8

Source§

type Error = <u8 as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for u16

Source§

type Error = <u16 as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for u32

Source§

type Error = <u32 as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for u64

Source§

type Error = <u64 as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for u128

Source§

type Error = <u128 as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for ()

Source§

type Error = NotEmpty

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for usize

Source§

type Error = <usize as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for String

Source§

type Error = <String as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for Ipv4Addr

Source§

type Error = <Ipv4Addr as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for Ipv6Addr

Source§

type Error = <Ipv6Addr as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for SocketAddrV4

Source§

impl Ast<'_> for SocketAddrV6

Source§

impl Ast<'_> for NonZero<i8>

Source§

type Error = <NonZero<i8> as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for NonZero<i16>

Source§

type Error = <NonZero<i16> as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for NonZero<i32>

Source§

type Error = <NonZero<i32> as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for NonZero<i64>

Source§

type Error = <NonZero<i64> as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for NonZero<i128>

Source§

type Error = <NonZero<i128> as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for NonZero<isize>

Source§

type Error = <NonZero<isize> as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for NonZero<u8>

Source§

type Error = <NonZero<u8> as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for NonZero<u16>

Source§

type Error = <NonZero<u16> as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for NonZero<u32>

Source§

type Error = <NonZero<u32> as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for NonZero<u64>

Source§

type Error = <NonZero<u64> as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for NonZero<u128>

Source§

type Error = <NonZero<u128> as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for NonZero<usize>

Source§

type Error = <NonZero<usize> as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl Ast<'_> for PathBuf

Source§

type Error = <PathBuf as FromStr>::Err

Source§

fn from_expr(s: &str) -> Result<Self, Self::Error>

Source§

impl<'fmt> Ast<'fmt> for &'fmt str

Source§

type Error = Infallible

Source§

fn from_expr(s: &'fmt str) -> Result<Self, Self::Error>

Source§

impl<'fmt> Ast<'fmt> for &'fmt Path

Source§

type Error = Infallible

Source§

fn from_expr(s: &'fmt str) -> Result<Self, Self::Error>

Source§

impl<'fmt, T: Ast<'fmt>> Ast<'fmt> for Option<T>

Source§

type Error = <T as Ast<'fmt>>::Error

Source§

fn from_expr(expr: &'fmt str) -> Result<Self, Self::Error>

Source§

impl<'fmt, T: Ast<'fmt>> Ast<'fmt> for Result<T, T::Error>

Source§

type Error = Infallible

Source§

fn from_expr(expr: &'fmt str) -> Result<Self, Self::Error>

Source§

impl<'fmt, T: Ast<'fmt>> Ast<'fmt> for Box<T>

Source§

type Error = <T as Ast<'fmt>>::Error

Source§

fn from_expr(s: &'fmt str) -> Result<Self, Self::Error>

Source§

impl<'fmt, T: Ast<'fmt>> Ast<'fmt> for Rc<T>

Source§

type Error = <T as Ast<'fmt>>::Error

Source§

fn from_expr(s: &'fmt str) -> Result<Self, Self::Error>

Source§

impl<'fmt, T: Ast<'fmt>> Ast<'fmt> for Arc<T>

Source§

type Error = <T as Ast<'fmt>>::Error

Source§

fn from_expr(s: &'fmt str) -> Result<Self, Self::Error>

Implementors§