-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support top-level attributes for venndb derive
- Loading branch information
glendc
committed
Mar 31, 2024
1 parent
48979b2
commit 8a5e0ad
Showing
5 changed files
with
219 additions
and
5 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
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,65 @@ | ||
use crate::errors::Errors; | ||
|
||
/// Represents a `#[derive(VennDB)]` type's top-level attributes. | ||
#[derive(Default)] | ||
pub struct TypeAttrs { | ||
pub name: Option<syn::LitStr>, | ||
} | ||
|
||
impl TypeAttrs { | ||
/// Parse top-level `#[venndb(...)]` attributes | ||
pub fn parse(errors: &Errors, derive_input: &syn::DeriveInput) -> Self { | ||
let mut this = Self::default(); | ||
|
||
for attr in &derive_input.attrs { | ||
let ml = if let Some(ml) = venndb_attr_to_meta_list(errors, attr) { | ||
ml | ||
} else { | ||
continue; | ||
}; | ||
|
||
for meta in ml { | ||
let name = meta.path(); | ||
if name.is_ident("name") { | ||
if let Some(m) = errors.expect_meta_name_value(&meta) { | ||
this.name = errors.expect_lit_str(&m.value).cloned(); | ||
} | ||
} else { | ||
errors.err( | ||
&meta, | ||
concat!( | ||
"Invalid field-level `venndb` attribute\n", | ||
"Expected one of: `name`", | ||
), | ||
); | ||
} | ||
} | ||
} | ||
|
||
this | ||
} | ||
} | ||
|
||
/// Filters out non-`#[venndb(...)]` attributes and converts to a sequence of `syn::Meta`. | ||
fn venndb_attr_to_meta_list( | ||
errors: &Errors, | ||
attr: &syn::Attribute, | ||
) -> Option<impl IntoIterator<Item = syn::Meta>> { | ||
if !is_argh_attr(attr) { | ||
return None; | ||
} | ||
let ml = errors.expect_meta_list(&attr.meta)?; | ||
errors.ok(ml.parse_args_with( | ||
syn::punctuated::Punctuated::<syn::Meta, syn::Token![,]>::parse_terminated, | ||
)) | ||
} | ||
|
||
// Whether the attribute is one like `#[<name> ...]` | ||
fn is_matching_attr(name: &str, attr: &syn::Attribute) -> bool { | ||
attr.path().segments.len() == 1 && attr.path().segments[0].ident == name | ||
} | ||
|
||
/// Checks for `#[venndb ...]` | ||
fn is_argh_attr(attr: &syn::Attribute) -> bool { | ||
is_matching_attr("venndb", attr) | ||
} |
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,24 @@ | ||
use venndb::VennDB; | ||
|
||
#[derive(Debug, VennDB)] | ||
#[venndb(name = "Database")] | ||
struct Employee { | ||
id: u32, | ||
name: String, | ||
is_manager: bool, | ||
is_admin: bool, | ||
is_active: bool, | ||
department: Department, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum Department { | ||
Engineering, | ||
Sales, | ||
Marketing, | ||
HR, | ||
} | ||
|
||
fn main() { | ||
let _ = Database::new(); | ||
} |