tauri_store/
error.rs

1use serde::{Serialize, Serializer};
2use std::error::Error as StdError;
3use std::result::Result as StdResult;
4
5/// A [`Result`](std::result::Result) type with [`Error`](crate::Error) as the error variant.
6pub type Result<T> = StdResult<T, Error>;
7
8/// A [`Result`](std::result::Result) type with a boxed error.
9pub type BoxResult<T> = StdResult<T, Box<dyn StdError>>;
10
11/// Runtime errors for the stores.
12#[non_exhaustive]
13#[derive(thiserror::Error, Debug)]
14pub enum Error {
15  #[error(transparent)]
16  Io(#[from] std::io::Error),
17  #[error(transparent)]
18  Json(#[from] serde_json::Error),
19  #[error(transparent)]
20  Tauri(#[from] tauri::Error),
21}
22
23impl Error {
24  pub const fn is_bad_rid(&self) -> bool {
25    matches!(self, Self::Tauri(tauri::Error::BadResourceId(_)))
26  }
27}
28
29impl Serialize for Error {
30  fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
31  where
32    S: Serializer,
33  {
34    serializer.serialize_str(self.to_string().as_str())
35  }
36}
37
38#[doc(hidden)]
39#[macro_export]
40macro_rules! io_err {
41  ($variant:ident) => {{
42    use $crate::Error;
43    use std::io::{Error as IoError, ErrorKind};
44    let err = IoError::from(ErrorKind::$variant);
45    Err(Error::Io(err))
46  }};
47  ($variant:ident, $($arg:tt)*) => {{
48    use $crate::Error;
49    use std::io::{Error as IoError, ErrorKind};
50    let err = IoError::new(ErrorKind::$variant, format!($($arg)*));
51    Err(Error::Io(err))
52  }};
53}