Skip to content

Instantly share code, notes, and snippets.

@softprops
Created September 21, 2019 10:30
Show Gist options
  • Select an option

  • Save softprops/714fa5ca9732ad6bfe1570838e7ddafb to your computer and use it in GitHub Desktop.

Select an option

Save softprops/714fa5ca9732ad6bfe1570838e7ddafb to your computer and use it in GitHub Desktop.
struct Position { line: usize, col: usize }
fn report<P>(path: P, content: &str, pos: &Position, error: &str)
where
P: AsRef<Path>,
{
let Position { line, col } = pos;
println!(
"{}",
format!("--> {}:{}:{}", path.as_ref().display(), line, col).dimmed()
);
for (idx, matched) in content.lines().enumerate().filter(|(idx, _)| (line-2..=*line).contains(idx)) {
println!("{}{}", format!("{} |", idx).dimmed(), matched)
}
println!(
"{}{}{}",
format!("{}|", " ".repeat(line.to_string().len() + 1)).dimmed(),
" ".repeat(*col),
format!("^ error: {}", error).red()
);
}
@softprops
Copy link
Copy Markdown
Author

--> ../tests/data/example.json:12:6
10 |    {
11 |      "foo":"bye",
12 |      "bar":false
   |      ^ error: does not look good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment