use crate::client_api::error::ApiError; use crate::daemon_management::error::DaemonError; use bromine::error::Error; use serde::Serialize; use std::fmt::{Display, Formatter}; pub type PluginResult = Result; #[derive(Clone, Debug, Serialize)] pub struct PluginError { message: String, } impl Display for PluginError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { self.message.fmt(f) } } impl std::error::Error for PluginError {} impl From<&str> for PluginError { fn from(s: &str) -> Self { Self { message: s.to_string(), } } } impl From for PluginError { fn from(message: String) -> Self { Self { message } } } impl From for PluginError { fn from(e: ApiError) -> Self { let message = match e { ApiError::IPC(ipc_error) => match ipc_error { Error::Message(message) => message, Error::SendError => String::from("Failed to send event to daemon"), Error::ErrorEvent(e) => { format!("Received error: {}", e.to_string()) } e => { format!("{:?}", e) } }, ApiError::VersionMismatch { server, client } => { format!("The servers API version ({}) is not supported by the client ({}). Please make sure both are up to date.", server, client) } }; Self { message } } } impl From for PluginError { fn from(e: std::io::Error) -> Self { Self { message: e.to_string(), } } } impl From for PluginError { fn from(e: toml::de::Error) -> Self { Self { message: format!("Deserialization failed: {:?}", e), } } } impl From for PluginError { fn from(e: toml::ser::Error) -> Self { Self { message: format!("Serialization failed: {:?}", e), } } } impl From for PluginError { fn from(e: DaemonError) -> Self { Self { message: e.message } } }