Skip to content

Commit

Permalink
🔨 Add Nameln
Browse files Browse the repository at this point in the history
  • Loading branch information
turtton committed Sep 12, 2024
1 parent 346942f commit 5d0f05d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,48 @@ fn main() {
}
```

### Nameln

```rust

use vodca::Nameln;

#[derive(Nameln)]
pub enum Events<T> {
Created { id: i64 },
Updated(T),
Deleted,
}

/// generated by vodca
impl<T> Enum<T> {
#[allow(unused_variables, non_snake_case)]
pub fn name(&self) -> String {
let name = match self {
Enum::Created { id: i64 } => "Created",
Enum::Updated(T) => "Updated",
Enum::Deleted => "Deleted",
};
name.to_string()
}
}
///

fn main() {
let i64: Enum<i64> = Enum::Created { id: 42 };
let name = i64.name();
assert_eq!(name, "Created");

let generics = Enum::Updated("Hello, world!".to_string());
let name = generics.name();
assert_eq!(name, "Updated");

let deleted: Enum<&str> = Enum::Deleted;
let name = deleted.name();
assert_eq!(name, "Deleted");
}
```

## Both

### Newln
Expand Down
31 changes: 30 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
use quote::quote;
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields, FieldsNamed, FieldsUnnamed};
use syn::{
parse_macro_input, Data, DataEnum, DataStruct, DeriveInput, Fields, FieldsNamed, FieldsUnnamed,
};

#[proc_macro_derive(Fromln)]
pub fn derive_fromln(input: TokenStream) -> TokenStream {
Expand Down Expand Up @@ -157,3 +159,30 @@ pub fn derive_references(input: TokenStream) -> TokenStream {
}
.into()
}

#[proc_macro_derive(Nameln)]
pub fn derive_name(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let name = &ast.ident;
let generics = &ast.generics;

let names = match ast.data {
Data::Enum(DataEnum { variants, .. }) => variants.into_iter().map(|variant| {
let variant_name = format!("{}", &variant.ident);
quote! { #name::#variant => #variant_name }
}),
_ => unimplemented!("Only enums are supported"),
};
quote! {
impl #generics #name #generics {
#[allow(unused_variables, non_snake_case)]
pub fn name(&self) -> String {
let name = match self {
#(#names,)*
};
name.to_string()
}
}
}
.into()
}
22 changes: 22 additions & 0 deletions tests/name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use vodca::Nameln;

#[derive(Nameln)]
pub enum Enum<T> {
I64 { id: i64 },
Generics(T),
Deleted,
}

fn main() {
let i64: Enum<i64> = Enum::I64 { id: 42 };
let name = i64.name();
assert_eq!(name, "I64");

let generics = Enum::Generics("Hello, world!".to_string());
let name = generics.name();
assert_eq!(name, "Generics");

let deleted: Enum<&str> = Enum::Deleted;
let name = deleted.name();
assert_eq!(name, "Deleted");
}
1 change: 1 addition & 0 deletions tests/parse_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ fn test() {
let try_test = trybuild::TestCases::new();
try_test.pass("tests/as_ref.rs");
try_test.pass("tests/from.rs");
try_test.pass("tests/name.rs");
try_test.pass("tests/new.rs");
try_test.pass("tests/references.rs");
try_test.pass("tests/references_generics.rs");
Expand Down

0 comments on commit 5d0f05d

Please sign in to comment.