dope-core 0.76.0

Owned native I/O and completion driver for dope
Documentation
use crate::driver::{
    life::domain,
    settings,
    settings::{claim, errors},
};
use crate::executor::{self, session};
use crate::{driver, topology::lending};
use std::convert::Infallible;

pub trait Setup: settings::Resource + Sized {
    type Profile: settings::Profile;
    type Error;
    type NativeError;
    type Pending<'d>: 'd
    where
        Self: 'd;
    type Storage<'d>: Default
    where
        Self: 'd;
    type Port<'storage, 'd>: Bind<'d, Self::Pending<'d>, Error = Self::NativeError> + 'storage
    where
        Self: 'd,
        'd: 'storage;

    fn config(&self) -> Result<settings::Config, errors::Claim> {
        claim::Needs::from_tree(self)?.apply_to(settings::Config::for_profile::<Self::Profile>())
    }

    fn prepare<'storage, 'd: 'storage>(
        self,
        storage: &'storage mut Self::Storage<'d>,
        driver: &mut driver::Local<'d>,
    ) -> Result<(Self::Port<'storage, 'd>, Self::Pending<'d>), Self::Error>
    where
        Self: 'd;
}

pub trait Bind<'d, Pending: 'd> {
    type Error;
    type Root<'port>: lending::Root<'d> + lending::raw::Resume<'d>
    where
        Self: 'port,
        'd: 'port;

    fn bind<'port>(
        &'port self,
        pending: Pending,
        driver: &mut driver::Local<'d>,
    ) -> Result<Self::Root<'port>, Self::Error>
    where
        'd: 'port;
}

#[derive(Debug)]
pub enum Build<E, N> {
    Prepare(E),
    Native(N),
}

pub type Run<B, T, E, X> = session::LoanResult<B, Result<T, E>, X>;

#[derive(Debug)]
pub enum Cause<'a, B, E, X> {
    Build(&'a B),
    Create(executor::Kind<'a>),
    Output(&'a E),
    Session(session::Cause<'a, X>),
    Close {
        failure: domain::Unmap,
        retained: Result<Result<(), &'a E>, crate::BranchError<&'a B, session::Cause<'a, X>>>,
    },
}

pub type Finished<'a, B, T, E = executor::drive::Error, X = Infallible> =
    Result<T, Diagnostic<'a, B, T, E, X>>;

pub struct Diagnostic<'a, B, T, E, X> {
    pub cause: Cause<'a, B, E, X>,
    pub output: Option<Result<&'a T, &'a E>>,
}

impl<'a, B, T, E, X> Diagnostic<'a, B, T, E, X> {
    pub fn build(cause: &'a B) -> Self {
        Self {
            cause: Cause::Build(cause),
            output: None,
        }
    }

    pub fn create(cause: &'a executor::Error<'_>) -> Self {
        Self {
            cause: Cause::Create(cause.kind()),
            output: None,
        }
    }
}

pub fn finish<B, T, E, X, Report, R>(
    report: &mut Report,
    observation: domain::Observation<'_, Run<B, T, E, X>>,
    finish: impl FnOnce(&mut Report, Finished<'_, B, T, E, X>) -> R,
) -> R {
    let diagnostic = match observation {
        Ok(Ok((Ok(value), None))) => return finish(report, Ok(value)),
        Ok(Ok((Err(ref cause), None))) => Diagnostic {
            cause: Cause::Output(cause),
            output: Some(Err(cause)),
        },
        Ok(Ok((ref output, Some(ref shutdown)))) => Diagnostic {
            cause: Cause::Session(shutdown.cause()),
            output: Some(output.as_ref()),
        },
        Ok(Err(lending::Error::Build(ref cause))) => Diagnostic::build(cause),
        Ok(Err(lending::Error::Run(ref cause))) => Diagnostic {
            cause: Cause::Session(cause.cause()),
            output: None,
        },
        Err((failure, prior)) => {
            let (retained, output) = match prior {
                Ok((output, shutdown)) => {
                    let output = output.as_ref();
                    let retained = match shutdown {
                        None => Ok(output.map(|_| ())),
                        Some(shutdown) => Err(crate::BranchError::Second(shutdown.cause())),
                    };
                    (retained, Some(output))
                }
                Err(lending::Error::Build(cause)) => (Err(crate::BranchError::First(cause)), None),
                Err(lending::Error::Run(cause)) => {
                    (Err(crate::BranchError::Second(cause.cause())), None)
                }
            };
            Diagnostic {
                cause: Cause::Close { failure, retained },
                output,
            }
        }
    };
    finish(report, Err(diagnostic))
}