use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum EncodeError {
#[error("unsupported value: {0}")]
Unsupported(String),
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum DecodeError {
#[error("syntax error at line {line}, column {col}: {message}")]
Syntax {
message: String,
line: u32,
col: u32,
},
#[error("structure error: {0}")]
Structure(String),
}
impl DecodeError {
#[must_use]
pub fn new(message: impl Into<String>) -> Self {
Self::Structure(message.into())
}
}
pub type EncodeResult<T> = Result<T, EncodeError>;
pub type DecodeResult<T> = Result<T, DecodeError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decode_syntax_error_displays_location() {
let e = DecodeError::Syntax { message: "unexpected }".into(), line: 3, col: 5 };
assert_eq!(e.to_string(), "syntax error at line 3, column 5: unexpected }");
}
#[test]
fn encode_unsupported_displays() {
let e = EncodeError::Unsupported("NaN".into());
assert_eq!(e.to_string(), "unsupported value: NaN");
}
}