-
Notifications
You must be signed in to change notification settings - Fork 933
config_type: add unstable_variant attribute #5379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
//! This module provides utilities for handling attributes on variants | ||
//! of `config_type` enum. Currently there are two types of attributes | ||
//! that could appear on the variants of `config_type` enum: `doc_hint` | ||
//! and `value`. Both comes in the form of name-value pair whose value | ||
//! is string literal. | ||
//! of `config_type` enum. Currently there are the following attributes | ||
//! that could appear on the variants of `config_type` enum: | ||
//! | ||
//! - `doc_hint`: name-value pair whose value is string literal | ||
//! - `value`: name-value pair whose value is string literal | ||
//! - `unstable_variant`: name only | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An idea for a follow up PR: I wonder if we could optionally include the tracking issue number in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this idea, but agree it should be in followup |
||
|
||
/// Returns the value of the first `doc_hint` attribute in the given slice or | ||
/// `None` if `doc_hint` attribute is not available. | ||
|
@@ -27,6 +29,11 @@ pub fn find_config_value(attrs: &[syn::Attribute]) -> Option<String> { | |
attrs.iter().filter_map(config_value).next() | ||
} | ||
|
||
/// Returns `true` if the there is at least one `unstable` attribute in the given slice. | ||
pub fn any_unstable_variant(attrs: &[syn::Attribute]) -> bool { | ||
attrs.iter().any(is_unstable_variant) | ||
} | ||
|
||
/// Returns a string literal value if the given attribute is `value` | ||
/// attribute or `None` otherwise. | ||
pub fn config_value(attr: &syn::Attribute) -> Option<String> { | ||
|
@@ -38,13 +45,25 @@ pub fn is_config_value(attr: &syn::Attribute) -> bool { | |
is_attr_name_value(attr, "value") | ||
} | ||
|
||
/// Returns `true` if the given attribute is an `unstable` attribute. | ||
pub fn is_unstable_variant(attr: &syn::Attribute) -> bool { | ||
is_attr_path(attr, "unstable_variant") | ||
} | ||
|
||
fn is_attr_name_value(attr: &syn::Attribute, name: &str) -> bool { | ||
attr.parse_meta().ok().map_or(false, |meta| match meta { | ||
syn::Meta::NameValue(syn::MetaNameValue { ref path, .. }) if path.is_ident(name) => true, | ||
_ => false, | ||
}) | ||
} | ||
|
||
fn is_attr_path(attr: &syn::Attribute, name: &str) -> bool { | ||
attr.parse_meta().ok().map_or(false, |meta| match meta { | ||
syn::Meta::Path(path) if path.is_ident(name) => true, | ||
_ => false, | ||
}) | ||
} | ||
|
||
fn get_name_value_str_lit(attr: &syn::Attribute, name: &str) -> Option<String> { | ||
attr.parse_meta().ok().and_then(|meta| match meta { | ||
syn::Meta::NameValue(syn::MetaNameValue { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
pub mod config { | ||
pub trait ConfigType: Sized { | ||
fn doc_hint() -> String; | ||
fn stable_variant(&self) -> bool; | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure this needs a version bump, however I don't know if this has knock on impacts anywhere else? AFAIK this is an internal only crate that we can break without fear?