Skip to content

Commit

Permalink
Move parse_args_with error handler into parsing module
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Mar 18, 2023
1 parent 906fa56 commit e6cf741
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions src/attr.rs
Expand Up @@ -239,7 +239,7 @@ impl Attribute {
pub fn parse_args_with<F: Parser>(&self, parser: F) -> Result<F::Output> {
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(),
Expand All @@ -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))
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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")]
Expand Down

0 comments on commit e6cf741

Please sign in to comment.