Skip to content

Commit 15b8bfd

Browse files
committed
manifest: Improve "wrong separator" error messages
Signed-off-by: Lann Martin <[email protected]>
1 parent 72e2c96 commit 15b8bfd

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

crates/manifest/tests/ui/invalid/bad_component_id.err

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ TOML parse error at line 6, column 12
22
|
33
6 | [component.bad_id]
44
| ^^^^^^
5-
'-'-separated words may only contain alphanumeric ASCII; got '_'
5+
words must be separated with '-', not '_'

crates/serde/src/id.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ impl<const DELIM: char> TryFrom<String> for Id<DELIM> {
3333
if id.is_empty() {
3434
return Err("empty".into());
3535
}
36+
// Special-case common "wrong separator" errors
37+
if let Some(wrong) = wrong_delim::<DELIM>() {
38+
if id.contains(wrong) {
39+
return Err(format!(
40+
"words must be separated with {DELIM:?}, not {wrong:?}"
41+
));
42+
}
43+
}
3644
for word in id.split(DELIM) {
3745
if word.is_empty() {
3846
return Err(format!("{DELIM:?}-separated words must not be empty"));
@@ -59,3 +67,11 @@ impl<const DELIM: char> TryFrom<String> for Id<DELIM> {
5967
Ok(Self(id))
6068
}
6169
}
70+
71+
const fn wrong_delim<const DELIM: char>() -> Option<char> {
72+
match DELIM {
73+
'_' => Some('-'),
74+
'-' => Some('_'),
75+
_ => None,
76+
}
77+
}

0 commit comments

Comments
 (0)