tauri_plugin_android_fs/models/
error.rs

1use std::borrow::Cow;
2use serde::{ser::Serializer, Serialize};
3
4#[derive(Debug, Clone, thiserror::Error)]
5#[error("{msg}")]
6pub struct Error {
7    pub(crate) msg: Cow<'static, str>
8}
9
10#[allow(unused)]
11impl Error {
12
13    pub(crate) const NOT_ANDROID: Self = Self {
14        msg: Cow::Borrowed("This plugin is only for Android")
15    };
16
17    pub(crate) fn with(msg: impl Into<Cow<'static, str>>) -> Self {
18        Self { msg: msg.into() }
19    }
20}
21
22#[cfg(target_os = "android")]
23impl From<tauri::plugin::mobile::PluginInvokeError> for crate::Error {
24
25    fn from(value: tauri::plugin::mobile::PluginInvokeError) -> Self {
26        Self { msg: Cow::Owned(value.to_string())}
27    }
28}
29
30#[cfg(target_os = "android")]
31impl From<base64::DecodeError> for crate::Error {
32
33    fn from(value: base64::DecodeError) -> Self {
34        Self { msg: Cow::Owned(value.to_string())}
35    }
36}
37
38impl From<std::io::Error> for crate::Error {
39
40    fn from(value: std::io::Error) -> Self {
41        Self { msg: Cow::Owned(value.to_string())}
42    }
43}
44
45impl<W> From<std::io::IntoInnerError<W>> for crate::Error {
46
47    fn from(value: std::io::IntoInnerError<W>) -> Self {
48        Self { msg: Cow::Owned(value.error().to_string())}
49    }
50}
51
52impl From<serde_json::Error> for crate::Error {
53
54    fn from(value: serde_json::Error) -> Self {
55        Self { msg: Cow::Owned(value.to_string())}
56    }
57}
58
59impl From<tauri::Error> for crate::Error {
60
61    fn from(value: tauri::Error) -> Self {
62        Self { msg: Cow::Owned(value.to_string())}
63    }
64}
65
66impl Serialize for crate::Error {
67
68    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
69    where
70        S: Serializer,
71    {
72        serializer.serialize_str(&self.msg)
73    }
74}