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