From e6cf74129396de160f6676d19c73204b91eb78f8 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 18 Mar 2023 13:46:44 -0700 Subject: [PATCH] Move parse_args_with error handler into parsing module --- src/attr.rs | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/attr.rs b/src/attr.rs index eafb0e616c..7e8baf8f4b 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -239,7 +239,7 @@ impl Attribute { pub fn parse_args_with(&self, parser: F) -> Result { match &self.meta { Meta::Path(path) => { - let expected = expected_parentheses(&self.style, path); + let expected = parsing::expected_parentheses(&self.style, path); let msg = format!("expected attribute arguments in parentheses: {}", expected); Err(crate::error::new2( path.segments.first().unwrap().ident.span(), @@ -248,7 +248,7 @@ impl Attribute { )) } Meta::NameValue(meta) => { - let expected = expected_parentheses(&self.style, &meta.path); + let expected = parsing::expected_parentheses(&self.style, &meta.path); let msg = format!("expected parentheses: {}", expected); Err(Error::new(meta.eq_token.span, msg)) } @@ -417,26 +417,6 @@ impl Attribute { } } -#[cfg(feature = "parsing")] -fn expected_parentheses(style: &AttrStyle, path: &Path) -> String { - let mut suggestion = String::new(); - match style { - AttrStyle::Outer => suggestion.push('#'), - AttrStyle::Inner(_) => suggestion.push_str("#!"), - } - suggestion.push('['); - - for (i, segment) in path.segments.iter().enumerate() { - if i > 0 || path.leading_colon.is_some() { - suggestion.push_str("::"); - } - write!(suggestion, "{}", segment.ident).unwrap(); - } - - suggestion.push_str("(...)]"); - suggestion -} - ast_enum! { /// Distinguishes between attributes that decorate an item and attributes /// that are contained within an item. @@ -681,6 +661,25 @@ pub(crate) mod parsing { value, }) } + + pub(super) fn expected_parentheses(style: &AttrStyle, path: &Path) -> String { + let mut suggestion = String::new(); + match style { + AttrStyle::Outer => suggestion.push('#'), + AttrStyle::Inner(_) => suggestion.push_str("#!"), + } + suggestion.push('['); + + for (i, segment) in path.segments.iter().enumerate() { + if i > 0 || path.leading_colon.is_some() { + suggestion.push_str("::"); + } + write!(suggestion, "{}", segment.ident).unwrap(); + } + + suggestion.push_str("(...)]"); + suggestion + } } #[cfg(feature = "printing")]