Skip to content

Commit

Permalink
fix(easy): Escape tokens in errors
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 12, 2022
1 parent 84ceb76 commit 7d90700
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/stream/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,41 @@ impl<T: PartialEq, R: PartialEq> PartialEq for Info<T, R> {
impl<T: fmt::Display, R: fmt::Display> fmt::Display for Info<T, R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Info::Token(ref c) => write!(f, "`{}`", c),
Info::Range(ref c) => write!(f, "`{}`", c),
Info::Token(ref c) => escaped_token(c, '`', f),
Info::Range(ref c) => escaped_token(c, '`', f),
Info::Owned(ref s) => write!(f, "{}", s),
Info::Static(s) => write!(f, "{}", s),
}
}
}

fn escaped_token<T: fmt::Display>(
token: &T,
quote: char,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
let token = token.to_string();
write!(f, "{}", quote)?;
for c in token.chars() {
match c {
'\t' | '\n' | '\r' => {
write!(f, "{}", c.escape_debug())?;
}
c if c.is_ascii_control() => {
write!(f, "{}", c.escape_debug())?;
}
c if c == quote => {
write!(f, "\\{}", quote)?;
}
_ => {
write!(f, "{}", c)?;
}
}
}
write!(f, "{}", quote)?;
Ok(())
}

impl<R> From<char> for Info<char, R> {
fn from(s: char) -> Info<char, R> {
Info::Token(s)
Expand Down

0 comments on commit 7d90700

Please sign in to comment.