refactor: use eyre instead of anyhow

This commit is contained in:
technofab 2025-01-07 16:22:17 +01:00
parent 698dfa31e6
commit 8b159d2b56
6 changed files with 21 additions and 21 deletions

View file

@ -26,7 +26,7 @@ axum-extra = { version = "0.9.3", optional = true, features = [
] } ] }
tracing = { version = "0.1", optional = true } tracing = { version = "0.1", optional = true }
thiserror = { version = "1.0", optional = true } thiserror = { version = "1.0", optional = true }
anyhow = { version = "1.0" } eyre = { version = "0.6.12" }
frunk = "0.4.2" frunk = "0.4.2"
[dev-dependencies] [dev-dependencies]

View file

@ -15,7 +15,7 @@ use crate::{
#[async_trait] #[async_trait]
pub trait AsyncConstructible: Sized + Any + Send + Sync { pub trait AsyncConstructible: Sized + Any + Send + Sync {
/// Error type for when resource fails to be constructed. /// Error type for when resource fails to be constructed.
type Error: Into<anyhow::Error> + Send + Sync; type Error: Into<eyre::Error> + Send + Sync;
/// Construct the resource with the provided application state. /// Construct the resource with the provided application state.
async fn construct_async(aero: &Aero) -> Result<Self, Self::Error>; async fn construct_async(aero: &Aero) -> Result<Self, Self::Error>;
/// Called after construction with the concrete resource to allow the callee /// Called after construction with the concrete resource to allow the callee
@ -48,7 +48,7 @@ impl<T: Constructible> AsyncConstructible for T {
#[async_trait] #[async_trait]
pub trait IndirectlyAsyncConstructible: Sized + Any + Send + Sync { pub trait IndirectlyAsyncConstructible: Sized + Any + Send + Sync {
/// Error type for when resource fails to be constructed. /// Error type for when resource fails to be constructed.
type Error: Into<anyhow::Error> + Send + Sync; type Error: Into<eyre::Error> + Send + Sync;
/// Construct the resource with the provided application state. /// Construct the resource with the provided application state.
async fn construct_async(aero: &Aero) -> Result<Self, Self::Error>; async fn construct_async(aero: &Aero) -> Result<Self, Self::Error>;
/// Called after construction with the concrete resource to allow the callee /// Called after construction with the concrete resource to allow the callee
@ -118,12 +118,12 @@ impl<T: Resource + IndirectlyAsyncConstructible> AsyncConstructibleResource for
#[async_trait] #[async_trait]
pub trait AsyncConstructibleResourceList: ResourceList { pub trait AsyncConstructibleResourceList: ResourceList {
/// Construct every resource in this list in the provided aerosol instance /// Construct every resource in this list in the provided aerosol instance
async fn construct_async<R: ResourceList>(aero: &Aero<R>) -> anyhow::Result<()>; async fn construct_async<R: ResourceList>(aero: &Aero<R>) -> eyre::Result<()>;
} }
#[async_trait] #[async_trait]
impl AsyncConstructibleResourceList for HNil { impl AsyncConstructibleResourceList for HNil {
async fn construct_async<R: ResourceList>(_aero: &Aero<R>) -> anyhow::Result<()> { async fn construct_async<R: ResourceList>(_aero: &Aero<R>) -> eyre::Result<()> {
Ok(()) Ok(())
} }
} }
@ -132,7 +132,7 @@ impl AsyncConstructibleResourceList for HNil {
impl<H: AsyncConstructibleResource, T: AsyncConstructibleResourceList> impl<H: AsyncConstructibleResource, T: AsyncConstructibleResourceList>
AsyncConstructibleResourceList for HCons<H, T> AsyncConstructibleResourceList for HCons<H, T>
{ {
async fn construct_async<R: ResourceList>(aero: &Aero<R>) -> anyhow::Result<()> { async fn construct_async<R: ResourceList>(aero: &Aero<R>) -> eyre::Result<()> {
aero.try_init_async::<H>().await.map_err(Into::into)?; aero.try_init_async::<H>().await.map_err(Into::into)?;
T::construct_async(aero).await T::construct_async(aero).await
} }
@ -201,7 +201,7 @@ impl<R: ResourceList> Aero<R> {
/// Convert into a different variant of the Aero type. Any missing required resources /// Convert into a different variant of the Aero type. Any missing required resources
/// will be automatically asynchronously constructed. /// will be automatically asynchronously constructed.
pub async fn try_construct_remaining_async<R2, I>(self) -> anyhow::Result<Aero<R2>> pub async fn try_construct_remaining_async<R2, I>(self) -> eyre::Result<Aero<R2>>
where where
R2: Sculptor<R, I> + ResourceList, R2: Sculptor<R, I> + ResourceList,
<R2 as Sculptor<R, I>>::Remainder: AsyncConstructibleResourceList, <R2 as Sculptor<R, I>>::Remainder: AsyncConstructibleResourceList,

View file

@ -35,7 +35,7 @@ pub enum DependencyError {
name: &'static str, name: &'static str,
/// Error returned by the resource constructor /// Error returned by the resource constructor
#[source] #[source]
source: anyhow::Error, source: eyre::Error,
}, },
} }
@ -52,7 +52,7 @@ impl DependencyError {
name: type_name::<T>(), name: type_name::<T>(),
} }
} }
pub(crate) fn failed_to_construct<T>(error: impl Into<anyhow::Error>) -> Self { pub(crate) fn failed_to_construct<T>(error: impl Into<eyre::Error>) -> Self {
Self::FailedToConstruct { Self::FailedToConstruct {
name: type_name::<T>(), name: type_name::<T>(),
source: error.into(), source: error.into(),

View file

@ -41,7 +41,7 @@
//! # struct MagicNumber(i32); //! # struct MagicNumber(i32);
//! # trait EmailSender: Send + Sync { fn send(&self) {} } //! # trait EmailSender: Send + Sync { fn send(&self) {} }
//! # impl EmailSender for PostmarkClient {} //! # impl EmailSender for PostmarkClient {}
//! # impl PostmarkClient { fn new() -> anyhow::Result<Self> { Ok(Self) }} //! # impl PostmarkClient { fn new() -> eyre::Result<Self> { Ok(Self) }}
//! use aerosol::{Aero, Constructible}; //! use aerosol::{Aero, Constructible};
//! //!
//! // Here, we can list all the things we want to guarantee are in //! // 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 //! // The `Constructible` trait can be implemented to allow resources to be automatically
//! // constructed. //! // constructed.
//! impl Constructible for PostmarkClient { //! impl Constructible for PostmarkClient {
//! type Error = anyhow::Error; //! type Error = eyre::Error;
//! //!
//! fn construct(aero: &Aero) -> Result<Self, Self::Error> { //! fn construct(aero: &Aero) -> Result<Self, Self::Error> {
//! PostmarkClient::new(/* initialize using environment variables */) //! PostmarkClient::new(/* initialize using environment variables */)
@ -109,7 +109,7 @@
//! } //! }
//! //!
//! impl Constructible for ConnectionPool { //! impl Constructible for ConnectionPool {
//! type Error = anyhow::Error; //! type Error = eyre::Error;
//! fn construct(aero: &Aero) -> Result<Self, Self::Error> { //! fn construct(aero: &Aero) -> Result<Self, Self::Error> {
//! // ... //! // ...
//! # Ok(ConnectionPool) //! # Ok(ConnectionPool)
@ -117,7 +117,7 @@
//! } //! }
//! //!
//! impl Constructible for MessageQueue { //! impl Constructible for MessageQueue {
//! type Error = anyhow::Error; //! type Error = eyre::Error;
//! fn construct(aero: &Aero) -> Result<Self, Self::Error> { //! fn construct(aero: &Aero) -> Result<Self, Self::Error> {
//! // ... //! // ...
//! # Ok(MessageQueue) //! # Ok(MessageQueue)

View file

@ -36,14 +36,14 @@ pub(crate) fn unwrap_resource<T: Resource>(opt: Option<T>) -> T {
} }
} }
pub(crate) fn unwrap_constructed<T: Resource, U>(res: Result<U, impl Into<anyhow::Error>>) -> U { pub(crate) fn unwrap_constructed<T: Resource, U>(res: Result<U, impl Into<eyre::Error>>) -> U {
match res { match res {
Ok(x) => x, Ok(x) => x,
Err(e) => panic!("Failed to construct `{}`: {}", type_name::<T>(), e.into()), Err(e) => panic!("Failed to construct `{}`: {}", type_name::<T>(), e.into()),
} }
} }
pub(crate) fn unwrap_constructed_hlist<T, U>(res: Result<U, impl Into<anyhow::Error>>) -> U { pub(crate) fn unwrap_constructed_hlist<T, U>(res: Result<U, impl Into<eyre::Error>>) -> U {
match res { match res {
Ok(x) => x, Ok(x) => x,
Err(e) => panic!( Err(e) => panic!(

View file

@ -11,7 +11,7 @@ use crate::{
/// Implemented for values which can be constructed from other resources. /// Implemented for values which can be constructed from other resources.
pub trait Constructible: Sized + Any + Send + Sync { pub trait Constructible: Sized + Any + Send + Sync {
/// Error type for when resource fails to be constructed. /// Error type for when resource fails to be constructed.
type Error: Into<anyhow::Error> + Send + Sync; type Error: Into<eyre::Error> + Send + Sync;
/// Construct the resource with the provided application state. /// Construct the resource with the provided application state.
fn construct(aero: &Aero) -> Result<Self, Self::Error>; fn construct(aero: &Aero) -> Result<Self, Self::Error>;
@ -29,7 +29,7 @@ pub trait Constructible: Sized + Any + Send + Sync {
/// Automatically implemented for values which can be indirectly constructed from other resources. /// Automatically implemented for values which can be indirectly constructed from other resources.
pub trait IndirectlyConstructible: Sized + Any + Send + Sync { pub trait IndirectlyConstructible: Sized + Any + Send + Sync {
/// Error type for when resource fails to be constructed. /// Error type for when resource fails to be constructed.
type Error: Into<anyhow::Error> + Send + Sync; type Error: Into<eyre::Error> + Send + Sync;
/// Construct the resource with the provided application state. /// Construct the resource with the provided application state.
fn construct(aero: &Aero) -> Result<Self, Self::Error>; fn construct(aero: &Aero) -> Result<Self, Self::Error>;
/// Called after construction with the concrete resource to allow the callee /// Called after construction with the concrete resource to allow the callee
@ -94,11 +94,11 @@ impl<T: Resource + IndirectlyConstructible> ConstructibleResource for T {}
/// Automatically implemented for resource lists where every resource can be constructed. /// Automatically implemented for resource lists where every resource can be constructed.
pub trait ConstructibleResourceList: ResourceList { pub trait ConstructibleResourceList: ResourceList {
/// Construct every resource in this list in the provided aerosol instance /// Construct every resource in this list in the provided aerosol instance
fn construct<R: ResourceList>(aero: &Aero<R>) -> anyhow::Result<()>; fn construct<R: ResourceList>(aero: &Aero<R>) -> eyre::Result<()>;
} }
impl ConstructibleResourceList for HNil { impl ConstructibleResourceList for HNil {
fn construct<R: ResourceList>(_aero: &Aero<R>) -> anyhow::Result<()> { fn construct<R: ResourceList>(_aero: &Aero<R>) -> eyre::Result<()> {
Ok(()) Ok(())
} }
} }
@ -106,7 +106,7 @@ impl ConstructibleResourceList for HNil {
impl<H: ConstructibleResource, T: ConstructibleResourceList> ConstructibleResourceList impl<H: ConstructibleResource, T: ConstructibleResourceList> ConstructibleResourceList
for HCons<H, T> for HCons<H, T>
{ {
fn construct<R: ResourceList>(aero: &Aero<R>) -> anyhow::Result<()> { fn construct<R: ResourceList>(aero: &Aero<R>) -> eyre::Result<()> {
aero.try_init::<H>().map_err(Into::into)?; aero.try_init::<H>().map_err(Into::into)?;
T::construct(aero) T::construct(aero)
} }
@ -175,7 +175,7 @@ impl<R: ResourceList> Aero<R> {
/// Convert into a different variant of the Aero type. Any missing required resources /// Convert into a different variant of the Aero type. Any missing required resources
/// will be automatically constructed. /// will be automatically constructed.
pub fn try_construct_remaining<R2, I>(self) -> anyhow::Result<Aero<R2>> pub fn try_construct_remaining<R2, I>(self) -> eyre::Result<Aero<R2>>
where where
R2: Sculptor<R, I> + ResourceList, R2: Sculptor<R, I> + ResourceList,
<R2 as Sculptor<R, I>>::Remainder: ConstructibleResourceList, <R2 as Sculptor<R, I>>::Remainder: ConstructibleResourceList,