derive_static_str

Crates.ioderive_static_str
lib.rsderive_static_str
version0.1.1
created_at2024-12-23 19:49:25.544713+00
updated_at2024-12-24 00:05:28.60119+00
descriptionA procedural macro to derive static str implementations
homepage
repositoryhttps://github.com/iamnbutler/derive_static_str
max_upload_size
id1493367
size12,216
Nate Butler (iamnbutler)

documentation

README

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");
}
Commit count: 4

cargo fmt