spanned_error_message
A library for formatting error messages with spans
This library is intended to be used outside of proc macros.
Examples
From a file in a string
use ;
// A demo file loaded from a string
let file = r#"
pub fn foo(bar: u32) -> u32 {
return String::from(bar * 2)
}
"#;
// Section is equivalent to a Span in proc_macro.
let section = from_search.unwrap;
// Create the message
let message = new
.create;
eprintln!;
// |
// 3 | return String::from(bar * 2)
// | ^^^^^^^^^^^^
You can also convert a [proc_macro2::Span] into a section. Also you can load the content from a file.
use ;
// Replace this with your own span
let span = call_site;
// Point the span to a file
let section = from_span_and_path.unwrap;
// Create a message
let message = new
.create;
With a label, title and reference to a file
use ;
// A demo file loaded from a string
let file = r#"
pub fn foo(bar: u32) -> u32 {
return String::from(bar * 2)
}
"#;
// Section is equivalent to a Span in proc_macro.
let mut section = from_search.unwrap
.label;
// Set the file path. This happens automatically if you load from a file instead of a string.
section.document.path = Some;
let message = new
.title
.create;
eprintln!;
// error: your code is broken
// --> src/lib.rs:3:11
// |
// 3 | return String::from(bar * 2)
// | ^^^^^^^^^^^^ your code is broken
Currently there is only support for single line pointers. If you have a multiline span, it will only point to the first line.