refactor(core): plugin initialization return value (#1575)

This commit is contained in:
Lucas Fernandes Nogueira 2021-04-21 21:44:18 -03:00 committed by GitHub
parent f708ff824e
commit 508eddc784
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 2 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": patch
---
Change plugin trait `initialization` return type to `std::result::Result<(), Box<dyn std::error::Error>>`.

View File

@ -57,6 +57,9 @@ pub enum Error {
#[cfg(feature = "updater")]
#[error("Updater: {0}")]
TauriUpdater(#[from] crate::updater::Error),
/// Error initializing plugin.
#[error("failed to initialize plugin `{0}`: {1}")]
PluginInitialization(String, String),
}
impl From<serde_json::Error> for Error {

View File

@ -12,6 +12,9 @@ use crate::{
use serde_json::Value as JsonValue;
use std::collections::HashMap;
/// The plugin result type.
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
/// The plugin interface.
pub trait Plugin<M: Params>: Send {
/// The plugin name. Used as key on the plugin config object.
@ -19,7 +22,7 @@ pub trait Plugin<M: Params>: Send {
/// Initialize the plugin.
#[allow(unused_variables)]
fn initialize(&mut self, config: JsonValue) -> crate::Result<()> {
fn initialize(&mut self, config: JsonValue) -> Result<()> {
Ok(())
}
@ -69,7 +72,9 @@ impl<M: Params> PluginStore<M> {
/// Initializes all plugins in the store.
pub(crate) fn initialize(&mut self, config: &PluginConfig) -> crate::Result<()> {
self.store.values_mut().try_for_each(|plugin| {
plugin.initialize(config.0.get(plugin.name()).cloned().unwrap_or_default())
plugin
.initialize(config.0.get(plugin.name()).cloned().unwrap_or_default())
.map_err(|e| crate::Error::PluginInitialization(plugin.name().to_string(), e.to_string()))
})
}