use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
Malformed,
Unsupported,
LimitExceeded,
InvalidEdit,
Encode,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, thiserror::Error)]
pub struct Error {
kind: ErrorKind,
code: &'static str,
}
impl Error {
pub(crate) const fn malformed(code: &'static str) -> Self {
Self::new(ErrorKind::Malformed, code)
}
pub(crate) const fn unsupported(code: &'static str) -> Self {
Self::new(ErrorKind::Unsupported, code)
}
pub(crate) const fn limit(code: &'static str) -> Self {
Self::new(ErrorKind::LimitExceeded, code)
}
pub(crate) const fn invalid_edit(code: &'static str) -> Self {
Self::new(ErrorKind::InvalidEdit, code)
}
pub(crate) const fn new(kind: ErrorKind, code: &'static str) -> Self {
Self { kind, code }
}
pub const fn kind(&self) -> ErrorKind {
self.kind
}
pub const fn code(&self) -> &'static str {
self.code
}
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.code)
}
}
pub type Result<T, E = Error> = std::result::Result<T, E>;