2 个版本
| 0.1.1 | 2021 年 9 月 4 日 |
|---|---|
| 0.1.0 | 2021 年 8 月 25 日 |
1643 在 Rust 模式
5KB
codespan-derive
用于从错误宏优雅地创建诊断的 derive 宏
使用方法
- 将
#[derive(IntoDiagnostic)]添加到您的错误宏类型上。 - 添加
#[file_id(Type)]以指示 Diagnostic 的 FileId 泛型类型。 - 为每个变体添加
#[message = ""]以指示错误消息应如何读取。 - 实现
IntoLabel的类似于 Span 的值可以标记为#[primary]或#[secondary]以在生成的错误中标记,可选的消息如#[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,
},
}
然后在某处处理它,如下所示
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)?;
}
依赖
~1.8–9.5MB
~82K SLoC