Skip to main content

dope_core/client/
provider.rs

1use crate::driver::{
2    life::domain,
3    settings,
4    settings::{claim, errors},
5};
6use crate::executor::{self, session};
7use crate::{driver, topology::lending};
8use std::convert::Infallible;
9
10pub trait Setup: settings::Resource + Sized {
11    type Profile: settings::Profile;
12    type Error;
13    type NativeError;
14    type Pending<'d>: 'd
15    where
16        Self: 'd;
17    type Storage<'d>: Default
18    where
19        Self: 'd;
20    type Port<'storage, 'd>: Bind<'d, Self::Pending<'d>, Error = Self::NativeError> + 'storage
21    where
22        Self: 'd,
23        'd: 'storage;
24
25    fn config(&self) -> Result<settings::Config, errors::Claim> {
26        claim::Needs::from_tree(self)?.apply_to(settings::Config::for_profile::<Self::Profile>())
27    }
28
29    fn prepare<'storage, 'd: 'storage>(
30        self,
31        storage: &'storage mut Self::Storage<'d>,
32        driver: &mut driver::Local<'d>,
33    ) -> Result<(Self::Port<'storage, 'd>, Self::Pending<'d>), Self::Error>
34    where
35        Self: 'd;
36}
37
38pub trait Bind<'d, Pending: 'd> {
39    type Error;
40    type Root<'port>: lending::Root<'d> + lending::raw::Resume<'d>
41    where
42        Self: 'port,
43        'd: 'port;
44
45    fn bind<'port>(
46        &'port self,
47        pending: Pending,
48        driver: &mut driver::Local<'d>,
49    ) -> Result<Self::Root<'port>, Self::Error>
50    where
51        'd: 'port;
52}
53
54#[derive(Debug)]
55pub enum Build<E, N> {
56    Prepare(E),
57    Native(N),
58}
59
60pub type Run<B, T, E, X> = session::LoanResult<B, Result<T, E>, X>;
61
62#[derive(Debug)]
63pub enum Cause<'a, B, E, X> {
64    Build(&'a B),
65    Create(executor::Kind<'a>),
66    Output(&'a E),
67    Session(session::Cause<'a, X>),
68    Close {
69        failure: domain::Unmap,
70        retained: Result<Result<(), &'a E>, crate::BranchError<&'a B, session::Cause<'a, X>>>,
71    },
72}
73
74pub type Finished<'a, B, T, E = executor::drive::Error, X = Infallible> =
75    Result<T, Diagnostic<'a, B, T, E, X>>;
76
77pub struct Diagnostic<'a, B, T, E, X> {
78    pub cause: Cause<'a, B, E, X>,
79    pub output: Option<Result<&'a T, &'a E>>,
80}
81
82impl<'a, B, T, E, X> Diagnostic<'a, B, T, E, X> {
83    pub fn build(cause: &'a B) -> Self {
84        Self {
85            cause: Cause::Build(cause),
86            output: None,
87        }
88    }
89
90    pub fn create(cause: &'a executor::Error<'_>) -> Self {
91        Self {
92            cause: Cause::Create(cause.kind()),
93            output: None,
94        }
95    }
96}
97
98pub fn finish<B, T, E, X, Report, R>(
99    report: &mut Report,
100    observation: domain::Observation<'_, Run<B, T, E, X>>,
101    finish: impl FnOnce(&mut Report, Finished<'_, B, T, E, X>) -> R,
102) -> R {
103    let diagnostic = match observation {
104        Ok(Ok((Ok(value), None))) => return finish(report, Ok(value)),
105        Ok(Ok((Err(ref cause), None))) => Diagnostic {
106            cause: Cause::Output(cause),
107            output: Some(Err(cause)),
108        },
109        Ok(Ok((ref output, Some(ref shutdown)))) => Diagnostic {
110            cause: Cause::Session(shutdown.cause()),
111            output: Some(output.as_ref()),
112        },
113        Ok(Err(lending::Error::Build(ref cause))) => Diagnostic::build(cause),
114        Ok(Err(lending::Error::Run(ref cause))) => Diagnostic {
115            cause: Cause::Session(cause.cause()),
116            output: None,
117        },
118        Err((failure, prior)) => {
119            let (retained, output) = match prior {
120                Ok((output, shutdown)) => {
121                    let output = output.as_ref();
122                    let retained = match shutdown {
123                        None => Ok(output.map(|_| ())),
124                        Some(shutdown) => Err(crate::BranchError::Second(shutdown.cause())),
125                    };
126                    (retained, Some(output))
127                }
128                Err(lending::Error::Build(cause)) => (Err(crate::BranchError::First(cause)), None),
129                Err(lending::Error::Run(cause)) => {
130                    (Err(crate::BranchError::Second(cause.cause())), None)
131                }
132            };
133            Diagnostic {
134                cause: Cause::Close { failure, retained },
135                output,
136            }
137        }
138    };
139    finish(report, Err(diagnostic))
140}