Skip to main content

tauri_plugin_android_fs/models/
error.rs

1use 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    #[error(transparent)]
82    Utf8Error(std::str::Utf8Error),
83}
84
85macro_rules! impl_into_err_from_inner {
86    ($from:ty, $e:pat => $a:expr) => {
87        impl From<$from> for crate::Error {
88            fn from($e: $from) -> crate::Error {
89                $a
90            }
91        }
92    };
93}
94
95#[cfg(target_os = "android")]
96impl_into_err_from_inner!(tauri::plugin::mobile::PluginInvokeError, e => crate::Error { inner: InnerError::PluginInvoke(e) });
97
98#[cfg(target_os = "android")]
99impl_into_err_from_inner!(base64::DecodeError, e => crate::Error { inner: InnerError::Base64Decode(e) });
100
101impl_into_err_from_inner!(std::io::Error, e => crate::Error { inner: InnerError::Io(e) });
102impl_into_err_from_inner!(serde_json::Error, e => crate::Error { inner: InnerError::SerdeJson(e) });
103impl_into_err_from_inner!(tauri::Error, e => crate::Error { inner: InnerError::Tauri(e) });
104impl_into_err_from_inner!(std::time::SystemTimeError, e => crate::Error { inner: InnerError::StdSystemTime(e) });
105impl_into_err_from_inner!(std::str::Utf8Error, e => crate::Error { inner: InnerError::Utf8Error(e) });
106
107impl<W> From<std::io::IntoInnerError<W>> for crate::Error {
108    fn from(e: std::io::IntoInnerError<W>) -> crate::Error {
109        crate::Error { inner: InnerError::Io(e.into_error()) }
110    }
111}
112
113impl Serialize for crate::Error {
114
115    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
116    where
117        S: Serializer,
118    {
119        match &self.inner {
120            InnerError::Raw(msg) => serializer.serialize_str(&msg),
121            e => serializer.serialize_str(&e.to_string())
122        }
123    }
124}