rill_json/error.rs
1//! Contains the primary `ParseError` type for the library.
2use std::fmt;
3
4/// The primary error type for all parsing operations.
5///
6/// This struct contains a human-readable error message and the
7/// location (line and column) where the error occurred.
8#[derive(Debug, PartialEq)]
9pub struct ParseError {
10 /// A description of what went wrong.
11 pub message: String,
12 /// The line number (1-indexed) where the error was detected.
13 pub line: usize,
14 /// The column number (1-indexed) where the error was detected.
15 pub column: usize,
16}
17
18// --- Error Formatting ---
19// This provides a user-friendly, human-readable error message.
20impl fmt::Display for ParseError {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 write!(
23 f,
24 "Error: {} at line {}, column {}.",
25 self.message, self.line, self.column
26 )
27 }
28}
29
30// --- Unit Tests ---
31#[cfg(test)]
32mod tests {
33 use super::*;
34
35 #[test]
36 fn test_error_display() {
37 let error = ParseError {
38 message: "Unexpected ']'".to_string(),
39 line: 10,
40 column: 5,
41 };
42 assert_eq!(
43 error.to_string(),
44 "Error: Unexpected ']' at line 10, column 5."
45 );
46 }
47}