sort!

added a new sort!(vec![], field) macro to the askama templates for ordinary.

The Macro

basically it just makes it possible to take a Vec of structs and sort them based on a field.

macro_rules! sort {
    ($v:expr, $f:ident) => {{
        let mut v = $v.clone();
        v.sort_by(|a, b| a.$f.cmp(&b.$f));
        v
    }};
}

Usage

real world: on this blog's sitemap.xml

struct MyStruct {
    my_field: u8,
}

let my_vec = vec![
    MyStruct { my_field: 3 },
    MyStruct { my_field: 2 },
    MyStruct { my_field: 1 },
];

sort!(my_vec, my_field);

Conclusion

this is the first time in my few years of writing Rust that i've written a declarative macro; previously they seemed scary but Askama doesn't really leave me with many better options ("Variables (no mutability allowed)") so finally just forced myself to figure it out.

seems like with a bit more time and exposure i could actually learn to like working with macro_rules!... just worried that i could come to like them a little too much and end up mostly writing overly-clever, unreadable, nonsense.