use std::fmt;
use std::io;
pub(crate) type ZaloResult<T> = Result<T, Error>;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Io(io::Error),
Json(serde_json::Error),
#[cfg(feature = "dump")]
MsgPackEncode(rmp_serde::encode::Error),
#[cfg(feature = "dump")]
MsgPackDecode(rmp_serde::decode::Error),
#[allow(missing_docs)]
InvalidHexColor { value: String, reason: String },
GrammarNotFound(String),
ThemeNotFound(String),
TokenizeRegex(String),
UnlinkedGrammars,
ReplacingGrammarPostLinking(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(err) => write!(f, "I/O error: {}", err),
Error::Json(err) => write!(f, "JSON parsing error: {}", err),
#[cfg(feature = "dump")]
Error::MsgPackEncode(err) => write!(f, "MessagePack encoding error: {}", err),
#[cfg(feature = "dump")]
Error::MsgPackDecode(err) => write!(f, "MessagePack decoding error: {}", err),
Error::InvalidHexColor { value, reason } => {
write!(f, "invalid hex color '{}': {}", value, reason)
}
Error::GrammarNotFound(name) => write!(f, "grammar '{}' not found", name),
Error::ThemeNotFound(name) => write!(f, "theme '{}' not found", name),
Error::TokenizeRegex(message) => write!(f, "regex compilation error: {}", message),
Error::UnlinkedGrammars => {
write!(f, "grammars are unlinked, call `registry.link_grammars()`")
}
Error::ReplacingGrammarPostLinking(s) => {
write!(f, "Tried to replace grammar `{s}` after linking")
}
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(err) => Some(err),
Error::Json(err) => Some(err),
#[cfg(feature = "dump")]
Error::MsgPackEncode(err) => Some(err),
#[cfg(feature = "dump")]
Error::MsgPackDecode(err) => Some(err),
Error::InvalidHexColor { .. }
| Error::UnlinkedGrammars
| Error::ReplacingGrammarPostLinking(_)
| Error::GrammarNotFound(_)
| Error::ThemeNotFound(_)
| Error::TokenizeRegex(_) => None,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err)
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error::Json(err)
}
}
#[cfg(feature = "dump")]
impl From<rmp_serde::encode::Error> for Error {
fn from(err: rmp_serde::encode::Error) -> Self {
Error::MsgPackEncode(err)
}
}
#[cfg(feature = "dump")]
impl From<rmp_serde::decode::Error> for Error {
fn from(err: rmp_serde::decode::Error) -> Self {
Error::MsgPackDecode(err)
}
}