From 8b159d2b56ae3b6036979596889c913cdfa6c0ee Mon Sep 17 00:00:00 2001 From: technofab Date: Tue, 7 Jan 2025 16:22:17 +0100 Subject: [PATCH] refactor: use eyre instead of anyhow --- Cargo.toml | 2 +- src/async_constructible.rs | 12 ++++++------ src/axum.rs | 4 ++-- src/lib.rs | 8 ++++---- src/resource.rs | 4 ++-- src/sync_constructible.rs | 12 ++++++------ 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4e3df47..9dd36ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ axum-extra = { version = "0.9.3", optional = true, features = [ ] } tracing = { version = "0.1", optional = true } thiserror = { version = "1.0", optional = true } -anyhow = { version = "1.0" } +eyre = { version = "0.6.12" } frunk = "0.4.2" [dev-dependencies] diff --git a/src/async_constructible.rs b/src/async_constructible.rs index 6626a5a..49c78b9 100644 --- a/src/async_constructible.rs +++ b/src/async_constructible.rs @@ -15,7 +15,7 @@ use crate::{ #[async_trait] pub trait AsyncConstructible: Sized + Any + Send + Sync { /// Error type for when resource fails to be constructed. - type Error: Into + Send + Sync; + type Error: Into + Send + Sync; /// Construct the resource with the provided application state. async fn construct_async(aero: &Aero) -> Result; /// Called after construction with the concrete resource to allow the callee @@ -48,7 +48,7 @@ impl AsyncConstructible for T { #[async_trait] pub trait IndirectlyAsyncConstructible: Sized + Any + Send + Sync { /// Error type for when resource fails to be constructed. - type Error: Into + Send + Sync; + type Error: Into + Send + Sync; /// Construct the resource with the provided application state. async fn construct_async(aero: &Aero) -> Result; /// Called after construction with the concrete resource to allow the callee @@ -118,12 +118,12 @@ impl AsyncConstructibleResource for #[async_trait] pub trait AsyncConstructibleResourceList: ResourceList { /// Construct every resource in this list in the provided aerosol instance - async fn construct_async(aero: &Aero) -> anyhow::Result<()>; + async fn construct_async(aero: &Aero) -> eyre::Result<()>; } #[async_trait] impl AsyncConstructibleResourceList for HNil { - async fn construct_async(_aero: &Aero) -> anyhow::Result<()> { + async fn construct_async(_aero: &Aero) -> eyre::Result<()> { Ok(()) } } @@ -132,7 +132,7 @@ impl AsyncConstructibleResourceList for HNil { impl AsyncConstructibleResourceList for HCons { - async fn construct_async(aero: &Aero) -> anyhow::Result<()> { + async fn construct_async(aero: &Aero) -> eyre::Result<()> { aero.try_init_async::().await.map_err(Into::into)?; T::construct_async(aero).await } @@ -201,7 +201,7 @@ impl Aero { /// Convert into a different variant of the Aero type. Any missing required resources /// will be automatically asynchronously constructed. - pub async fn try_construct_remaining_async(self) -> anyhow::Result> + pub async fn try_construct_remaining_async(self) -> eyre::Result> where R2: Sculptor + ResourceList, >::Remainder: AsyncConstructibleResourceList, diff --git a/src/axum.rs b/src/axum.rs index 228db42..5e76860 100644 --- a/src/axum.rs +++ b/src/axum.rs @@ -35,7 +35,7 @@ pub enum DependencyError { name: &'static str, /// Error returned by the resource constructor #[source] - source: anyhow::Error, + source: eyre::Error, }, } @@ -52,7 +52,7 @@ impl DependencyError { name: type_name::(), } } - pub(crate) fn failed_to_construct(error: impl Into) -> Self { + pub(crate) fn failed_to_construct(error: impl Into) -> Self { Self::FailedToConstruct { name: type_name::(), source: error.into(), diff --git a/src/lib.rs b/src/lib.rs index ef4caca..41677d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,7 +41,7 @@ //! # struct MagicNumber(i32); //! # trait EmailSender: Send + Sync { fn send(&self) {} } //! # impl EmailSender for PostmarkClient {} -//! # impl PostmarkClient { fn new() -> anyhow::Result { Ok(Self) }} +//! # impl PostmarkClient { fn new() -> eyre::Result { Ok(Self) }} //! use aerosol::{Aero, Constructible}; //! //! // Here, we can list all the things we want to guarantee are in @@ -91,7 +91,7 @@ //! // The `Constructible` trait can be implemented to allow resources to be automatically //! // constructed. //! impl Constructible for PostmarkClient { -//! type Error = anyhow::Error; +//! type Error = eyre::Error; //! //! fn construct(aero: &Aero) -> Result { //! PostmarkClient::new(/* initialize using environment variables */) @@ -109,7 +109,7 @@ //! } //! //! impl Constructible for ConnectionPool { -//! type Error = anyhow::Error; +//! type Error = eyre::Error; //! fn construct(aero: &Aero) -> Result { //! // ... //! # Ok(ConnectionPool) @@ -117,7 +117,7 @@ //! } //! //! impl Constructible for MessageQueue { -//! type Error = anyhow::Error; +//! type Error = eyre::Error; //! fn construct(aero: &Aero) -> Result { //! // ... //! # Ok(MessageQueue) diff --git a/src/resource.rs b/src/resource.rs index 63cde6f..fcdc654 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -36,14 +36,14 @@ pub(crate) fn unwrap_resource(opt: Option) -> T { } } -pub(crate) fn unwrap_constructed(res: Result>) -> U { +pub(crate) fn unwrap_constructed(res: Result>) -> U { match res { Ok(x) => x, Err(e) => panic!("Failed to construct `{}`: {}", type_name::(), e.into()), } } -pub(crate) fn unwrap_constructed_hlist(res: Result>) -> U { +pub(crate) fn unwrap_constructed_hlist(res: Result>) -> U { match res { Ok(x) => x, Err(e) => panic!( diff --git a/src/sync_constructible.rs b/src/sync_constructible.rs index bdb6500..4f6abd0 100644 --- a/src/sync_constructible.rs +++ b/src/sync_constructible.rs @@ -11,7 +11,7 @@ use crate::{ /// Implemented for values which can be constructed from other resources. pub trait Constructible: Sized + Any + Send + Sync { /// Error type for when resource fails to be constructed. - type Error: Into + Send + Sync; + type Error: Into + Send + Sync; /// Construct the resource with the provided application state. fn construct(aero: &Aero) -> Result; @@ -29,7 +29,7 @@ pub trait Constructible: Sized + Any + Send + Sync { /// Automatically implemented for values which can be indirectly constructed from other resources. pub trait IndirectlyConstructible: Sized + Any + Send + Sync { /// Error type for when resource fails to be constructed. - type Error: Into + Send + Sync; + type Error: Into + Send + Sync; /// Construct the resource with the provided application state. fn construct(aero: &Aero) -> Result; /// Called after construction with the concrete resource to allow the callee @@ -94,11 +94,11 @@ impl ConstructibleResource for T {} /// Automatically implemented for resource lists where every resource can be constructed. pub trait ConstructibleResourceList: ResourceList { /// Construct every resource in this list in the provided aerosol instance - fn construct(aero: &Aero) -> anyhow::Result<()>; + fn construct(aero: &Aero) -> eyre::Result<()>; } impl ConstructibleResourceList for HNil { - fn construct(_aero: &Aero) -> anyhow::Result<()> { + fn construct(_aero: &Aero) -> eyre::Result<()> { Ok(()) } } @@ -106,7 +106,7 @@ impl ConstructibleResourceList for HNil { impl ConstructibleResourceList for HCons { - fn construct(aero: &Aero) -> anyhow::Result<()> { + fn construct(aero: &Aero) -> eyre::Result<()> { aero.try_init::().map_err(Into::into)?; T::construct(aero) } @@ -175,7 +175,7 @@ impl Aero { /// Convert into a different variant of the Aero type. Any missing required resources /// will be automatically constructed. - pub fn try_construct_remaining(self) -> anyhow::Result> + pub fn try_construct_remaining(self) -> eyre::Result> where R2: Sculptor + ResourceList, >::Remainder: ConstructibleResourceList,