use std::convert::Infallible;
use crate::Errno;
use crate::executor::{InstallError, drive, session};
use crate::topology::lending;
#[derive(Debug)]
pub struct Shutdown {
pub drive: drive::Error,
pub disarm: Option<Errno>,
}
impl Shutdown {
pub fn cause<X>(&self) -> session::Cause<'_, X> {
session::Cause::Quiesce {
drive: &self.drive,
disarm: self.disarm,
}
}
pub(super) fn after(drive: Option<drive::Error>, disarm: Option<Errno>) -> Option<Self> {
match drive {
Some(drive) => Some(Self { drive, disarm }),
None => disarm.map(|errno| Self {
drive: drive::Error::TimerDisarm(errno),
disarm: None,
}),
}
}
}
pub trait Sealed {}
impl Sealed for () {}
impl Sealed for Shutdown {}
pub trait Returned: Sealed + Sized {
type Value<R>;
type Retained<R>;
fn closed<R, X>(output: R, shutdown: Option<Shutdown>) -> session::Outcome<R, X, Self>;
}
impl Returned for () {
type Value<R> = R;
type Retained<R> = R;
fn closed<R, X>(output: R, shutdown: Option<Shutdown>) -> session::Outcome<R, X, Self> {
match shutdown {
Some(Shutdown { drive, disarm }) => Err(Error::Quiesce {
output,
drive,
disarm,
}),
None => Ok(output),
}
}
}
impl Returned for Shutdown {
type Value<R> = (R, Option<Self>);
type Retained<R> = Infallible;
fn closed<R, X>(output: R, shutdown: Option<Self>) -> session::Outcome<R, X, Self> {
Ok((output, shutdown))
}
}
#[derive(Debug)]
pub enum Error<T, E = Infallible> {
Drive(drive::Error),
Quiesce {
output: T,
drive: drive::Error,
disarm: Option<Errno>,
},
Install {
cause: InstallError<E>,
disarm: Option<Errno>,
},
}
impl<T, E> Error<T, E> {
pub fn output(&self) -> Option<&T> {
match self {
Self::Quiesce { output, .. } => Some(output),
Self::Drive(_) | Self::Install { .. } => None,
}
}
pub fn cause(&self) -> session::Cause<'_, E> {
match self {
Self::Drive(error) => session::Cause::Drive(error),
Self::Quiesce { drive, disarm, .. } => session::Cause::Quiesce {
drive,
disarm: *disarm,
},
Self::Install { cause, disarm } => session::Cause::Install {
cause,
disarm: *disarm,
},
}
}
}
impl<T, E> From<drive::Error> for Error<T, E> {
fn from(error: drive::Error) -> Self {
Self::Drive(error)
}
}
impl<T, X> From<lending::Error<Infallible, T, X>> for Error<T, X> {
fn from(error: lending::Error<Infallible, T, X>) -> Self {
match error {
lending::Error::Run(error) => error,
lending::Error::Build(never) => match never {},
}
}
}