Allow anyhow::Error as Error type

This commit is contained in:
Diggory Blake 2023-07-02 15:33:45 +01:00
parent 489b5d04d8
commit 079fbb4654
No known key found for this signature in database
GPG key ID: E6BDFA83146ABD40
4 changed files with 12 additions and 15 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "aerosol" name = "aerosol"
version = "1.0.0-alpha.3" version = "1.0.0-alpha.4"
authors = ["Diggory Blake <diggsey@googlemail.com>"] authors = ["Diggory Blake <diggsey@googlemail.com>"]
edition = "2018" edition = "2018"
description = "Simple dependency injection for Rust" description = "Simple dependency injection for Rust"
@ -13,7 +13,7 @@ all-features = true
[features] [features]
default = [] default = []
async = ["async-trait"] async = ["async-trait"]
axum = ["dep:axum", "async", "tracing", "thiserror", "anyhow"] axum = ["dep:axum", "async", "tracing", "thiserror"]
[dependencies] [dependencies]
parking_lot = "0.12.1" parking_lot = "0.12.1"
@ -22,7 +22,7 @@ async-trait = { version = "0.1", optional = true }
axum = { version = "0.6", optional = true } axum = { version = "0.6", optional = true }
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", optional = true } anyhow = { version = "1.0" }
[dev-dependencies] [dev-dependencies]
tokio = { version = "1.0", features = ["macros"] } tokio = { version = "1.0", features = ["macros"] }

View file

@ -1,4 +1,4 @@
use std::{error::Error, sync::Arc}; use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
@ -14,7 +14,7 @@ use crate::{
#[async_trait] #[async_trait]
pub trait AsyncConstructible: Sized { pub trait AsyncConstructible: Sized {
/// Error type for when resource fails to be constructed. /// Error type for when resource fails to be constructed.
type Error: Error + Send + Sync; type Error: Into<anyhow::Error> + Send + Sync;
/// Construct the resource with the provided application state. /// Construct the resource with the provided application state.
async fn construct_async(aero: &Aerosol) -> Result<Self, Self::Error>; async fn construct_async(aero: &Aerosol) -> Result<Self, Self::Error>;
} }
@ -32,7 +32,7 @@ impl<T: Constructible> AsyncConstructible for T {
#[async_trait] #[async_trait]
pub trait IndirectlyAsyncConstructible: Sized { pub trait IndirectlyAsyncConstructible: Sized {
/// Error type for when resource fails to be constructed. /// Error type for when resource fails to be constructed.
type Error: Error + Send + Sync; type Error: Into<anyhow::Error> + Send + Sync;
/// Construct the resource with the provided application state. /// Construct the resource with the provided application state.
async fn construct_async(aero: &Aerosol) -> Result<Self, Self::Error>; async fn construct_async(aero: &Aerosol) -> Result<Self, Self::Error>;
} }

View file

@ -1,7 +1,4 @@
use std::{ use std::any::{type_name, Any};
any::{type_name, Any},
error::Error,
};
/// Bound on the types that can be used as an aerosol resource. /// Bound on the types that can be used as an aerosol resource.
pub trait Resource: Any + Send + Sync + Clone {} pub trait Resource: Any + Send + Sync + Clone {}
@ -15,10 +12,10 @@ pub(crate) fn unwrap_resource<T: Resource>(opt: Option<T>) -> T {
} }
} }
pub(crate) fn unwrap_constructed<T: Resource, U>(res: Result<U, impl Error>) -> U { pub(crate) fn unwrap_constructed<T: Resource, U>(res: Result<U, impl Into<anyhow::Error>>) -> U {
match res { match res {
Ok(x) => x, Ok(x) => x,
Err(e) => panic!("Failed to construct `{}`: {}", type_name::<T>(), e), Err(e) => panic!("Failed to construct `{}`: {}", type_name::<T>(), e.into()),
} }
} }

View file

@ -1,4 +1,4 @@
use std::{error::Error, sync::Arc}; use std::sync::Arc;
use crate::{ use crate::{
resource::{unwrap_constructed, Resource}, resource::{unwrap_constructed, Resource},
@ -9,7 +9,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 { pub trait Constructible: Sized {
/// Error type for when resource fails to be constructed. /// Error type for when resource fails to be constructed.
type Error: Error + Send + Sync; type Error: Into<anyhow::Error> + Send + Sync;
/// Construct the resource with the provided application state. /// Construct the resource with the provided application state.
fn construct(aero: &Aerosol) -> Result<Self, Self::Error>; fn construct(aero: &Aerosol) -> Result<Self, Self::Error>;
} }
@ -17,7 +17,7 @@ pub trait Constructible: Sized {
/// 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 { pub trait IndirectlyConstructible: Sized {
/// Error type for when resource fails to be constructed. /// Error type for when resource fails to be constructed.
type Error: Error + Send + Sync; type Error: Into<anyhow::Error> + Send + Sync;
/// Construct the resource with the provided application state. /// Construct the resource with the provided application state.
fn construct(aero: &Aerosol) -> Result<Self, Self::Error>; fn construct(aero: &Aerosol) -> Result<Self, Self::Error>;
} }