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 {
14        inner: InnerError::Raw(Cow::Borrowed("This plugin is only for Android"))
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 fn with(msg: impl Into<Cow<'static, str>>) -> Self {
22        Self { inner: InnerError::Raw(msg.into()) }
23    }
24}
25
26impl From<crate::Error> for std::io::Error {
27
28    fn from(e: crate::Error) -> std::io::Error {
29        match e.inner {
30            InnerError::Io(e) => e,
31            e => std::io::Error::new(std::io::ErrorKind::Other, e)
32        }
33    }
34}
35
36
37#[derive(Debug, thiserror::Error)]
38enum InnerError {
39    #[error("{0}")]
40    Raw(Cow<'static, str>),
41
42    #[cfg(target_os = "android")]
43    #[error(transparent)]
44    PluginInvoke(tauri::plugin::mobile::PluginInvokeError),
45
46    #[cfg(target_os = "android")]
47    #[error(transparent)]
48    Base64Decode(base64::DecodeError),
49
50    #[error(transparent)]
51    Io(std::io::Error),
52
53    #[error(transparent)]
54    SerdeJson(serde_json::Error),
55
56    #[error(transparent)]
57    Tauri(tauri::Error),
58
59    #[error(transparent)]
60    StdSystemTime(std::time::SystemTimeError),
61}
62
63macro_rules! impl_into_err_from_inner {
64    ($from:ty, $e:pat => $a:expr) => {
65        impl From<$from> for crate::Error {
66            fn from($e: $from) -> crate::Error {
67                $a
68            }
69        }
70    };
71}
72
73#[cfg(target_os = "android")]
74impl_into_err_from_inner!(tauri::plugin::mobile::PluginInvokeError, e => crate::Error { inner: InnerError::PluginInvoke(e) });
75
76#[cfg(target_os = "android")]
77impl_into_err_from_inner!(base64::DecodeError, e => crate::Error { inner: InnerError::Base64Decode(e) });
78
79impl_into_err_from_inner!(std::io::Error, e => crate::Error { inner: InnerError::Io(e) });
80impl_into_err_from_inner!(serde_json::Error, e => crate::Error { inner: InnerError::SerdeJson(e) });
81impl_into_err_from_inner!(tauri::Error, e => crate::Error { inner: InnerError::Tauri(e) });
82impl_into_err_from_inner!(std::time::SystemTimeError, e => crate::Error { inner: InnerError::StdSystemTime(e) });
83
84impl<W> From<std::io::IntoInnerError<W>> for crate::Error {
85    fn from(e: std::io::IntoInnerError<W>) -> crate::Error {
86        crate::Error { inner: InnerError::Io(e.into_error()) }
87    }
88}
89
90impl Serialize for crate::Error {
91
92    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
93    where
94        S: Serializer,
95    {
96        match &self.inner {
97            InnerError::Raw(msg) => serializer.serialize_str(&msg),
98            e => serializer.serialize_str(&e.to_string())
99        }
100    }
101}