#derive #proc-macro #macro-derive #static #variant #enums #static-str

macro derive_static_str

A procedural macro to derive static str implementations

2 releases

0.1.1 Dec 24, 2024
0.1.0 Dec 23, 2024

#500 in Procedural macros

Download history 59/week @ 2024-12-17 220/week @ 2024-12-24 9/week @ 2024-12-31

288 downloads per month

MIT license

10KB
113 lines

derive_static_str

A proc macro for composing static string slices from enum variants. Sometimes you just want a &'static str with some prefix or suffix...

Crates.io

Documentation

Usage

Add derive_static_str and strum to your Cargo.toml:

[dependencies]
derive_static_str = "0.1.0"
strum = { version = "0.24", features = ["derive"] }

Both prefixes and suffixes can be added via:

#[static_str(prefix = "...")]
#[static_str(suffix = "...")]

The overall case of the output can be controlled with strum's serialize_all.

use derive_static_str::{static_str, DeriveStaticStr};
use strum::EnumString;

#[derive(EnumString, DeriveStaticStr)]
#[static_str(prefix = "my_prefix_", suffix = "_suffix")]
#[strum(serialize_all = "snake_case")]
enum MyEnum {
    VariantOne,
    VariantTwo,
}

fn main() {
    assert_eq!(MyEnum::VariantOne.as_static_str(), "my_prefix_variant_one_suffix");
    assert_eq!(MyEnum::VariantTwo.as_static_str(), "my_prefix_variant_two_suffix");
}

Dependencies

~2MB
~44K SLoC