#enums #template #different #variant #askama #enum-template

macro askama-enum

Implement different askama templates for different enum variants

2 releases

0.0.2 Apr 22, 2022
0.0.1 Apr 21, 2022

#1482 in Procedural macros

ISC OR MIT OR Apache-2…

24KB
393 lines

askama-enum

GitHub Workflow Status Crates.io Minimum supported Rust version License

Implement different Askama templates for different enum variants.

#[derive(EnumTemplate)]
#[template(ext = "html", source = "default")] // default, optional
enum MyEnum<'a, T: std::fmt::Display> {
    // uses the default `#[template]`
    A,

    // uses specific `#[template]`
    #[template(ext = "html", source = "B")]
    B,

    // you can use tuple structs
    #[template(
        ext = "html",
        source = "{{self.0}} {{self.1}} {{self.2}} {{self.3}}",
    )]
    C(u8, &'a u16, u32, &'a u64),

    // and named fields, too
    #[template(ext = "html", source = "{{some}} {{fields}}")]
    D { some: T, fields: T },
}

assert_eq!(
    MyEnum::A::<&str>.to_string(),
    "default",
);
assert_eq!(
    MyEnum::B::<&str>.to_string(),
    "B",
);
assert_eq!(
    MyEnum::C::<&str>(1, &2, 3, &4).to_string(),
    "1 2 3 4",
);
assert_eq!(
    MyEnum::D { some: "some", fields: "fields" }.to_string(),
    "some fields",
);

Dependencies

~1.1–1.6MB
~38K SLoC