tauri_plugin_notifications/
error.rs1use serde::{ser::Serializer, Serialize};
6
7pub type Result<T> = std::result::Result<T, Error>;
8
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11 #[error(transparent)]
12 Io(#[from] std::io::Error),
13 #[cfg(mobile)]
14 #[error(transparent)]
15 PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
16}
17
18impl Serialize for Error {
19 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
20 where
21 S: Serializer,
22 {
23 serializer.serialize_str(self.to_string().as_ref())
24 }
25}
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30 use std::io;
31
32 #[test]
33 fn test_io_error_conversion() {
34 let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
35 let err: Error = io_err.into();
36 assert!(matches!(err, Error::Io(_)));
37 }
38
39 #[test]
40 fn test_io_error_serialization() {
41 let io_err = io::Error::new(io::ErrorKind::PermissionDenied, "access denied");
42 let err = Error::Io(io_err);
43 let json = serde_json::to_string(&err).expect("Failed to serialize IO error");
44 assert!(json.contains("access denied"));
45 }
46
47 #[test]
48 fn test_io_error_display() {
49 let io_err = io::Error::new(io::ErrorKind::NotFound, "test error");
50 let err = Error::Io(io_err);
51 let display_str = format!("{}", err);
52 assert!(display_str.contains("test error"));
53 }
54
55 #[cfg(mobile)]
56 #[test]
57 fn test_plugin_invoke_error_conversion() {
58 use tauri::plugin::mobile::PluginInvokeError;
59
60 let plugin_err = PluginInvokeError::MethodNotFound("test_method".to_string());
61 let err: Error = plugin_err.into();
62 assert!(matches!(err, Error::PluginInvoke(_)));
63 }
64
65 #[test]
66 fn test_result_type_err() {
67 let io_err = io::Error::other("test");
68 let result: Result<i32> = Err(Error::Io(io_err));
69 assert!(result.is_err());
70 }
71}