Index: Cargo.toml ================================================================== --- Cargo.toml +++ Cargo.toml @@ -1,8 +1,8 @@ [package] name = "qsu" -version = "0.1.0" +version = "0.2.0" edition = "2021" license = "0BSD" categories = [ "asynchronous" ] keywords = [ "service", "systemd", "winsvc" ] repository = "https://repos.qrnch.tech/pub/qsu" @@ -28,11 +28,11 @@ rt = [] tokio = ["rt", "tokio/macros", "tokio/rt-multi-thread", "tokio/signal"] wait-for-debugger = ["dep:dbgtools-win"] [dependencies] -apperr = { version = "0.1.0" } +apperr = { version = "0.2.0" } async-trait = { version = "0.1.77" } chrono = { version = "0.4.31" } clap = { version = "4.4.14", optional = true, features = [ "derive", "env", "string", "wrap_help" ] } Index: examples/err/mod.rs ================================================================== --- examples/err/mod.rs +++ examples/err/mod.rs @@ -5,10 +5,11 @@ IO(String), Qsu(String) } impl std::error::Error for Error {} +impl apperr::Blessed for Error {} impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Error::IO(s) => { Index: src/err.rs ================================================================== --- src/err.rs +++ src/err.rs @@ -81,25 +81,31 @@ /// Attempt to convert [`Error`] into application-specific error. /// /// If it's not an `Error::App()` nor can be downcast to type `E`, the error /// will be returned back as an `Err()`. - pub fn try_into_apperr(self) -> Result { + pub fn try_into_apperr(self) -> Result + where + E: std::error::Error + apperr::Blessed + 'static + { match self { Error::App(e) => match e.try_into_inner::() { Ok(e) => Ok(e), - Err(e) => Err(Error::App(AppErr::new(e))) + Err(e) => Err(Error::App(e)) }, e => Err(e) } } /// Unwrap application-specific error from an [`Error`]. /// /// # Panic /// Panics if `Error` variant is not `Error::App()`. - pub fn unwrap_apperr(self) -> E { + pub fn unwrap_apperr(self) -> E + where + E: std::error::Error + apperr::Blessed + 'static + { let Ok(e) = self.try_into_apperr::() else { panic!("Unable to unwrap error E"); }; e } Index: src/rt.rs ================================================================== --- src/rt.rs +++ src/rt.rs @@ -74,14 +74,24 @@ //! callbacks to return application-defined errors using the regular question //! mark, it may be helpful to add the following error conversion to the error //! module: //! //! ``` +//! use std::fmt; +//! //! // Application-specific Error type +//! #[derive(Debug)] //! enum Error { //! // .. application-defined error variants //! } +//! impl fmt::Display for Error { +//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { +//! Ok(()) +//! } +//! } +//! impl std::error::Error for Error {} +//! impl apperr::Blessed for Error {} //! //! impl From for qsu::AppErr { //! fn from(err: Error) -> qsu::AppErr { //! qsu::AppErr::new(err) //! } Index: tests/err/mod.rs ================================================================== --- tests/err/mod.rs +++ tests/err/mod.rs @@ -6,10 +6,11 @@ IO(String), Qsu(String) } impl std::error::Error for Error {} +impl apperr::Blessed for Error {} impl Error { pub fn hello(msg: impl ToString) -> Self { Error::Hello(msg.to_string()) } Index: www/changelog.md ================================================================== --- www/changelog.md +++ www/changelog.md @@ -3,10 +3,13 @@ ## [Unreleased] ### Added ### Changed + +- Use apperr 0.2.0, which introduces trait bounds to the `AppErr::new(E)` to + make it more difficult to pass the wrong type to it. ### Removed ---