-
Notifications
You must be signed in to change notification settings - Fork 287
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
Add: Support #[deprecated] attribute #860
Changes from 15 commits
f86d749
87561a4
e001222
d75abe9
19845d8
608a31e
ff164fb
6c29ff7
5733bf8
b5ae00e
a9587bb
fbe51c6
6bfb3ff
31fff88
a439a3a
bb39e3a
1a3f566
81bf4ca
50b6171
05d7035
19db2a4
d13bb58
5b119db
507eaa5
5809afc
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,13 +35,15 @@ pub enum AnnotationValue { | |
pub struct AnnotationSet { | ||
annotations: HashMap<String, AnnotationValue>, | ||
pub must_use: bool, | ||
pub deprecated: Option<String>, | ||
} | ||
|
||
impl AnnotationSet { | ||
pub fn new() -> AnnotationSet { | ||
AnnotationSet { | ||
annotations: HashMap::new(), | ||
must_use: false, | ||
deprecated: None, | ||
} | ||
} | ||
|
||
|
@@ -53,6 +55,10 @@ impl AnnotationSet { | |
self.must_use && config.language != Language::Cython | ||
} | ||
|
||
pub(crate) fn deprecated(&self, config: &Config) -> bool { | ||
sevenc-nanashi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.deprecated.is_some() && config.language != Language::Cython | ||
} | ||
|
||
pub fn load(attrs: &[syn::Attribute]) -> Result<AnnotationSet, String> { | ||
let lines = attrs.get_comment_lines(); | ||
let lines: Vec<&str> = lines | ||
|
@@ -68,6 +74,36 @@ impl AnnotationSet { | |
.collect(); | ||
|
||
let must_use = attrs.has_attr_word("must_use"); | ||
let deprecated = if let Some(note) = attrs.attr_name_value_lookup("deprecated") { | ||
sevenc-nanashi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Some(note) | ||
} else if attrs.has_attr_word("deprecated") { | ||
Some("".to_string()) | ||
} else if let Some(attr) = attrs.iter().find(|attr| { | ||
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. Is this code-path tested? 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. It should be. |
||
if let Ok(syn::Meta::List(list)) = attr.parse_meta() { | ||
list.path.is_ident("deprecated") | ||
} else { | ||
false | ||
} | ||
}) { | ||
let args: syn::punctuated::Punctuated<syn::MetaNameValue, Token![,]> = attr | ||
.parse_args_with(syn::punctuated::Punctuated::parse_terminated) | ||
.map_err(|e| format!("Couldn't parse deprecated attribute: {}", e.to_string()))?; | ||
let Some(lit) = args | ||
.iter() | ||
.find(|arg| arg.path.is_ident("note")) | ||
.map(|arg| &arg.lit) | ||
else { | ||
return Err("Couldn't parse deprecated attribute: no `note` field".to_string()); | ||
}; | ||
|
||
if let syn::Lit::Str(lit) = lit { | ||
Some(lit.value()) | ||
} else { | ||
return Err("deprecated attribute must be a string".to_string()); | ||
} | ||
} else { | ||
None | ||
}; | ||
|
||
let mut annotations = HashMap::new(); | ||
|
||
|
@@ -118,6 +154,7 @@ impl AnnotationSet { | |
Ok(AnnotationSet { | ||
annotations, | ||
must_use, | ||
deprecated, | ||
}) | ||
} | ||
|
||
|
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.
ref: #859