Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add the string that failed validation to IdentError::InvalidChars error variant #1555

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading