Skip to content

Commit 558be37

Browse files
committed
yeast: Fix escaping bug in yeast-macros
Happily, it turned out that there was already a library function for handling this case.
1 parent d1711f5 commit 558be37

1 file changed

Lines changed: 5 additions & 32 deletions

File tree

shared/yeast-macros/src/parse.rs

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,39 +1183,12 @@ fn parse_named_string_arg(tokens: &mut Tokens, expected_name: &str) -> Result<Na
11831183
Ok(NamedString { value, span })
11841184
}
11851185

1186-
/// Read a literal as a plain Rust string, stripping the surrounding quotes
1187-
/// and unescaping. Falls back to `None` if the literal isn't a string.
1186+
/// Read a literal as a plain Rust string, respecting Rust's own escape
1187+
/// rules (via `syn::LitStr`). Falls back to `None` if the literal
1188+
/// isn't a string.
11881189
fn string_literal_value(lit: &Literal) -> Option<String> {
1189-
let raw = lit.to_string();
1190-
let bytes = raw.as_bytes();
1191-
// Match plain `"..."` literals; reject byte strings, raw strings (for
1192-
// simplicity), char literals, numbers, etc.
1193-
if bytes.first() != Some(&b'"') || bytes.last() != Some(&b'"') {
1194-
return None;
1195-
}
1196-
let mut out = String::with_capacity(raw.len());
1197-
let mut chars = raw[1..raw.len() - 1].chars();
1198-
while let Some(c) = chars.next() {
1199-
if c != '\\' {
1200-
out.push(c);
1201-
continue;
1202-
}
1203-
match chars.next()? {
1204-
'n' => out.push('\n'),
1205-
't' => out.push('\t'),
1206-
'r' => out.push('\r'),
1207-
'\\' => out.push('\\'),
1208-
'\'' => out.push('\''),
1209-
'"' => out.push('"'),
1210-
'0' => out.push('\0'),
1211-
other => {
1212-
// Unknown escape — give up rather than silently mis-parse.
1213-
out.push('\\');
1214-
out.push(other);
1215-
}
1216-
}
1217-
}
1218-
Some(out)
1190+
let tokens = TokenStream::from(TokenTree::Literal(lit.clone()));
1191+
syn::parse2::<syn::LitStr>(tokens).ok().map(|s| s.value())
12191192
}
12201193

12211194
/// Split a token stream into top-level comma-separated items. Commas inside

0 commit comments

Comments
 (0)