Skip to content

Commit 09f1450

Browse files
committed
feat(derive): Add better string parsing support
1 parent 5b423ed commit 09f1450

File tree

6 files changed

+281
-19
lines changed

6 files changed

+281
-19
lines changed

geekorm-derive/src/attr.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@
5151
use proc_macro2::{Span, TokenStream};
5252
use quote::{quote, ToTokens};
5353
use syn::{
54-
parse::{Parse, ParseStream},
54+
parse::{discouraged::AnyDelimiter, Parse, ParseStream},
5555
punctuated::Punctuated,
5656
spanned::Spanned,
57+
token::{Bracket, Comma},
5758
Attribute, Ident, LitBool, LitInt, LitStr, Token,
5859
};
5960

@@ -70,6 +71,8 @@ pub(crate) struct GeekAttribute {
7071
pub(crate) enum GeekAttributeKeys {
7172
/// Rename the field for the table
7273
Rename,
74+
/// Key
75+
Key,
7376
/// Unique value
7477
Unique,
7578
/// New Constructor
@@ -82,6 +85,8 @@ pub(crate) enum GeekAttributeKeys {
8285
NotNull,
8386
/// Foreign Key
8487
ForeignKey,
88+
/// Aliases
89+
Aliases,
8590
/// Random value
8691
Rand,
8792
RandLength,
@@ -93,17 +98,21 @@ pub(crate) enum GeekAttributeKeys {
9398
/// Searchable
9499
Searchable,
95100
/// On Actions
101+
OnValidate,
96102
OnUpdate,
97103
OnSave,
98104
/// Skip this field
99105
Skip,
106+
/// Disable features
107+
Disable,
100108
}
101109

102-
#[derive(Debug, Clone)]
110+
#[derive(Debug, Clone, PartialEq)]
103111
pub(crate) enum GeekAttributeValue {
104112
String(String),
105113
Int(i64),
106114
Bool(bool),
115+
Vec(Vec<String>),
107116
}
108117

109118
impl GeekAttribute {
@@ -240,7 +249,16 @@ impl GeekAttribute {
240249
Ok(())
241250
}
242251
}
243-
252+
Some(GeekAttributeKeys::Key) => {
253+
if self.value.is_none() {
254+
Err(syn::Error::new(
255+
self.span.span(),
256+
"The `key` attribute requires a string or int value",
257+
))
258+
} else {
259+
Ok(())
260+
}
261+
}
244262
_ => Ok(()),
245263
}
246264
}
@@ -253,7 +271,10 @@ impl Parse for GeekAttribute {
253271

254272
let key: Option<GeekAttributeKeys> = match name_str.as_str() {
255273
"skip" => Some(GeekAttributeKeys::Skip),
274+
"disable" => Some(GeekAttributeKeys::Disable),
256275
"rename" => Some(GeekAttributeKeys::Rename),
276+
"key" | "name" => Some(GeekAttributeKeys::Key),
277+
"aliases" => Some(GeekAttributeKeys::Aliases),
257278
// Primary Keys
258279
"primary_key" => Some(GeekAttributeKeys::PrimaryKey),
259280
"auto_increment" => Some(GeekAttributeKeys::AutoIncrement),
@@ -262,6 +283,7 @@ impl Parse for GeekAttribute {
262283
// Foreign Key
263284
"foreign_key" => Some(GeekAttributeKeys::ForeignKey),
264285
// Functions on action
286+
"validate" | "on_validate" => Some(GeekAttributeKeys::OnValidate),
265287
"update" | "on_update" | "on_update_write" => Some(GeekAttributeKeys::OnUpdate),
266288
"save" | "on_save" | "on_save_write" => Some(GeekAttributeKeys::OnSave),
267289

geekorm-derive/src/derive/column.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ impl ColumnDerive {
179179
// If the column is unique, then it should be searchable by default
180180
self.mode = Some(ColumnMode::Searchable { enabled: true });
181181
}
182+
GeekAttributeKeys::OnValidate => {
183+
if let Some(GeekAttributeValue::Bool(validate)) = &attr.value {
184+
self.save = Some(validate.to_string());
185+
self.update = Some(validate.to_string());
186+
}
187+
}
182188
GeekAttributeKeys::OnUpdate => {
183189
if let Some(GeekAttributeValue::String(value)) = &attr.value {
184190
self.update = Some(value.to_string());
@@ -340,6 +346,11 @@ impl ColumnDerive {
340346
GeekAttributeKeys::HashAlgorithm => {
341347
// Skip
342348
}
349+
GeekAttributeKeys::Key
350+
| GeekAttributeKeys::Disable
351+
| GeekAttributeKeys::Aliases => {
352+
// Skip (enum keys)
353+
}
343354
}
344355
} else {
345356
// TODO(geekmasher): Handle this better

geekorm-derive/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ pub fn depricated_table_derive(input: TokenStream) -> TokenStream {
7474
/// enum Role {
7575
/// Admin,
7676
/// Moderator,
77+
/// #[geekorm(key = "UserAccounts")]
7778
/// User,
7879
/// #[default]
7980
/// Guest,
@@ -92,8 +93,13 @@ pub fn depricated_table_derive(input: TokenStream) -> TokenStream {
9293
/// # assert_eq!(geekmasher.role, Role::Admin);
9394
/// # assert_eq!(Value::from(geekmasher.role), Value::Text("Admin".to_string()));
9495
/// # assert_eq!(Role::from(Value::Text("Admin".to_string())), Role::Admin);
96+
/// # assert_eq!(Role::from(Value::Text("UserAccounts".to_string())), Role::User);
97+
///
98+
/// let role = Role::from("UserAccounts");
99+
/// # assert_eq!(role, Role::User);
100+
/// # assert_eq!(role.to_string(), String::from("UserAccounts"));
95101
/// ```
96-
#[proc_macro_derive(Data)]
102+
#[proc_macro_derive(Data, attributes(geekorm))]
97103
pub fn data_derive(input: TokenStream) -> TokenStream {
98104
let ast: DeriveInput = parse_macro_input!(input as DeriveInput);
99105

geekorm-derive/src/parsers.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use syn::{
88
#[cfg(feature = "rand")]
99
use geekorm_core::utils::generate_random_string;
1010
use geekorm_core::{Columns, Table};
11-
use values::{generate_from_value, generate_serde};
11+
use values::{generate_from_value, generate_serde, generate_strings};
1212

1313
mod helpers;
1414
mod tablebuilder;
@@ -74,12 +74,20 @@ pub(crate) fn derive_parser(ast: &DeriveInput) -> Result<TokenStream, syn::Error
7474
pub(crate) fn enum_parser(ast: &DeriveInput) -> Result<TokenStream, syn::Error> {
7575
let name = &ast.ident;
7676

77+
let attributes = GeekAttribute::parse_all(&ast.attrs)?;
78+
7779
match &ast.data {
7880
Data::Enum(DataEnum { variants, .. }) => {
7981
let mut tokens = TokenStream::new();
8082

8183
tokens.extend(generate_from_value(name, variants, &ast.generics)?);
8284
tokens.extend(generate_serde(name, variants, &ast.generics)?);
85+
tokens.extend(generate_strings(
86+
name,
87+
variants,
88+
&ast.generics,
89+
&attributes,
90+
)?);
8391

8492
Ok(tokens)
8593
}

geekorm-derive/src/parsers/tablebuilder.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,6 @@ pub fn generate_backend(
258258
})?;
259259

260260
auto_update.extend(quote! {
261-
#[cfg(feature = "log")]
262-
::log::debug!("Auto updating field: '{}'", stringify!(#ident));
263261
self.#ident = #auto;
264262
});
265263
}

0 commit comments

Comments
 (0)