Skip to content

Commit

Permalink
chore: add the string that failed validation to `IdentError::InvalidC…
Browse files Browse the repository at this point in the history
…hars` (#1555)
  • Loading branch information
greenhat authored Nov 4, 2024
1 parent ce26595 commit 60a5f37
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions assembly/src/ast/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::{SourceSpan, Span, Spanned};
pub enum IdentError {
#[error("invalid identifier: cannot be empty")]
Empty,
#[error("invalid identifier: must contain only lowercase, ascii alphanumeric characters, or underscores")]
InvalidChars,
#[error("invalid identifier '{ident}': must contain only lowercase, ascii alphanumeric characters, or underscores")]
InvalidChars { ident: Arc<str> },
#[error("invalid identifier: must start with lowercase ascii alphabetic character")]
InvalidStart,
#[error("invalid identifier: length exceeds the maximum of {max} bytes")]
Expand Down Expand Up @@ -109,7 +109,7 @@ impl Ident {
return Err(IdentError::InvalidStart);
}
if !source.chars().all(|c| c.is_ascii_alphabetic() || matches!(c, '_' | '0'..='9')) {
return Err(IdentError::InvalidChars);
return Err(IdentError::InvalidChars { ident: source.into() });
}
Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions assembly/src/ast/procedure/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,17 @@ impl FromStr for ProcedureName {
match c {
'"' => {
if chars.next().is_some() {
break Err(IdentError::InvalidChars);
break Err(IdentError::InvalidChars { ident: s.into() });
}
let tok = &s[1..pos];
break Ok(Arc::from(tok.to_string().into_boxed_str()));
},
c if c.is_alphanumeric() => continue,
'_' | '$' | '-' | '!' | '?' | '<' | '>' | ':' | '.' => continue,
_ => break Err(IdentError::InvalidChars),
_ => break Err(IdentError::InvalidChars { ident: s.into() }),
}
} else {
break Err(IdentError::InvalidChars);
break Err(IdentError::InvalidChars { ident: s.into() });
}
},
Some((_, c)) if c.is_ascii_lowercase() || c == '_' || c == '$' => {
Expand All @@ -317,13 +317,13 @@ impl FromStr for ProcedureName {
'_' | '$' => false,
_ => true,
}) {
Err(IdentError::InvalidChars)
Err(IdentError::InvalidChars { ident: s.into() })
} else {
Ok(Arc::from(s.to_string().into_boxed_str()))
}
},
Some((_, c)) if c.is_ascii_uppercase() => Err(IdentError::Casing(CaseKindError::Snake)),
Some(_) => Err(IdentError::InvalidChars),
Some(_) => Err(IdentError::InvalidChars { ident: s.into() }),
}?;
Ok(Self(Ident::new_unchecked(Span::unknown(raw))))
}
Expand Down
5 changes: 4 additions & 1 deletion assembly/src/library/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,10 @@ mod tests {
assert_matches!(path, Err(PathError::InvalidComponent(IdentError::InvalidStart)));

let path = LibraryPath::new("foo::b@r");
assert_matches!(path, Err(PathError::InvalidComponent(IdentError::InvalidChars)));
assert_matches!(
path,
Err(PathError::InvalidComponent(IdentError::InvalidChars { ident: _ }))
);

let path = LibraryPath::new("#foo::bar");
assert_matches!(
Expand Down

0 comments on commit 60a5f37

Please sign in to comment.