cgp_error/traits/
can_raise_error.rs

1use cgp_component::{DelegateComponent, HasCgpProvider, IsProviderFor, UseContext, UseDelegate};
2use cgp_macro::{cgp_component, cgp_provider};
3
4use crate::traits::has_error_type::HasErrorType;
5
6/**
7   Used for injecting external error types into [`Self::Error`](HasErrorType::Error).
8
9   As an example, if `Context: CanRaiseError<ParseIntError>`, then we would be
10   able to call `Context::raise_error(err)` for an error value
11   [`err: ParseIntError`](core::num::ParseIntError) and get back
12   a [`Context::Error`](HasErrorType::Error) value.
13*/
14#[cgp_component {
15    provider: ErrorRaiser
16}]
17pub trait CanRaiseError<SourceError>: HasErrorType {
18    fn raise_error(error: SourceError) -> Self::Error;
19}
20
21#[cgp_provider(ErrorRaiserComponent)]
22impl<Context, SourceError, Components, Delegate> ErrorRaiser<Context, SourceError>
23    for UseDelegate<Components>
24where
25    Context: HasErrorType,
26    Components: DelegateComponent<SourceError, Delegate = Delegate>,
27    Delegate: ErrorRaiser<Context, SourceError>,
28{
29    fn raise_error(e: SourceError) -> Context::Error {
30        Delegate::raise_error(e)
31    }
32}