Skip to content

Commit

Permalink
Fix issue #3: Make generated registries pub(crate) by default, and the
Browse files Browse the repository at this point in the history
fields public. The visibility can be configured using the `visibility`
option of the `metered` attribute, where the value is the desired
visibility tokens, e.g: `pub(self)`, `pub(module)` or `` for inherited
visibility (~private).
  • Loading branch information
magnet committed Feb 12, 2019
1 parent a60db99 commit 48c83e3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion demos/demo-nightly/src/baz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Baz {
metric_reg: BazMetricRegistry,
}

#[metered(registry = BazMetricRegistry, /* default = self.metrics */ registry_expr = self.metric_reg)]
#[metered(registry = BazMetricRegistry, /* default = self.metrics */ registry_expr = self.metric_reg, visibility = pub(self))]
#[measure(InFlight)] // Applies to all methods that have the `measure` attribute
impl Baz {
// This is measured with an InFlight gauge, because it's the default on the block.
Expand Down
2 changes: 1 addition & 1 deletion demos/demo-stable/src/baz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Baz {
metric_reg: BazMetricRegistry,
}

#[metered(registry = BazMetricRegistry, /* default = self.metrics */ registry_expr = self.metric_reg)]
#[metered(registry = BazMetricRegistry, /* default = self.metrics */ registry_expr = self.metric_reg, visibility = pub(self))]
#[measure(InFlight)] // Applies to all methods that have the `measure` attribute
impl Baz {
// This is measured with an InFlight gauge, because it's the default on the block.
Expand Down
10 changes: 6 additions & 4 deletions metered-macro/src/metered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,28 @@ pub fn metered(attrs: TokenStream, item: TokenStream) -> syn::Result<TokenStream
let measured = &woven_impl_block.woven_fns;
let registry_name = &metered.registry_name;
let registry_ident = &metered.registry_ident;
let visibility = &metered.visibility;

let mut code = quote! {};

let mut reg_fields = quote! {};

for (fun_name, _) in measured.iter() {
use heck::CamelCase;
let fun_reg_name = format!("{}{}", registry_name, fun_name.to_string().to_camel_case());
let fun_registry_ident = syn::Ident::new(&fun_reg_name, impl_block.impl_token.span);

reg_fields = quote! {
#reg_fields
#fun_name : #fun_registry_ident,
pub #fun_name : #fun_registry_ident,
}
}

code = quote! {
#code

#[derive(Debug, Default, serde::Serialize)]
struct #registry_ident {
#visibility struct #registry_ident {
#reg_fields
}
};
Expand All @@ -59,7 +61,7 @@ pub fn metered(attrs: TokenStream, item: TokenStream) -> syn::Result<TokenStream

fun_reg_fields = quote! {
#fun_reg_fields
#metric_field : #metric_type,
pub #metric_field : #metric_type,
}
}
}
Expand All @@ -68,7 +70,7 @@ pub fn metered(attrs: TokenStream, item: TokenStream) -> syn::Result<TokenStream
#code

#[derive(Debug, Default, serde::Serialize)]
struct #fun_registry_ident {
#visibility struct #fun_registry_ident {
#fun_reg_fields
}
};
Expand Down
24 changes: 24 additions & 0 deletions metered-macro/src/metered_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct Metered<'a> {
pub registry_ident: &'a syn::Ident,
pub registry_name: String,
pub registry_expr: Cow<'a, syn::Expr>,
pub visibility: Cow<'a, syn::Visibility>,
}

pub struct MeteredKeyValAttribute {
Expand Down Expand Up @@ -83,10 +84,26 @@ impl MeteredKeyValAttribute {
.map(|id| Cow::Borrowed(id))
.unwrap_or_else(|| Cow::Owned(syn::parse_str::<syn::Expr>("self.metrics").unwrap()));

let visibility = self
.values
.iter()
.filter_map(|opt| {
if let MeteredOption::Visibility(tpe) = opt {
Some(&tpe.value)
} else {
None
}
})
.next()
.map(|id| Cow::Borrowed(id))
.unwrap_or_else(|| {
Cow::Owned(syn::parse_str::<syn::Visibility>("pub(crate)").unwrap())
});
Metered {
registry_ident,
registry_name,
registry_expr,
visibility,
}
}
}
Expand All @@ -106,15 +123,19 @@ impl Parse for MeteredKeyValAttribute {
mod kw {
syn::custom_keyword!(registry);
syn::custom_keyword!(registry_expr);
syn::custom_keyword!(visibility);
}

pub type MeteredRegistryOption = KVOption<kw::registry, syn::Ident>;

pub type MeteredRegistryExprOption = KVOption<kw::registry_expr, syn::Expr>;

pub type MeteredVisibilityOption = KVOption<kw::visibility, syn::Visibility>;

pub enum MeteredOption {
Registry(MeteredRegistryOption),
RegistryExpr(MeteredRegistryExprOption),
Visibility(MeteredVisibilityOption),
}

impl MeteredOption {
Expand All @@ -123,6 +144,7 @@ impl MeteredOption {
match self {
MeteredOption::Registry(_) => <kw::registry>::display(),
MeteredOption::RegistryExpr(_) => <kw::registry_expr>::display(),
MeteredOption::Visibility(_) => <kw::visibility>::display(),
}
}
}
Expand All @@ -133,6 +155,8 @@ impl Parse for MeteredOption {
Ok(input.parse_as(MeteredOption::Registry)?)
} else if MeteredRegistryExprOption::peek(input) {
Ok(input.parse_as(MeteredOption::RegistryExpr)?)
} else if MeteredVisibilityOption::peek(input) {
Ok(input.parse_as(MeteredOption::Visibility)?)
} else {
let err = format!("invalid metered option: {}", input);
Err(input.error(err))
Expand Down

0 comments on commit 48c83e3

Please sign in to comment.