use alloc::string::String;
use core::fmt;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ErrorKind {
Path,
Limit,
NoSuchMember,
NotAList,
Scalar,
NothingSelected,
Value,
Refused,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
kind: ErrorKind,
at: String,
reason: String,
code: Option<u16>,
}
impl Error {
pub fn new(kind: ErrorKind, at: impl Into<String>, reason: impl Into<String>) -> Self {
Self {
kind,
at: at.into(),
reason: reason.into(),
code: None,
}
}
pub fn with_code(mut self, code: u16) -> Self {
self.code = Some(code);
self
}
pub fn code(&self) -> Option<u16> {
self.code
}
pub fn kind(&self) -> ErrorKind {
self.kind
}
pub fn at(&self) -> &str {
&self.at
}
pub(crate) fn located(mut self, path: &str) -> Self {
if self.at.is_empty() {
self.at = String::from(path);
}
self
}
pub fn reason(&self) -> &str {
&self.reason
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.at.is_empty() {
true => f.write_str(&self.reason),
false => write!(f, "{}, at {}", self.reason, self.at),
}
}
}
impl core::error::Error for Error {}