Skip to main content

vs_plugin_api/
error.rs

1//! Error types shared by plugin backends and hosts.
2
3use std::path::PathBuf;
4
5use thiserror::Error;
6
7/// Errors shared by plugin backends.
8#[derive(Debug, Error)]
9pub enum PluginError {
10    /// The plugin source could not be parsed.
11    #[error("failed to parse plugin source at {path}: {message}")]
12    InvalidSource { path: PathBuf, message: String },
13    /// The requested plugin version is not exposed by the plugin.
14    #[error("plugin {plugin} does not expose version {version}")]
15    VersionNotFound { plugin: String, version: String },
16    /// The plugin did not provide a result for the requested hook.
17    #[error("plugin did not provide a result")]
18    NoResultProvided,
19    /// The backend hit an execution error.
20    #[error("plugin backend error: {0}")]
21    Backend(String),
22}
23
24/// Shared result type for plugin operations.
25pub type PluginResult<T> = Result<T, PluginError>;
26
27/// Extension trait to convert any `Display` error into [`PluginError::Backend`].
28pub trait IntoPluginResult<T> {
29    fn into_plugin_result(self) -> Result<T, PluginError>;
30}
31
32impl<T, E: std::fmt::Display> IntoPluginResult<T> for Result<T, E> {
33    fn into_plugin_result(self) -> Result<T, PluginError> {
34        self.map_err(|error| PluginError::Backend(error.to_string()))
35    }
36}