-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring + make DB Enums more concise with proc macros
- Loading branch information
1 parent
7ec7623
commit 9cf15f4
Showing
11 changed files
with
232 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
pub mod string_manipulation; | ||
|
||
use quote::quote; | ||
|
||
pub fn quote_display_impl( | ||
enum_name: syn::Ident, | ||
variant_idents: Vec<&syn::Ident>, | ||
display_results: &[String], | ||
) -> proc_macro2::TokenStream { | ||
let display = { | ||
let iter = display_results.iter(); | ||
quote! { | ||
impl std::fmt::Display for #enum_name { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
#(Self::#variant_idents => { | ||
write!( | ||
f, | ||
#iter | ||
) | ||
})* | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
let as_str = { | ||
let iter = display_results.iter(); | ||
quote! { | ||
impl #enum_name { | ||
pub fn as_str(&self) -> &'static str { | ||
match self { | ||
#(Self::#variant_idents => { | ||
#iter | ||
})* | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
quote! { | ||
#display | ||
#as_str | ||
} | ||
} | ||
|
||
pub fn impl_display<'a>( | ||
enum_name: syn::Ident, | ||
variant_idents: Vec<&'a syn::Ident>, | ||
display_pat: fn(ident: &'a syn::Ident) -> String, | ||
) -> proc_macro2::TokenStream { | ||
let res = variant_idents | ||
.iter() | ||
.map(|i| display_pat(i)) | ||
.collect::<Vec<_>>(); | ||
|
||
quote_display_impl(enum_name, variant_idents, &res) | ||
} | ||
|
||
pub fn impl_display_with_vals<'a>( | ||
enum_name: syn::Ident, | ||
variant_idents: Vec<&'a syn::Ident>, | ||
variants_values: Vec<String>, | ||
display_pat: fn(ident: &'a syn::Ident, val: &str) -> String, | ||
) -> proc_macro2::TokenStream { | ||
let res = variant_idents | ||
.iter() | ||
.zip(variants_values.iter()) | ||
.map(|(i, v)| display_pat(i, v)) | ||
.collect::<Vec<_>>(); | ||
|
||
quote_display_impl(enum_name, variant_idents, &res) | ||
} | ||
|
||
pub fn get_variant_str_values_by_name(enum_item: syn::DataEnum, name: &str) -> Vec<String> { | ||
enum_item | ||
.variants | ||
.iter() | ||
.filter_map(|v| { | ||
if !v.attrs.iter().any(|attr| attr.path().is_ident(name)) { | ||
return None; | ||
} | ||
|
||
v.attrs | ||
.iter() | ||
.find(|attr| attr.path().is_ident(name)) | ||
.map(|attr| match &attr.meta { | ||
syn::Meta::NameValue(nv) => match &nv.value { | ||
syn::Expr::Lit(lit_expr) => match &lit_expr.lit { | ||
syn::Lit::Str(str) => Some(str.value()), | ||
_ => None, | ||
}, | ||
_ => None, | ||
}, | ||
_ => None, | ||
}) | ||
}) | ||
.map(|x| x.unwrap()) | ||
.collect() | ||
} |
15 changes: 15 additions & 0 deletions
15
serenity_discord_bot_derive/src/utils/string_manipulation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
pub fn pascal_to_snake_case(s: &str) -> String { | ||
let mut res = String::with_capacity(s.len()); | ||
|
||
let mut chars = s.chars().peekable(); | ||
while let Some(c) = chars.next() { | ||
res.push_str(&c.to_lowercase().to_string()); | ||
if let Some(next) = chars.peek() { | ||
if next.is_uppercase() { | ||
res.push('_'); | ||
} | ||
} | ||
} | ||
|
||
res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.