use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
message: String,
line: usize,
column: usize,
offset: usize,
}
impl Error {
pub(crate) fn parse(
message: impl Into<String>,
line: usize,
column: usize,
offset: usize,
) -> Self {
Error {
message: message.into(),
line,
column,
offset,
}
}
pub(crate) fn custom(message: impl Into<String>) -> Self {
Error {
message: message.into(),
line: 0,
column: 0,
offset: 0,
}
}
pub fn message(&self) -> &str {
&self.message
}
pub fn line(&self) -> usize {
self.line
}
pub fn column(&self) -> usize {
self.column
}
pub fn offset(&self) -> usize {
self.offset
}
pub fn has_position(&self) -> bool {
self.line != 0
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.has_position() {
write!(
f,
"TOML parse error at line {}, column {}: {}",
self.line, self.column, self.message
)
} else {
f.write_str(&self.message)
}
}
}
impl std::error::Error for Error {}