use ktav::{parse, Error, ErrorKind};
fn main() {
for src in [
"port:8080\n", "port: 80\nport: 443\n", "port:i twelve\n", ": orphan\n", "key: {\n inner: 1\n", "}\n", "x: {foo}\n", "just-some-text\n", ] {
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
);
}