#macro-derive #display #enums #case-conversion

macro derive_screaming_snake_case

A lightweight Rust proc-macro crate that implements the Display trait for enums with unit variants by converting each variant's name into SCREAMING_SNAKE_CASE

2 releases (1 stable)

new 1.0.0 May 15, 2025
0.1.0 May 15, 2025

#2720 in Rust patterns

Download history 215/week @ 2025-05-10

215 downloads per month

MIT license

8KB
66 lines

derive_screaming_snake_case

A lightweight Rust proc-macro crate that implements the Display trait for enums with unit variants by converting each variant's name into SCREAMING_SNAKE_CASE.

What It Does

This macro implements std::fmt::Display for enums where each variant is a unit variant (i.e., no data). It turns the variant name into SCREAMING_SNAKE_CASE at compile time.

Example

use derive_screaming_snake_case::Display;

#[derive(Display)]
enum Status {
    Ok,
    NotFound,
    InternalServerError,
}

fn main() {
    assert_eq!(Status::Ok.to_string(), "OK");
    assert_eq!(Status::NotFound.to_string(), "NOT_FOUND");
    assert_eq!(Status::InternalServerError.to_string(), "INTERNAL_SERVER_ERROR");
}

This Will Fail to Compile

use derive_screaming_snake_case::Display;

#[derive(Display)]
enum Message {
    // ❌ This is NOT a unit variant
    Text(String),

    // ❌ This is a struct variant
    Error { code: u32, message: String },

    // ✅ This is fine
    Ping,
}

License

Licensed under MIT license.
Copyright (c) 2025 Abhinandh S

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Dependencies

~185–610KB
~15K SLoC