1 //! Module that contains skip related stuffs.
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 {
10 attributes: Vec<String>,
14 pub(crate) fn update_with_attrs(&mut self, attrs: &[ast::Attribute]) {
15 self.macros.append(&mut get_skip_names("macros", attrs));
17 .append(&mut get_skip_names("attributes", attrs));
20 pub(crate) fn update(&mut self, mut other: SkipContext) {
21 self.macros.append(&mut other.macros);
22 self.attributes.append(&mut other.attributes);
25 pub(crate) fn skip_macro(&self, name: &str) -> bool {
26 self.macros.iter().any(|n| n == name)
29 pub(crate) fn skip_attribute(&self, name: &str) -> bool {
30 self.attributes.iter().any(|n| n == name)
34 static RUSTFMT: &'static str = "rustfmt";
35 static SKIP: &'static str = "skip";
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 {
42 match segments.len() {
43 2 => segments[1].ident.to_string() == SKIP,
45 segments[1].ident.to_string() == SKIP
46 && ["macros", "attributes"]
48 .any(|&n| n == &segments[2].ident.name.as_str())
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);
58 // syntax::ast::Path is implemented partialEq
59 // but it is designed for segments.len() == 1
60 if format!("{}", attr.path) != path {
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());