tauri_plugin_android_fs/models/
error.rs1use std::borrow::Cow;
2use serde::{ser::Serializer, Serialize};
3
4#[derive(Debug, thiserror::Error)]
5#[error(transparent)]
6pub struct Error {
7 inner: InnerError
8}
9
10#[allow(unused)]
11impl crate::Error {
12
13 pub(crate) const NOT_ANDROID: Self = Self::from_static_str(
14 "unsupported platform; only Android is supported"
15 );
16
17 pub(crate) fn missing_value(value_name: impl AsRef<str>) -> Self {
18 Self::with(format!("missing value: {}", value_name.as_ref()))
19 }
20
21 pub(crate) const fn from_static_str(msg: &'static str) -> Self {
22 Self { inner: InnerError::Raw(Cow::Borrowed(msg)), }
23 }
24
25 pub fn with(msg: impl Into<Cow<'static, str>>) -> Self {
26 Self { inner: InnerError::Raw(msg.into()) }
27 }
28}
29
30impl From<crate::Error> for std::io::Error {
31
32 fn from(e: crate::Error) -> std::io::Error {
33 match e.inner {
34 InnerError::Io(e) => e,
35 e => std::io::Error::new(std::io::ErrorKind::Other, e)
36 }
37 }
38}
39
40impl From<crate::Error> for tauri::Error {
41
42 fn from(e: crate::Error) -> tauri::Error {
43 match e.inner {
44 InnerError::Tauri(e) => e,
45 InnerError::Io(e) => tauri::Error::Io(e),
46
47 #[cfg(target_os = "android")]
48 InnerError::PluginInvoke(e) => tauri::Error::PluginInvoke(e),
49
50 e => tauri::Error::Anyhow(e.into()),
51 }
52 }
53}
54
55
56#[derive(Debug, thiserror::Error)]
57enum InnerError {
58 #[error("{0}")]
59 Raw(Cow<'static, str>),
60
61 #[cfg(target_os = "android")]
62 #[error(transparent)]
63 PluginInvoke(tauri::plugin::mobile::PluginInvokeError),
64
65 #[cfg(target_os = "android")]
66 #[error(transparent)]
67 Base64Decode(base64::DecodeError),
68
69 #[error(transparent)]
70 Io(std::io::Error),
71
72 #[error(transparent)]
73 SerdeJson(serde_json::Error),
74
75 #[error(transparent)]
76 Tauri(tauri::Error),
77
78 #[error(transparent)]
79 StdSystemTime(std::time::SystemTimeError),
80}
81
82macro_rules! impl_into_err_from_inner {
83 ($from:ty, $e:pat => $a:expr) => {
84 impl From<$from> for crate::Error {
85 fn from($e: $from) -> crate::Error {
86 $a
87 }
88 }
89 };
90}
91
92#[cfg(target_os = "android")]
93impl_into_err_from_inner!(tauri::plugin::mobile::PluginInvokeError, e => crate::Error { inner: InnerError::PluginInvoke(e) });
94
95#[cfg(target_os = "android")]
96impl_into_err_from_inner!(base64::DecodeError, e => crate::Error { inner: InnerError::Base64Decode(e) });
97
98impl_into_err_from_inner!(std::io::Error, e => crate::Error { inner: InnerError::Io(e) });
99impl_into_err_from_inner!(serde_json::Error, e => crate::Error { inner: InnerError::SerdeJson(e) });
100impl_into_err_from_inner!(tauri::Error, e => crate::Error { inner: InnerError::Tauri(e) });
101impl_into_err_from_inner!(std::time::SystemTimeError, e => crate::Error { inner: InnerError::StdSystemTime(e) });
102
103impl<W> From<std::io::IntoInnerError<W>> for crate::Error {
104 fn from(e: std::io::IntoInnerError<W>) -> crate::Error {
105 crate::Error { inner: InnerError::Io(e.into_error()) }
106 }
107}
108
109impl Serialize for crate::Error {
110
111 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
112 where
113 S: Serializer,
114 {
115 match &self.inner {
116 InnerError::Raw(msg) => serializer.serialize_str(&msg),
117 e => serializer.serialize_str(&e.to_string())
118 }
119 }
120}