Skip to content

Commit

Permalink
Add export_keys optional param to native_db macro
Browse files Browse the repository at this point in the history
This adds an optional `export_keys` bool param to the `native_db` proc macro that controls the visibility of the model's secondary key enum.

Default value is `false`, which matches the existing behaviour of setting the visibility of the secondary key enum to `pub(crate)`. Using `#[native_db(export_keys = true)]` changes this to `pub`.

See vincent-herlemont#220 for discussion.
  • Loading branch information
Grimeh committed Sep 1, 2024
1 parent 50063a0 commit 1e19533
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
6 changes: 4 additions & 2 deletions native_db_macro/src/model_attributes.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use crate::keys::{KeyDefinition, KeyOptions};
use crate::struct_name::StructName;
use proc_macro::Ident;
use std::collections::HashSet;
use syn::meta::ParseNestedMeta;
use syn::parse::Result;
use syn::Field;
use syn::{Field, LitBool};

#[derive(Clone)]
pub(crate) struct ModelAttributes {
pub(crate) struct_name: StructName,
pub(crate) primary_key: Option<KeyDefinition<()>>,
pub(crate) secondary_keys: HashSet<KeyDefinition<KeyOptions>>,
pub(crate) do_export_keys: Option<LitBool>,
}

impl ModelAttributes {
Expand Down Expand Up @@ -60,6 +60,8 @@ impl ModelAttributes {
Ok(())
})?;
self.secondary_keys.insert(key);
} else if meta.path.is_ident("export_keys") {
self.do_export_keys = Some(meta.value()?.parse()?);
} else {
panic!(
"Unknown attribute: {}",
Expand Down
17 changes: 16 additions & 1 deletion native_db_macro/src/model_native_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::struct_name::StructName;
use crate::ToTokenStream;
use proc_macro::Span;
use quote::quote;
use syn::Ident;
use syn::{Ident};

pub(crate) struct ModelNativeDB {
struct_name: StructName,
Expand Down Expand Up @@ -116,6 +116,21 @@ impl ModelNativeDB {
Ident::new(&format!("{}Key", struct_name), Span::call_site().into())
}

pub(crate) fn keys_enum_visibility(&self) -> proc_macro2::TokenStream {
let do_export = match &self.attrs.do_export_keys {
Some(do_export_keys) => do_export_keys.value,
None => false,
};

let visibility = if do_export {
""
} else {
"(crate)"
};

format!("pub{}", visibility).parse().unwrap()
}

pub(crate) fn secondary_keys_enum(&self) -> Vec<proc_macro2::TokenStream> {
self.attrs
.secondary_keys
Expand Down
4 changes: 3 additions & 1 deletion native_db_macro/src/native_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn native_db(args: TokenStream, input: TokenStream) -> TokenStream {
struct_name: struct_name.clone(),
primary_key: None,
secondary_keys: Default::default(),
do_export_keys: None,
};
let model_attributes_parser = syn::meta::parser(|meta| attrs.parse(meta));
parse_macro_input!(args with model_attributes_parser);
Expand All @@ -33,6 +34,7 @@ pub fn native_db(args: TokenStream, input: TokenStream) -> TokenStream {
let native_db_gks = model_native_db.native_db_secondary_key();
let native_db_model = model_native_db.native_db_model();

let keys_enum_visibility = model_native_db.keys_enum_visibility();
let keys_enum_name = model_native_db.keys_enum_name();
let keys_enum = model_native_db.secondary_keys_enum();
let keys_enum_database_key = model_native_db.keys_enum_database_key();
Expand All @@ -56,7 +58,7 @@ pub fn native_db(args: TokenStream, input: TokenStream) -> TokenStream {
#native_db_gks
}

pub(crate) enum #keys_enum_name {
#keys_enum_visibility enum #keys_enum_name {
#(#keys_enum),*
}

Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ doc_comment! {
include_str!("../README.md")
}

/// Macro which link [`native_model`](https://crates.io/crates/native_model) to the Native DB. See [`Builder.define`](struct.Builder.html#method.define) for more information.
/// Macro which link [`native_model`](https://crates.io/crates/native_model) to the Native DB. See
/// [`Builder.define`](struct.Builder.html#method.define) for more information.
///
/// Optional parameter `export_keys` controls whether the model keys enum is visible outside of the
/// crate, default value is false with visibility limited to `pub(crate)`. Use
/// `#[native_db(export_keys = true)]` to make enum visible outside of crate (ie. `pub`).
pub use native_db_macro::*;
pub use serialization::*;

0 comments on commit 1e19533

Please sign in to comment.