Skip to content

Commit

Permalink
diag: Improve parse errors from sysusers
Browse files Browse the repository at this point in the history
  • Loading branch information
VorpalBlade committed Dec 16, 2024
1 parent aaca749 commit ab712e5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
26 changes: 22 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ resolver = "2"

[workspace.dependencies]
ahash = "0.8.11"
annotate-snippets = { version = "0.11.5", features = ["simd"] }
ar = "0.9.0"
base64-simd = "0.8.0"
bitflags = { version = "2.6.0", features = ["serde"] }
Expand Down
1 change: 1 addition & 0 deletions crates/konfigkoll_script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ version = "0.1.7"

[dependencies]
ahash.workspace = true
annotate-snippets.workspace = true
camino.workspace = true
color-eyre.workspace = true
compact_str.workspace = true
Expand Down
29 changes: 16 additions & 13 deletions crates/konfigkoll_script/src/plugins/passwd/sysusers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use winnow::Parser;
#[derive(Debug, PartialEq)]
pub(super) struct SysusersParseError {
message: String,
pos: usize,
span: std::ops::Range<usize>,
input: String,
}

Expand All @@ -32,27 +32,30 @@ impl SysusersParseError {
) -> Self {
let message = error.inner().to_string();
let input = input.to_owned();
let start = error.offset();
let end = (start + 1..)
.find(|e| input.is_char_boundary(*e))
.unwrap_or(start);
Self {
message,
pos: error.offset(),
span: start..end,
input,
}
}
}

impl std::fmt::Display for SysusersParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let pos = self.pos;
let input = &self.input;
let message = &self.message;
write!(
f,
"Error at position {}: {}\n{}\n{}^",
pos,
message,
&input[..pos],
" ".repeat(pos)
)
let message = annotate_snippets::Level::Error
.title(&self.message)
.snippet(
annotate_snippets::Snippet::source(&self.input)
.fold(true)
.annotation(annotate_snippets::Level::Error.span(self.span.clone())),
);
let renderer = annotate_snippets::Renderer::plain();
let rendered = renderer.render(message);
rendered.fmt(f)
}
}

Expand Down

0 comments on commit ab712e5

Please sign in to comment.