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 {
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
60macro_rules! impl_into_err_from_inner {
61 ($from:ty, $e:pat => $a:expr) => {
62 impl From<$from> for crate::Error {
63 fn from($e: $from) -> crate::Error {
64 $a
65 }
66 }
67 };
68}
69
70#[cfg(target_os = "android")]
71impl_into_err_from_inner!(tauri::plugin::mobile::PluginInvokeError, e => crate::Error { inner: InnerError::PluginInvoke(e) });
72
73#[cfg(target_os = "android")]
74impl_into_err_from_inner!(base64::DecodeError, e => crate::Error { inner: InnerError::Base64Decode(e) });
75
76impl_into_err_from_inner!(std::io::Error, e => crate::Error { inner: InnerError::Io(e) });
77impl_into_err_from_inner!(serde_json::Error, e => crate::Error { inner: InnerError::SerdeJson(e) });
78impl_into_err_from_inner!(tauri::Error, e => crate::Error { inner: InnerError::Tauri(e) });
79
80impl<W> From<std::io::IntoInnerError<W>> for crate::Error {
81 fn from(e: std::io::IntoInnerError<W>) -> crate::Error {
82 crate::Error { inner: InnerError::Io(e.into_error()) }
83 }
84}
85
86impl Serialize for crate::Error {
87
88 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
89 where
90 S: Serializer,
91 {
92 match &self.inner {
93 InnerError::Raw(msg) => serializer.serialize_str(&msg),
94 e => serializer.serialize_str(&e.to_string())
95 }
96 }
97}