anthill_di/types/
result.rs

1use crate::LifeCycle;
2
3use super::TypeInfo;
4
5use thiserror::Error;
6
7pub type BuildDependencyResult<T> = Result<T, BuildDependencyError>;
8
9#[derive(Debug, Error)]
10pub enum BuildDependencyError {
11    #[error("Service not found {type_info:?}")]
12    NotFound { type_info: TypeInfo },
13    #[error("Service [{child_type_info:?}] resolve service [{parent_type_info:?}], which before resolve this service. If you app required cycled reference, remove loop-check feature")]
14    CyclicReference { child_type_info: TypeInfo, parent_type_info: TypeInfo },
15    #[error("Map component error. Idk how [{err:?}]")]
16    MapComponentError { err: MapComponentError },
17    #[error("Add component error. Probably you add service from ctr twice, or in other space and ctr second. Check service ctr [{err:?}]")]
18    AddDependencyError { err: AddDependencyError },
19    #[error("{err:?}")]
20    Custom { err: anyhow::Error }
21}
22
23// Required for anyhow::Error compare
24impl PartialEq for BuildDependencyError {
25    fn eq(&self, other: &Self) -> bool {
26        match (self, other) {
27            (Self::NotFound { type_info: l_type_info }, Self::NotFound { type_info: r_type_info }) => l_type_info == r_type_info,
28            (Self::CyclicReference { child_type_info: l_child_type_info, parent_type_info: l_parent_type_info }, Self::CyclicReference { child_type_info: r_child_type_info, parent_type_info: r_parent_type_info }) => l_child_type_info == r_child_type_info && l_parent_type_info == r_parent_type_info,
29            (Self::MapComponentError { err: l_err }, Self::MapComponentError { err: r_err }) => l_err == r_err,
30            (Self::AddDependencyError { err: l_err }, Self::AddDependencyError { err: r_err }) => l_err == r_err,
31            (Self::Custom { err: _ }, Self::Custom { err: _ }) => true,
32            _ => false,
33        }
34    }
35}
36
37pub type AddDependencyResult<T> = Result<T, AddDependencyError>;
38
39#[derive(Debug, PartialEq, Error)]
40pub enum AddDependencyError {
41    #[error("Add component [{component_type_info:?}] error, component exist")]
42    DependencyExist { component_type_info: TypeInfo, },   
43}
44
45pub type MapComponentResult<T> = Result<T, MapComponentError>;
46
47#[derive(Debug, PartialEq, Error)]
48pub enum MapComponentError {
49    #[error("Map component [{component_type_info:?}] to service [{service_type_info:?}] error, component not found")]
50    ComponentNotFound { component_type_info: TypeInfo, service_type_info: TypeInfo },
51}
52
53pub type DeleteComponentResult<T> = Result<T, DeleteComponentError>;
54
55#[derive(Debug, PartialEq, Error)]
56pub enum DeleteComponentError {
57    #[error("Delete component [{component_type_info:?}] error, component not found")]
58    ComponentNotFound { component_type_info: TypeInfo },
59    #[error("Delete component [{component_type_info:?}] with life cycle [{life_cycle:?}] error, life cycle not support delete")]
60    NotSupportedLifeCycle { component_type_info: TypeInfo, life_cycle: LifeCycle },
61}