Crates.io | plural-derive |
lib.rs | plural-derive |
version | 0.1.0 |
source | src |
created_at | 2024-12-06 00:02:47.70043 |
updated_at | 2024-12-06 00:02:47.70043 |
description | This crate contains the derive macro `Plural` we use with the `PluralDisplay` trait |
homepage | |
repository | https://github.com/klebs6/klebs-general |
max_upload_size | |
id | 1473761 |
size | 12,306 |
plural-derive
plural-derive
is a procedural macro crate for Rust that simplifies generating plural forms for enum variants. With this crate, you can derive the PluralDisplay
trait for enums, enabling a method to retrieve the pluralized version of each variant.
#[plural("...")]
attribute to specify custom plural forms for enum variants.Add plural-derive
and plural-trait
to your Cargo.toml
:
[dependencies]
plural-derive = "0.1.0"
plural-trait = "0.1.0"
use plural_derive::Plural;
use plural_trait::PluralDisplay;
#[derive(Plural, Debug)]
pub enum PoeticForms {
#[plural("alcaic stanzas")]
AlcaicStanza,
#[plural("ballads")]
Ballad,
#[plural("burns stanzas")]
BurnsStanza,
BlankVerse, // Default: "blank verses"
Couplet, // Default: "couplets"
Haiku, // Default: "haikus"
}
fn main() {
let form = PoeticForms::Haiku;
println!("Plural: {}", form.plural()); // Output: "Plural: haikus"
}
You can specify irregular or custom plural forms using the #[plural("...")]
attribute:
#[derive(Plural, Debug)]
pub enum CustomEnum {
#[plural("special cases")]
SpecialCase,
#[plural("irregular")]
IrregularForm,
}
Enums with no variants are supported safely:
#[derive(Plural, Debug)]
pub enum EmptyEnum {}
Attempting to use an instance of EmptyEnum
will result in a compile-time error.
The PluralDisplay
trait provides the plural()
method to retrieve the pluralized form of an enum variant:
use PoeticForms::*;
assert_eq!(AlcaicStanza.plural(), "alcaic stanzas");
assert_eq!(BlankVerse.plural(), "blank verses");
assert_eq!(Couplet.plural(), "couplets");
Run the included test suite to verify functionality:
cargo test
#[plural("...")]
attribute allows specifying custom plural forms.#[plural(...)]
attribute, the macro converts CamelCase to lowercase words and appends "s".PluralDisplay
trait, which provides the plural()
method.Plural
for structs or other data types will result in a compile-time error.Contributions are welcome! If you encounter bugs, have suggestions, or want to extend the functionality, feel free to open an issue or submit a pull request.
git checkout -b feature-name
.git commit -m 'Add feature'
.git push origin feature-name
.This project is licensed under the MIT License. See the LICENSE file for details.
Special thanks to the Rust community for their resources and guidance in building procedural macros.