diff options
author | Matthias Beyer <matthias.beyer@ifm.com> | 2022-04-13 14:02:36 +0200 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@ifm.com> | 2022-04-13 14:34:30 +0200 |
commit | eadc3650207194010b13fb02d94b4723688bb595 (patch) | |
tree | ab9deb4756e8fb55219d73f9ece9e1305382a968 | |
parent | 443604eb92128c1588a9b162eca78bfd6dbff0c3 (diff) |
plugin_inotify: Adapt for miette as error handling crate
This patch adapts the plugin_inotify crate for the changes from
commit 9ccd86589db8f9a72c4d7f545813e8da40b4d039 ("Replace PluginError with miette::Error")
which replaced anyhow with miette.
Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | plugins/plugin_inotify/Cargo.toml | 2 | ||||
-rw-r--r-- | plugins/plugin_inotify/src/lib.rs | 15 |
3 files changed, 10 insertions, 9 deletions
@@ -2035,13 +2035,13 @@ dependencies = [ name = "plugin_inotify" version = "0.1.0" dependencies = [ - "anyhow", "async-trait", "env_logger 0.9.0", "futures", "humantime-serde", "inotify", "log", + "miette", "serde", "tedge_api", "tedge_lib", diff --git a/plugins/plugin_inotify/Cargo.toml b/plugins/plugin_inotify/Cargo.toml index 3b0438d8..1ea91cb5 100644 --- a/plugins/plugin_inotify/Cargo.toml +++ b/plugins/plugin_inotify/Cargo.toml @@ -6,11 +6,11 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -anyhow = "1" async-trait = "0.1" env_logger = "0.9" futures = "0.3" humantime-serde = "1" +miette = "4.4" inotify = "0.10" log = { version = "0.4", features = ["serde"] } serde = { version = "1.0.136", features = ["derive"] } diff --git a/plugins/plugin_inotify/src/lib.rs b/plugins/plugin_inotify/src/lib.rs index 302c2933..93d3ef8f 100644 --- a/plugins/plugin_inotify/src/lib.rs +++ b/plugins/plugin_inotify/src/lib.rs @@ -3,6 +3,7 @@ use std::path::PathBuf; use async_trait::async_trait; use tokio_util::sync::CancellationToken; +use miette::IntoDiagnostic; use tedge_api::Address; use tedge_api::Plugin; @@ -47,7 +48,7 @@ impl<PD: PluginDirectory> PluginBuilder<PD> for InotifyPluginBuilder { .clone() .try_into() .map(|_: InotifyConfig| ()) - .map_err(|_| anyhow::anyhow!("Failed to parse inotify configuration")) + .map_err(|_| miette::miette!("Failed to parse inotify configuration")) .map_err(PluginError::from) } @@ -59,7 +60,7 @@ impl<PD: PluginDirectory> PluginBuilder<PD> for InotifyPluginBuilder { ) -> Result<BuiltPlugin, PluginError> { let config = config .try_into::<InotifyConfig>() - .map_err(|_| anyhow::anyhow!("Failed to parse inotify configuration"))?; + .map_err(|_| miette::miette!("Failed to parse inotify configuration"))?; let addr = plugin_dir.get_address_for(&config.target)?; Ok(InotifyPlugin::new(addr, config).finish()) @@ -97,7 +98,7 @@ struct State { #[async_trait] impl Plugin for InotifyPlugin { async fn start(&mut self) -> Result<(), PluginError> { - let mut inotify = inotify::Inotify::init().map_err(anyhow::Error::from)?; + let mut inotify = inotify::Inotify::init().into_diagnostic()?; let mut watches = HashMap::new(); for (path, modes) in self.config.pathes.iter() { @@ -105,7 +106,7 @@ impl Plugin for InotifyPlugin { mask | inotify::WatchMask::from(*el) }); - let descriptor = inotify.add_watch(path, mask).map_err(anyhow::Error::from)?; + let descriptor = inotify.add_watch(path, mask).into_diagnostic()?; watches.insert(descriptor, path.clone()); } @@ -129,7 +130,7 @@ impl Plugin for InotifyPlugin { if let Some(stopper) = self.stopper.take() { stopper .stop() - .map_err(|_| anyhow::anyhow!("Failed to stop mainloop"))? + .map_err(|_| miette::miette!("Failed to stop mainloop"))? } Ok(()) } @@ -144,7 +145,7 @@ async fn main_inotify( let mut stream = state .inotify .event_stream(Vec::from([0; 1024])) - .map_err(anyhow::Error::from)?; + .into_diagnostic()?; loop { tokio::select! { @@ -165,7 +166,7 @@ async fn main_inotify( Some(Err(err)) => { debug!("Received inotify event = {:?}", err); if state.fail_on_err { - return Err(PluginError::from(anyhow::Error::from(err))) + return Err(err).into_diagnostic() } }, |