Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ pub enum ContentError {
}

impl ContentError {
/// line number is 1-based, for humans.
pub fn to_with_context(self, line: Option<(usize, String)>) -> GerberParserErrorWithContext {
GerberParserErrorWithContext { error: self, line }
pub fn to_with_context(self, context: Option<ErrorContext>) -> GerberParserErrorWithContext {
GerberParserErrorWithContext {
error: self,
context,
}
}
}

Expand All @@ -150,18 +152,36 @@ impl PartialEq for ContentError {
}
}

/// Where in the source a parse error occurred.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ErrorContext {
/// 1-based line number on which the failing token starts.
pub line: usize,
/// 1-based column offset of the failing token within that line.
pub offset: usize,
/// The token (command block) that failed to parse.
pub token: String,
}

#[derive(Error, Debug, PartialEq)]
pub struct GerberParserErrorWithContext {
pub error: ContentError,
/// line number is 1-based, for humans.
pub line: Option<(usize, String)>,
pub context: Option<ErrorContext>,
}

impl std::fmt::Display for GerberParserErrorWithContext {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self.line {
Some((number, content)) => {
write!(f, "Error: {}\nLine {}: '{}'", self.error, number, content)
match &self.context {
Some(ErrorContext {
line,
offset,
token,
}) => {
write!(
f,
"Error: {}\nLine {}:{}: '{}'",
self.error, line, offset, token
)
}
_ => {
write!(f, "Error at unspecified line: {}", self.error)
Expand Down
Loading
Loading