Skip to content

Commit 1e19533

Browse files
committed
Add export_keys optional param to native_db macro
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.
1 parent 50063a0 commit 1e19533

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

native_db_macro/src/model_attributes.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use crate::keys::{KeyDefinition, KeyOptions};
22
use crate::struct_name::StructName;
3-
use proc_macro::Ident;
43
use std::collections::HashSet;
54
use syn::meta::ParseNestedMeta;
65
use syn::parse::Result;
7-
use syn::Field;
6+
use syn::{Field, LitBool};
87

98
#[derive(Clone)]
109
pub(crate) struct ModelAttributes {
1110
pub(crate) struct_name: StructName,
1211
pub(crate) primary_key: Option<KeyDefinition<()>>,
1312
pub(crate) secondary_keys: HashSet<KeyDefinition<KeyOptions>>,
13+
pub(crate) do_export_keys: Option<LitBool>,
1414
}
1515

1616
impl ModelAttributes {
@@ -60,6 +60,8 @@ impl ModelAttributes {
6060
Ok(())
6161
})?;
6262
self.secondary_keys.insert(key);
63+
} else if meta.path.is_ident("export_keys") {
64+
self.do_export_keys = Some(meta.value()?.parse()?);
6365
} else {
6466
panic!(
6567
"Unknown attribute: {}",

native_db_macro/src/model_native_db.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::struct_name::StructName;
33
use crate::ToTokenStream;
44
use proc_macro::Span;
55
use quote::quote;
6-
use syn::Ident;
6+
use syn::{Ident};
77

88
pub(crate) struct ModelNativeDB {
99
struct_name: StructName,
@@ -116,6 +116,21 @@ impl ModelNativeDB {
116116
Ident::new(&format!("{}Key", struct_name), Span::call_site().into())
117117
}
118118

119+
pub(crate) fn keys_enum_visibility(&self) -> proc_macro2::TokenStream {
120+
let do_export = match &self.attrs.do_export_keys {
121+
Some(do_export_keys) => do_export_keys.value,
122+
None => false,
123+
};
124+
125+
let visibility = if do_export {
126+
""
127+
} else {
128+
"(crate)"
129+
};
130+
131+
format!("pub{}", visibility).parse().unwrap()
132+
}
133+
119134
pub(crate) fn secondary_keys_enum(&self) -> Vec<proc_macro2::TokenStream> {
120135
self.attrs
121136
.secondary_keys

native_db_macro/src/native_db.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub fn native_db(args: TokenStream, input: TokenStream) -> TokenStream {
1313
struct_name: struct_name.clone(),
1414
primary_key: None,
1515
secondary_keys: Default::default(),
16+
do_export_keys: None,
1617
};
1718
let model_attributes_parser = syn::meta::parser(|meta| attrs.parse(meta));
1819
parse_macro_input!(args with model_attributes_parser);
@@ -33,6 +34,7 @@ pub fn native_db(args: TokenStream, input: TokenStream) -> TokenStream {
3334
let native_db_gks = model_native_db.native_db_secondary_key();
3435
let native_db_model = model_native_db.native_db_model();
3536

37+
let keys_enum_visibility = model_native_db.keys_enum_visibility();
3638
let keys_enum_name = model_native_db.keys_enum_name();
3739
let keys_enum = model_native_db.secondary_keys_enum();
3840
let keys_enum_database_key = model_native_db.keys_enum_database_key();
@@ -56,7 +58,7 @@ pub fn native_db(args: TokenStream, input: TokenStream) -> TokenStream {
5658
#native_db_gks
5759
}
5860

59-
pub(crate) enum #keys_enum_name {
61+
#keys_enum_visibility enum #keys_enum_name {
6062
#(#keys_enum),*
6163
}
6264

src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,11 @@ doc_comment! {
386386
include_str!("../README.md")
387387
}
388388

389-
/// 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.
389+
/// Macro which link [`native_model`](https://crates.io/crates/native_model) to the Native DB. See
390+
/// [`Builder.define`](struct.Builder.html#method.define) for more information.
391+
///
392+
/// Optional parameter `export_keys` controls whether the model keys enum is visible outside of the
393+
/// crate, default value is false with visibility limited to `pub(crate)`. Use
394+
/// `#[native_db(export_keys = true)]` to make enum visible outside of crate (ie. `pub`).
390395
pub use native_db_macro::*;
391396
pub use serialization::*;

0 commit comments

Comments
 (0)