Crate cl_format_macros

Source
Expand description

The macros here should auto generate several traits and the major TildeAble trait.

For example:

#[derive(Debug, PartialEq, TildeAble)]
pub enum TildeKind {
    /// ~C ~:C
    #[implTo(char)]
    Char,

    /// ~$ ~5$ ~f
    #[implTo(float)]
    Float(Option<String>),

    /// ~d ~:d ~:@d
    Digit(Option<String>),

    /// ~a
    #[implTo(float, char, String)]
    Va,

    /// loop
    Loop(Vec<Tilde>),

    /// text inside the tilde
    Text(String),

    /// vec
    VecTilde(Vec<Tilde>),
}

Will generate:

/// all default method is return none.
trait TildeAble {
    fn len(&self) -> usize;
    fn into_tildekind_char(&self) -> Option<&dyn TildeKindChar>{None}
    fn into_tildekind_va(&self) -> Option<&dyn TildeKindVa>{None}
    // and all other fields...
}

impl TildeAble for char {
    fn into_tildekind_char(&self) -> Option<&dyn TildeKindChar> {
        Some(self)
    }

    fn into_tildekind_va(&self) -> Option<&dyn TildeKindVa> {
        Some(self)
    }
}

impl TildeAble for float {
    fn into_tildekind_va(&self) -> Option<&dyn TildeKindVa> {
        Some(self)
    }
}

impl TildeAble for String {
    fn into_tildekind_va(&self) -> Option<&dyn TildeKindVa> {
        Some(self)
    }
}

trait TildeKindChar {
    fn format(&self, tkind: &TildeKind, buf: &mut String) -> Result<(), TildeError> {
        Err("un-implenmented yet".into())
    }
}

trait TildeKindVa {
    fn format(&self, tkind: &TildeKind, buf: &mut String) -> Result<(), TildeError> {
        Err("un-implenmented yet".into())
    }
}

Derive Macrosยง

TildeAble