| Crates.io | codespan-derive |
| lib.rs | codespan-derive |
| version | 0.1.1 |
| created_at | 2021-08-25 05:54:20.420596+00 |
| updated_at | 2021-09-04 06:25:36.600222+00 |
| description | derive(IntoDiagnostic) for easy codespan integration |
| homepage | |
| repository | https://github.com/compiler-errors/codespan-derive |
| max_upload_size | |
| id | 441995 |
| size | 3,997 |
Derive macro for ergonomically creating a Diagnostic from an error macro
#[derive(IntoDiagnostic)] onto your error macro type.#[file_id(Type)] to signal what the FileId generic type of the Diagnostic will be.#[message = ""] signalling what the error message should read.IntoLabel can be tagged with #[primary] or #[secondary] to be marked in the generated error, with an optional message like #[primary = ""].#[derive(IntoDiagnostic)]
#[file_id(SomeFileIdType)]
enum Error {
#[message = "Compiler found the number `{0}` is too large"]
NumberTooLarge(usize),
#[message = "Cannot parse string {string}"]
BadString {
string: String,
#[primary = "The bad string appears here"]
span: Span,
},
}
Then handle it somewhere like:
if let Some(err) = result {
// IntoDiagnostic derived from macro
let diagnostic = err.into_diagnostic();
// Basic codespan-diagnostic printing to terminal
let writer = StandardStream::stderr(ColorChoice::Always);
let config = codespan_reporting::term::Config::default();
term::emit(&mut writer.lock(), &config, &files, &diagnostic)?;
}