use serde::{Serialize, Serializer};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone)]
pub enum Error {
Io(String),
String(String),
#[cfg(mobile)]
PluginInvoke(String),
SerdeJson(String),
Unsupported(String),
}
impl Error {
pub fn new(msg: impl Into<String>) -> Self {
Error::String(msg.into())
}
pub fn unsupported(operation: &str) -> Self {
Error::Unsupported(format!(
"`{}` is not supported on this platform",
operation
))
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Io(err) => write!(f, "IO error: {}", err),
Error::String(s) => write!(f, "{}", s),
#[cfg(mobile)]
Error::PluginInvoke(err) => write!(f, "Plugin invoke error: {}", err),
Error::SerdeJson(err) => write!(f, "Serde JSON error: {}", err),
Error::Unsupported(msg) => write!(f, "Unsupported: {}", msg),
}
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Io(err.to_string())
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error::SerdeJson(err.to_string())
}
}
#[cfg(mobile)]
impl From<tauri::plugin::mobile::PluginInvokeError> for Error {
fn from(err: tauri::plugin::mobile::PluginInvokeError) -> Self {
Error::PluginInvoke(err.to_string())
}
}
impl From<&str> for Error {
fn from(err: &str) -> Self {
Error::String(err.to_string())
}
}
impl From<String> for Error {
fn from(err: String) -> Self {
Error::String(err)
}
}
impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
unsafe impl Send for Error {}
unsafe impl Sync for Error {}