-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.rs
More file actions
55 lines (52 loc) · 2.22 KB
/
errors.rs
File metadata and controls
55 lines (52 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! Demonstrates the structured-error API: every parse failure carries
//! a typed `ErrorKind` with a byte-offset `Span`, so editor integrations
//! and tooling can highlight the exact offending range instead of a
//! whole line.
//!
//! Run with:
//!
//! cargo run -p ktav --example errors
use ktav::{parse, Error, ErrorKind};
fn main() {
// Each input below triggers a different error category. We dispatch
// on the structured variant and print line / span / category.
for src in [
"port:8080\n", // MissingSeparatorSpace
"port: 80\nport: 443\n", // DuplicateKey
"port:i twelve\n", // InvalidTypedScalar
": orphan\n", // EmptyKey
"key: {\n inner: 1\n", // UnclosedCompound (object)
"}\n", // UnbalancedBracket
"x: {foo}\n", // InlineNonEmptyCompound
"just-some-text\n", // MissingSeparator
] {
match parse(src) {
Ok(_) => unreachable!("input is invalid: {src:?}"),
Err(Error::Structured(kind)) => describe(src, &kind),
Err(other) => println!("other: {other}"),
}
}
}
fn describe(src: &str, kind: &ErrorKind) {
let span = kind.span();
let (line, column) = span.line_col(src);
let slice = span.slice(src).unwrap_or("");
let category = match kind {
ErrorKind::MissingSeparatorSpace { .. } => "MissingSeparatorSpace",
ErrorKind::InvalidTypedScalar { .. } => "InvalidTypedScalar",
ErrorKind::DuplicateKey { .. } => "DuplicateKey",
ErrorKind::KeyPathConflict { .. } => "KeyPathConflict",
ErrorKind::EmptyKey { .. } => "EmptyKey",
ErrorKind::InvalidKey { .. } => "InvalidKey",
ErrorKind::UnclosedCompound { .. } => "UnclosedCompound",
ErrorKind::UnbalancedBracket { .. } => "UnbalancedBracket",
ErrorKind::InlineNonEmptyCompound { .. } => "InlineNonEmptyCompound",
ErrorKind::MissingSeparator { .. } => "MissingSeparator",
ErrorKind::Other { .. } => "Other",
_ => "<unknown — ErrorKind is #[non_exhaustive]>",
};
println!(
"{:<24} line {} col {} bytes {}..{} -> {:?}",
category, line, column, span.start, span.end, slice
);
}