4 个版本 (破坏性更新)
0.4.0 | 2021 年 4 月 25 日 |
---|---|
0.3.0 | 2021 年 4 月 20 日 |
0.2.0 | 2021 年 2 月 2 日 |
0.1.0 | 2020 年 11 月 20 日 |
#10 in #error-code
24KB
323 行
🪄 Srcerr
用于跟踪错误代码和详细信息的类型。
此库提供了一个 SourceError
结构体,用于保存
ErrorCode
: 枚举,其变体表示错误代码和简单描述。ErrorDetail
: 枚举,与ErrorCode
的变体相匹配,但每个变体都包含特定于错误实例的信息。Severity
: 报告错误的严重程度。
此库基于 codespan-reporting
渲染诊断错误。
还可以使用 "codespan"
功能来公开 codespan
类型
srcerr = { version = "0.4.0", features = ["codespan"] }
用法
1. 实现 ErrorCode
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SimpleErrorCode {
/// Error when a value is out of range.
ValueOutOfRange,
/// Error when a string is too long.
StringTooLong,
}
impl ErrorCode for SimpleErrorCode {
const ERROR_CODE_MAX: usize = 2;
const PREFIX: &'static str = "E";
fn code(self) -> usize {
match self {
Self::ValueOutOfRange => 1,
Self::StringTooLong => 2,
}
}
fn description(self) -> &'static str {
match self {
Self::ValueOutOfRange => "Value out of range.",
Self::StringTooLong => "String provided is too long.",
}
}
}
2. 实现 ErrorDetail
#[derive(Debug)]
pub enum SimpleErrorDetail {
/// Error when a value is out of range.
ValueOutOfRange {
/// ID of the file containing the invalid value.
file_id: usize,
/// The value.
value: i32,
/// Byte begin and end indices where the value is defined.
value_byte_indices: Range<usize>,
/// Range that the value must be within.
range: RangeInclusive<u32>,
},
/// Error when a string is too long.
StringTooLong {
/// ID of the file containing the invalid value.
file_id: usize,
/// The value that is too long.
value: String,
/// Byte begin and end indices where the value is defined.
value_byte_indices: Range<usize>,
/// Maximum length allowed for the string.
limit: usize,
},
}
impl<'files> ErrorDetail<'files> for SimpleErrorDetail {
type Files = SimpleFiles<&'files str, &'files str>;
fn labels(&self) -> Vec<Label<usize>> {
match self {
Self::ValueOutOfRange {
file_id,
value_byte_indices,
range,
..
} => {
vec![
Label::primary(*file_id, value_byte_indices.clone()).with_message(format!(
"not within the range: `{}..={}`",
range.start(),
range.end()
)),
]
}
Self::StringTooLong {
file_id,
value_byte_indices,
limit,
..
} => {
vec![
Label::primary(*file_id, value_byte_indices.clone())
.with_message(format!("exceeds the {} character limit.", limit)),
]
}
}
}
fn notes(&self, _files: &Self::Files) -> Vec<String> {
match self {
Self::ValueOutOfRange { range, .. } => {
let valid_exprs = range.clone().map(|n| Cow::Owned(n.to_string()));
let suggestion = Note::valid_exprs(valid_exprs).expect("Failed to format note.");
vec![suggestion]
}
Self::StringTooLong { .. } => vec![],
}
}
}
3. 当发生错误时构建 SourceError。
fn value_out_of_range<'f>(
file_id: usize,
) -> SourceError<'f, SimpleErrorCode, SimpleErrorDetail, SimpleFiles<&'f str, &'f str>> {
let error_code = SimpleErrorCode::ValueOutOfRange;
let error_detail = SimpleErrorDetail::ValueOutOfRange {
file_id,
value: -1,
value_byte_indices: 21..23,
range: 1..=3,
};
let severity = Severity::Error;
SourceError::new(error_code, error_detail, severity)
}
fn string_too_long<'f>(
file_id: usize,
content: &str,
) -> SourceError<'f, SimpleErrorCode, SimpleErrorDetail, SimpleFiles<&'f str, &'f str>> {
let error_code = SimpleErrorCode::StringTooLong;
let error_detail = SimpleErrorDetail::StringTooLong {
file_id,
value: content[40..47].to_string(),
value_byte_indices: 39..48,
limit: 5,
};
let severity = Severity::Error;
SourceError::new(error_code, error_detail, severity)
}
4. 输出诊断消息。
let value_out_of_range = value_out_of_range(file_id);
let value_out_of_range = value_out_of_range.as_diagnostic(&files);
let string_too_long = string_too_long(file_id, content);
let string_too_long = string_too_long.as_diagnostic(&files);
let writer = StandardStream::stderr(ColorChoice::Always);
let config = term::Config::default();
term::emit(&mut writer.lock(), &config, &files, &value_out_of_range)?;
term::emit(&mut writer.lock(), &config, &files, &string_too_long)?;
示例用法请参阅 示例。
cargo run --example simple
cargo run --example source_ref_hint
cargo run --example long_expr_context
cargo run --example html > /tmp/index.html
cargo run --example codespan --features codespan
许可证
许可协议为以下之一
- Apache 许可证 2.0 版,(LICENSE-APACHE 或 https://www.apache.org/licenses/LICENSE-2.0)
- MIT 许可证 (LICENSE-MIT 或 https://opensource.org/licenses/MIT)
您可选择。
贡献
除非您明确说明,否则任何有意提交给作品并由您定义的 Apache-2.0 许可证所包含的贡献,都应按上述方式双重许可,不附加任何额外条款或条件。
依赖项
~0.2–9MB
~47K SLoC