rename-item

Procedural macro to rename items on declaration, according to different case styles.

This crate provides the [macro@rename] attribute-like macro that changes the name of the item it is applied to. In order to refer to this changed name, this crate provides the [renamed!] function-like macro.

Both macros accept the same four named arguments for specifying the generated name:

The following case styles are supported:

| Case style | case = | Example | | ----------------- | ---------------- | --------- | | Upper camel case | "upper_camel" | FooBar | | Lower camel case | "lower_camel" | fooBar | | Snake case | "snake" | foo_bar | | Shouty snake case | "shouty_snake" | FOO_BAR |

This crate is mainly useful when writing declarative macros, as declarative macros cannot create new identifiers. Additionally, this crate allows declarative macros to adapt a given name to various case styles, useful when the macro defines different kinds of items.

Examples

```rust use rename_item::{rename, renamed};

// This function has the name _return_five

[rename(name = "return-five", prefix = "_")]

fn foo() -> i32 { 5 }

asserteq!(return_five(), 5);

// This obtains the name of the function using the macro arguments from above, and calls it let five = renamed!(name = "return-five", case = "snake", prefix = "")(); asserteq!(five, 5); ```

Here's a concrete example, a declarative macro that defines setters for struct members:

```rust macrorules! structwith_setters { ( // Match a normal struct definition struct $name:ident { $($member:ident: $ty:ty),* $(,)? } ) => { // Emit the same struct definition struct $name { $($member: $ty,)* }

    // Additionally emit an impl block with a setter function for each member
    impl $name {
        $(
            #[::rename_item::rename(name($member), prefix = "set_")]
            fn foo(&mut self, val: $ty) {
                self.$member = val;
            }
        )*
    }
};

}

structwithsetters! { struct Highscore { score: i32, player: String, } }

let mut h = Highscore { score: 42, player: "Hackerman".into(), }; h.setscore(9001); asserteq!(h.score, 9001); ```

License

Licensed under either of MIT License or Apache License, Version 2.0 at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.