Skip to content

Commit

Permalink
Add name() method
Browse files Browse the repository at this point in the history
  • Loading branch information
RenaudDenis committed Jul 15, 2024
1 parent a0ab209 commit 045ac49
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

# Version 1.3.0 (2024-07-15)

- Add `name()` method

# Version 1.2.0 (2024-07-02)

- Add `discriminant()` and `from_discriminant(num)` methods
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "unit-enum"
version = "1.2.0"
version = "1.3.0"
authors = ["Renaud Denis <[email protected]>"]
description = "A procedural macro for deriving ordinal methods in unit-like enums for Rust."
license = "MIT OR Apache-2.0"
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ consisting solely of unit variants. This macro simplifies working with such enum

## Features

- `name`: Retrieve the name of an enum variant.
- `ordinal`: Retrieve the ordinal of an enum variant, starting from 0.
- `from_ordinal`: Convert an ordinal back to an enum variant, if possible.
- `discriminant`: Retrieve the discriminant of an enum variant.
Expand All @@ -23,7 +24,7 @@ Add the following to your `Cargo.toml`:

```toml
[dependencies]
unit-enum = "1.2.0"
unit-enum = "1.3.0"
```

## Quick Start
Expand All @@ -39,6 +40,9 @@ enum Color {
}

fn main() {
println!("Name of Blue: {:?}", Color::Blue.name());
// Name of Blue: "Blue"

println!("Ordinal of Green: {:?}", Color::Green.ordinal());
// Ordinal of Green: 1

Expand Down
3 changes: 3 additions & 0 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ enum Color {
}

fn main() {
println!("Name of Blue: {:?}", Color::Blue.name());
// Name of Blue: "Blue"

println!("Ordinal of Green: {:?}", Color::Green.ordinal());
// Ordinal of Green: 1

Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ fn impl_unit_enum(ast: &DeriveInput) -> TokenStream {
let variants = &data.variants;
let num_variants = variants.len(); // Count the number of variants

let name_match_arms = variants.iter().enumerate().map(|(index, variant)| {
let variant_name = &variant.ident;
match &variant.fields {
Fields::Unit => quote! { #name::#variant_name => stringify!(#variant_name) },
_ => panic!("UnitEnum only supports unit variants (no fields)"),
}
});

let ordinal_match_arms = variants.iter().enumerate().map(|(index, variant)| {
let variant_name = &variant.ident;
match &variant.fields {
Expand All @@ -40,6 +48,13 @@ fn impl_unit_enum(ast: &DeriveInput) -> TokenStream {

let gen = quote! {
impl #name {
/// Returns the name of the enum variant.
pub fn name(&self) -> &str {
match self {
#(#name_match_arms,)*
}
}

/// Returns the zero-based ordinal of the enum variant.
pub fn ordinal(&self) -> usize {
match self {
Expand Down Expand Up @@ -85,6 +100,7 @@ fn impl_unit_enum(ast: &DeriveInput) -> TokenStream {
/// definition order, starting from the first variant.
pub fn values() -> impl Iterator<Item = Self> {
vec![#(#values_arms,)*].into_iter()

}
}
};
Expand Down

0 comments on commit 045ac49

Please sign in to comment.