1 unstable release
0.1.0 | Dec 6, 2024 |
---|
#17 in #plural
207 downloads per month
Used in 13 crates
(via ai-descriptor)
9KB
68 lines
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.
Features
- Customizable Pluralization: Use the
#[plural("...")]
attribute to specify custom plural forms for enum variants. - Default Pluralization: Automatically generates default plural forms by converting CamelCase variants to lowercase words and appending "s".
- Handles Special Cases: Supports special characters, spaces, and irregular pluralization rules.
- Type Safety: Provides compile-time enforcement for pluralization logic, ensuring correctness.
- Support for Empty Enums: Generates safe, uninhabited code for empty enums.
Example Usage
Add Dependencies
Add plural-derive
and plural-trait
to your Cargo.toml
:
[dependencies]
plural-derive = "0.1.0"
plural-trait = "0.1.0"
Define an Enum
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"
}
Custom Pluralization
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,
}
Empty Enum Handling
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.
Test Output
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");
Testing
Run the included test suite to verify functionality:
cargo test
How It Works
- Custom Attribute Parsing: The
#[plural("...")]
attribute allows specifying custom plural forms. - Default Pluralization Logic: For variants without a
#[plural(...)]
attribute, the macro converts CamelCase to lowercase words and appends "s". - Trait Implementation: The procedural macro generates an implementation of the
PluralDisplay
trait, which provides theplural()
method.
Limitations
- Only supports enums. Attempting to derive
Plural
for structs or other data types will result in a compile-time error. - Default pluralization is basic and may not handle all linguistic nuances.
Contributing
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.
- Fork the repository.
- Create your feature branch:
git checkout -b feature-name
. - Commit your changes:
git commit -m 'Add feature'
. - Push to the branch:
git push origin feature-name
. - Open a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgments
Special thanks to the Rust community for their resources and guidance in building procedural macros.
Dependencies
~230–680KB
~16K SLoC