Skip to main content

tauri_plugin_store/
error.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5use serde::{Serialize, Serializer};
6
7/// Alias for a [`Result`](std::result::Result) with the error type [`Error`].
8pub type Result<T> = std::result::Result<T, Error>;
9
10/// The error types.
11#[derive(thiserror::Error, Debug)]
12#[non_exhaustive]
13pub enum Error {
14    /// The store contents could not be serialized by the configured [`SerializeFn`](crate::SerializeFn).
15    #[error("Failed to serialize store. {0}")]
16    Serialize(Box<dyn std::error::Error + Send + Sync>),
17    /// The store contents could not be deserialized by the configured [`DeserializeFn`](crate::DeserializeFn).
18    #[error("Failed to deserialize store. {0}")]
19    Deserialize(Box<dyn std::error::Error + Send + Sync>),
20    /// JSON error.
21    #[error(transparent)]
22    Json(#[from] serde_json::Error),
23    /// IO error.
24    #[error(transparent)]
25    Io(#[from] std::io::Error),
26    // /// Store already exists
27    // #[error("Store at \"{0}\" already exists")]
28    // AlreadyExists(PathBuf),
29    /// Serialize function not found
30    #[error("Serialize Function \"{0}\" not found")]
31    SerializeFunctionNotFound(String),
32    /// Deserialize function not found
33    #[error("Deserialize Function \"{0}\" not found")]
34    DeserializeFunctionNotFound(String),
35    /// Some Tauri API failed
36    #[error(transparent)]
37    Tauri(#[from] tauri::Error),
38}
39
40impl Serialize for Error {
41    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
42    where
43        S: Serializer,
44    {
45        serializer.serialize_str(self.to_string().as_ref())
46    }
47}