tauri_plugin_widgets/
error.rs1use serde::{Serialize, Serializer};
8
9pub type Result<T> = std::result::Result<T, Error>;
11
12#[derive(Debug, Clone)]
14pub enum Error {
15 Io(String),
17 String(String),
19 #[cfg(mobile)]
21 PluginInvoke(String),
22 SerdeJson(String),
24 Unsupported(String),
26}
27
28impl Error {
29 pub fn new(msg: impl Into<String>) -> Self {
31 Error::String(msg.into())
32 }
33
34 pub fn unsupported(operation: &str) -> Self {
37 Error::Unsupported(format!("`{}` is not supported on this platform", operation))
38 }
39}
40
41impl std::fmt::Display for Error {
42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 match self {
44 Error::Io(err) => write!(f, "IO error: {}", err),
45 Error::String(s) => write!(f, "{}", s),
46 #[cfg(mobile)]
47 Error::PluginInvoke(err) => write!(f, "Plugin invoke error: {}", err),
48 Error::SerdeJson(err) => write!(f, "Serde JSON error: {}", err),
49 Error::Unsupported(msg) => write!(f, "Unsupported: {}", msg),
50 }
51 }
52}
53
54impl std::error::Error for Error {}
55
56impl From<std::io::Error> for Error {
57 fn from(err: std::io::Error) -> Self {
58 Error::Io(err.to_string())
59 }
60}
61
62impl From<serde_json::Error> for Error {
63 fn from(err: serde_json::Error) -> Self {
64 Error::SerdeJson(err.to_string())
65 }
66}
67
68#[cfg(mobile)]
69impl From<tauri::plugin::mobile::PluginInvokeError> for Error {
70 fn from(err: tauri::plugin::mobile::PluginInvokeError) -> Self {
71 Error::PluginInvoke(err.to_string())
72 }
73}
74
75impl From<&str> for Error {
76 fn from(err: &str) -> Self {
77 Error::String(err.to_string())
78 }
79}
80
81impl From<String> for Error {
82 fn from(err: String) -> Self {
83 Error::String(err)
84 }
85}
86
87impl Serialize for Error {
88 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
89 where
90 S: Serializer,
91 {
92 serializer.serialize_str(&self.to_string())
93 }
94}
95
96unsafe impl Send for Error {}
97unsafe impl Sync for Error {}