summaryrefslogtreecommitdiffstats
path: root/src/tools/rustfmt/config_proc_macro/src/attrs.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/tools/rustfmt/config_proc_macro/src/attrs.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/rustfmt/config_proc_macro/src/attrs.rs')
-rw-r--r--src/tools/rustfmt/config_proc_macro/src/attrs.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/tools/rustfmt/config_proc_macro/src/attrs.rs b/src/tools/rustfmt/config_proc_macro/src/attrs.rs
new file mode 100644
index 000000000..0baba046f
--- /dev/null
+++ b/src/tools/rustfmt/config_proc_macro/src/attrs.rs
@@ -0,0 +1,57 @@
+//! 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.
+
+/// Returns the value of the first `doc_hint` attribute in the given slice or
+/// `None` if `doc_hint` attribute is not available.
+pub fn find_doc_hint(attrs: &[syn::Attribute]) -> Option<String> {
+ attrs.iter().filter_map(doc_hint).next()
+}
+
+/// Returns `true` if the given attribute is a `doc_hint` attribute.
+pub fn is_doc_hint(attr: &syn::Attribute) -> bool {
+ is_attr_name_value(attr, "doc_hint")
+}
+
+/// Returns a string literal value if the given attribute is `doc_hint`
+/// attribute or `None` otherwise.
+pub fn doc_hint(attr: &syn::Attribute) -> Option<String> {
+ get_name_value_str_lit(attr, "doc_hint")
+}
+
+/// Returns the value of the first `value` attribute in the given slice or
+/// `None` if `value` attribute is not available.
+pub fn find_config_value(attrs: &[syn::Attribute]) -> Option<String> {
+ attrs.iter().filter_map(config_value).next()
+}
+
+/// Returns a string literal value if the given attribute is `value`
+/// attribute or `None` otherwise.
+pub fn config_value(attr: &syn::Attribute) -> Option<String> {
+ get_name_value_str_lit(attr, "value")
+}
+
+/// Returns `true` if the given attribute is a `value` attribute.
+pub fn is_config_value(attr: &syn::Attribute) -> bool {
+ is_attr_name_value(attr, "value")
+}
+
+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 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 {
+ ref path,
+ lit: syn::Lit::Str(ref lit_str),
+ ..
+ }) if path.is_ident(name) => Some(lit_str.value()),
+ _ => None,
+ })
+}