tauri_plugin_polygon/
error.rs

1use serde::{Deserialize, Serialize};
2use std::{
3    collections::{HashMap, HashSet},
4    sync::{PoisonError, RwLockReadGuard, RwLockWriteGuard},
5};
6
7use crate::polygon;
8
9pub type Result<T> = std::result::Result<T, Error>;
10
11#[derive(thiserror::Error, Debug, Deserialize, Serialize, Clone)]
12pub enum Error {
13    #[error(
14        "Not Initialized. Call tauri_wherever::init(app_handle) first when setup a tauri app."
15    )]
16    NotInitialized,
17    #[error("Polygon with id [{0}] not found.")]
18    PolygonNotFound(String),
19    #[error("Polygon with id [{0}] already exists.")]
20    PolygonExists(String),
21    #[error("At least 3 points needed but got {0}.")]
22    PointsNotEnough(usize),
23    #[error("Can not read/write cache. {0}")]
24    LockError(String),
25    #[error("Failed to initialize plugin. {0}")]
26    PluginInitializationError(String),
27}
28
29impl From<std::io::Error> for Error {
30    fn from(error: std::io::Error) -> Self {
31        Error::PluginInitializationError(format!("{error}"))
32    }
33}
34
35impl<'a> From<PoisonError<RwLockWriteGuard<'a, HashSet<String>>>> for Error {
36    fn from(error: PoisonError<RwLockWriteGuard<'a, HashSet<String>>>) -> Self {
37        Error::LockError(format!("{error}"))
38    }
39}
40
41impl<'a> From<PoisonError<RwLockReadGuard<'a, HashMap<String, polygon::Polygon>>>> for Error {
42    fn from(error: PoisonError<RwLockReadGuard<'a, HashMap<String, polygon::Polygon>>>) -> Self {
43        Error::LockError(format!("{error}"))
44    }
45}
46
47impl<'a> From<PoisonError<RwLockWriteGuard<'a, HashMap<String, polygon::Polygon>>>> for Error {
48    fn from(error: PoisonError<RwLockWriteGuard<'a, HashMap<String, polygon::Polygon>>>) -> Self {
49        Error::LockError(format!("{error}"))
50    }
51}