plux_rs/utils/
error.rs

1use semver::Version;
2use std::{
3    error::Error as StdError,
4    fmt::{Debug, Display},
5};
6use thiserror::Error;
7
8use crate::{Bundle, Depend};
9
10#[derive(Error, Debug)]
11pub enum BundleFromError {
12    #[error("Error converting OsStr to str")]
13    OsStrToStrFailed,
14    #[error("Failed to get ID")]
15    IDFailed,
16    #[error("Failed to get version")]
17    VersionFailed,
18    #[error("Failed to get format")]
19    FormatFailed,
20    #[error("Failed to parse version")]
21    ParseVersion(#[from] semver::Error),
22}
23
24#[cfg(feature = "archive")]
25#[derive(Error, Debug)]
26pub enum BundleZipError {
27    #[error("Bundle has no name")]
28    NoNameFailed,
29    #[error("Missing bundle")]
30    MissingBundleFailed,
31    #[error("The directory contains a directory with the same name as the bundle")]
32    ContainSameDirFailed,
33    #[error("Failed to create bundle")]
34    CreateBundleFailed(std::io::Error),
35    #[error("Failed to open file in bundle")]
36    OpenFileInBundleFailed(#[from] std::io::Error),
37    #[error("Failed to zip")]
38    ZipFailed(#[from] zip::result::ZipError),
39}
40
41#[cfg(feature = "archive")]
42#[derive(Error, Debug)]
43pub enum BundleUnzipError {
44    #[error("Bundle has no name")]
45    NoNameFailed,
46    #[error("Missing bundle")]
47    MissingBundleFailed,
48    #[error("The directory contains a file with the same name as the bundle")]
49    ContainSameFileFailed,
50    #[error("Failed to open bundle")]
51    OpenBundleFailed(#[from] std::io::Error),
52    #[error("Failed to unzip")]
53    UnzipFailed(#[from] zip::result::ZipError),
54    #[error("Error creating BundleInfo")]
55    BundleFromFailed(#[from] BundleFromError),
56}
57
58#[derive(Error, Debug)]
59pub enum StopLoaderError {
60    #[error("Failed to unregister plugins `{0:?}`")]
61    UnregisterPluginFailed(Vec<UnregisterPluginError>),
62    #[error("Failed to unregister managers `{0:?}`")]
63    UnregisterManagerFailed(Vec<UnregisterManagerError>),
64}
65
66#[derive(Error, Debug)]
67pub enum RegisterManagerError {
68    #[error("Format `{0}` is already occupied")]
69    AlreadyOccupiedFormat(String),
70    #[error("Manager registration error by the manager")]
71    RegisterManagerByManager(#[from] Box<dyn StdError + Send + Sync>),
72}
73
74#[derive(Error, Debug)]
75pub enum UnregisterManagerError {
76    #[error("Not found manager")]
77    NotFound,
78    #[error("Failed to unregister plugin")]
79    UnregisterPlugin(#[from] UnregisterPluginError),
80    #[error("Manager unregistration error by the manager")]
81    UnregisterManagerByManager(#[from] Box<dyn StdError + Send + Sync>),
82}
83
84#[derive(Error, Debug)]
85pub enum RegisterPluginError {
86    #[error("Not found plugin")]
87    NotFound,
88    #[error("Failed to bundle from filename")]
89    BundleFromFailed(#[from] BundleFromError),
90    #[error("Unknown plugin manager for the format '{0}'")]
91    UnknownManagerFormat(String),
92    #[error("Plugin registration error by the manager")]
93    RegisterPluginByManager(#[from] Box<dyn StdError + Send + Sync>),
94    #[error("A plugin with ID `{0}` and version `{1}` already exists")]
95    AlreadyExistsIDAndVersion(String, Version),
96}
97
98#[derive(Error, Debug)]
99pub enum UnregisterPluginError {
100    #[error("Not found plugin")]
101    NotFound,
102    #[error("Plugin unload error")]
103    UnloadError(#[from] UnloadPluginError),
104    #[error("The plugin has an unregistered manager")]
105    HasUnregisteredManager,
106    #[error("Plugin unregistration error by the manager")]
107    UnregisterPluginByManager(#[from] Box<dyn StdError + Send + Sync>),
108}
109
110#[derive(Error, Debug)]
111pub enum LoadPluginError {
112    #[error("Not found plugin")]
113    NotFound,
114    #[error("The following dependencies could not be found: {0:?}")]
115    NotFoundDependencies(Vec<Depend>),
116    #[error("Dependency `{depend}` returned an error: {error:?}")]
117    LoadDependency {
118        depend: Depend,
119        error: Box<LoadPluginError>,
120    },
121    #[error("Plugin load error by the manager")]
122    LoadPluginByManager(#[from] Box<dyn StdError + Send + Sync>),
123    #[error("Requests not found: {0:?}")]
124    RequestsNotFound(Vec<String>),
125}
126
127#[derive(Error, Debug)]
128pub enum UnloadPluginError {
129    #[error("Not found plugin")]
130    NotFound,
131    #[error("The plugin `{plugin}` currently uses the plugin `{depend}` as a dependency")]
132    CurrentlyUsesDepend { plugin: Bundle, depend: Bundle },
133    #[error("Plugin unload error by the manager")]
134    UnloadPluginByManager(#[from] Box<dyn StdError + Send + Sync>),
135}
136
137#[derive(Error, Debug)]
138pub enum RegisterRequestError {
139    #[error("Function not found")]
140    NotFound,
141    #[error("The arguments are set incorrectly")]
142    ArgumentsIncorrectly,
143}
144
145#[derive(Error, Debug)]
146pub enum PluginCallRequestError {
147    #[error("Request not found")]
148    NotFound,
149}
150
151#[derive(Error, Debug)]
152pub enum PluginRegisterFunctionError {
153    #[error("Function {0} already exists")]
154    AlreadyExists(String),
155}
156
157#[derive(Error, Debug)]
158pub enum PluginCallFunctionError {
159    #[error("Function not found")]
160    NotFound,
161}
162
163#[derive(Error, Debug)]
164pub enum CallFunctionDependError {
165    #[error("Depend not found")]
166    DependNotFound,
167    #[error("Failed to call function")]
168    FailedCallFunction(#[from] PluginCallFunctionError),
169}
170
171pub type ManagerResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync>>;
172
173#[derive(Debug)]
174pub struct ParseVariableError {
175    ty: &'static str,
176}
177
178impl ParseVariableError {
179    pub fn new(ty: &'static str) -> Self {
180        Self { ty }
181    }
182}
183
184impl Display for ParseVariableError {
185    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
186        write!(f, "data cannot be converted to this type `{}`", self.ty)
187    }
188}
189
190impl StdError for ParseVariableError {}