use std::error::Error;
use std::fmt;
use std::future::Future;
use crate::dependency::Dependencies;
use crate::error::LookupError;
use crate::hooks::Hooks;
use crate::resolver::Resolver;
use crate::scope::Scope;
use crate::timer::{BoxError, Bound};
#[diagnostic::on_unimplemented(
message = "`{Self}` is not a type the container can construct",
label = "the container cannot build this",
note = "add #[injectable] to the type, or bind it with a factory"
)]
pub trait Construct: Sized + Send + Sync + 'static {
type Scope: Scope;
const CONSTRUCT_TIMEOUT: Bound = Bound::Default;
fn dependencies(d: &mut Dependencies);
fn construct(r: &Resolver<'_>) -> impl Future<Output = Result<Self, ConstructError>> + Send;
fn hooks(_h: &mut Hooks<Self>) {}
}
#[non_exhaustive]
pub enum ConstructError {
Dependency(LookupError),
Failed(BoxError),
}
impl ConstructError {
pub fn failed(e: impl Into<BoxError>) -> Self {
ConstructError::Failed(e.into())
}
}
impl From<LookupError> for ConstructError {
fn from(e: LookupError) -> Self {
ConstructError::Dependency(e)
}
}
impl fmt::Debug for ConstructError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ConstructError::Dependency(e) => f.debug_tuple("Dependency").field(e).finish(),
ConstructError::Failed(e) => f.debug_tuple("Failed").field(e).finish(),
}
}
}
impl fmt::Display for ConstructError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ConstructError::Dependency(e) => fmt::Display::fmt(e, f),
ConstructError::Failed(e) => fmt::Display::fmt(e, f),
}
}
}
impl Error for ConstructError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
ConstructError::Dependency(e) => e.source(),
ConstructError::Failed(e) => e.source(),
}
}
}