]> git.lizzy.rs Git - rust.git/blob - src/skip.rs
Update rustc-ap-* crates to 581.0.0 (#3783)
[rust.git] / src / skip.rs
1 //! Module that contains skip related stuffs.
2
3 use syntax::ast;
4
5 /// Take care of skip name stack. You can update it by attributes slice or
6 /// by other context. Query this context to know if you need skip a block.
7 #[derive(Default, Clone)]
8 pub(crate) struct SkipContext {
9     macros: Vec<String>,
10     attributes: Vec<String>,
11 }
12
13 impl SkipContext {
14     pub(crate) fn update_with_attrs(&mut self, attrs: &[ast::Attribute]) {
15         self.macros.append(&mut get_skip_names("macros", attrs));
16         self.attributes
17             .append(&mut get_skip_names("attributes", attrs));
18     }
19
20     pub(crate) fn update(&mut self, mut other: SkipContext) {
21         self.macros.append(&mut other.macros);
22         self.attributes.append(&mut other.attributes);
23     }
24
25     pub(crate) fn skip_macro(&self, name: &str) -> bool {
26         self.macros.iter().any(|n| n == name)
27     }
28
29     pub(crate) fn skip_attribute(&self, name: &str) -> bool {
30         self.attributes.iter().any(|n| n == name)
31     }
32 }
33
34 static RUSTFMT: &'static str = "rustfmt";
35 static SKIP: &'static str = "skip";
36
37 /// Say if you're playing with `rustfmt`'s skip attribute
38 pub(crate) fn is_skip_attr(segments: &[ast::PathSegment]) -> bool {
39     if segments.len() < 2 || segments[0].ident.to_string() != RUSTFMT {
40         return false;
41     }
42     match segments.len() {
43         2 => segments[1].ident.to_string() == SKIP,
44         3 => {
45             segments[1].ident.to_string() == SKIP
46                 && ["macros", "attributes"]
47                     .iter()
48                     .any(|&n| n == &segments[2].ident.name.as_str())
49         }
50         _ => false,
51     }
52 }
53
54 fn get_skip_names(kind: &str, attrs: &[ast::Attribute]) -> Vec<String> {
55     let mut skip_names = vec![];
56     let path = format!("{}::{}::{}", RUSTFMT, SKIP, kind);
57     for attr in attrs {
58         // syntax::ast::Path is implemented partialEq
59         // but it is designed for segments.len() == 1
60         if format!("{}", attr.path) != path {
61             continue;
62         }
63
64         if let Some(list) = attr.meta_item_list() {
65             for nested_meta_item in list {
66                 if let Some(name) = nested_meta_item.ident() {
67                     skip_names.push(name.to_string());
68                 }
69             }
70         }
71     }
72     skip_names
73 }