A library for formatting error messages with spans
This library is intended to be used outside of proc macros.
From a file in a string
```rust use spannederrormessage::{SpannedErrorMessage, Section};
// 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 procmacro. let section = Section::fromsearch("String::from", file).unwrap();
// Create the message let message = SpannedErrorMessage::new() .create(§ion);
eprintln!("{}", message); // | // 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.
```rust use spannederrormessage::{SpannedErrorMessage, Section};
// Replace this with your own span let span = procmacro2::Span::callsite();
// Point the span to a file let section = Section::fromspanand_path(&span, "src/lib.rs").unwrap();
// Create a message let message = SpannedErrorMessage::new() .create(§ion); ```
With a label, title and reference to a file
```rust use spannederrormessage::{SpannedErrorMessage, Section};
// 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 procmacro. let mut section = Section::fromsearch("String::from", file).unwrap() .label("the problem is here");
// Set the file path. This happens automatically if you load from a file instead of a string. section.document.path = Some("src/lib.rs".into());
let message = SpannedErrorMessage::new() .title("your code is broken") .create(§ion);
eprintln!("{}", message); // 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.