Skip to main content

dope_core/executor/session/
error.rs

1use std::convert::Infallible;
2
3use crate::Errno;
4
5use crate::executor::{InstallError, drive, session};
6use crate::topology::lending;
7
8#[derive(Debug)]
9pub struct Shutdown {
10    pub drive: drive::Error,
11    pub disarm: Option<Errno>,
12}
13
14impl Shutdown {
15    pub fn cause<X>(&self) -> session::Cause<'_, X> {
16        session::Cause::Quiesce {
17            drive: &self.drive,
18            disarm: self.disarm,
19        }
20    }
21
22    pub(super) fn after(drive: Option<drive::Error>, disarm: Option<Errno>) -> Option<Self> {
23        match drive {
24            Some(drive) => Some(Self { drive, disarm }),
25            None => disarm.map(|errno| Self {
26                drive: drive::Error::TimerDisarm(errno),
27                disarm: None,
28            }),
29        }
30    }
31}
32
33pub trait Sealed {}
34impl Sealed for () {}
35impl Sealed for Shutdown {}
36
37pub trait Returned: Sealed + Sized {
38    type Value<R>;
39    type Retained<R>;
40
41    fn closed<R, X>(output: R, shutdown: Option<Shutdown>) -> session::Outcome<R, X, Self>;
42}
43
44impl Returned for () {
45    type Value<R> = R;
46    type Retained<R> = R;
47
48    fn closed<R, X>(output: R, shutdown: Option<Shutdown>) -> session::Outcome<R, X, Self> {
49        match shutdown {
50            Some(Shutdown { drive, disarm }) => Err(Error::Quiesce {
51                output,
52                drive,
53                disarm,
54            }),
55            None => Ok(output),
56        }
57    }
58}
59
60impl Returned for Shutdown {
61    type Value<R> = (R, Option<Self>);
62    type Retained<R> = Infallible;
63
64    fn closed<R, X>(output: R, shutdown: Option<Self>) -> session::Outcome<R, X, Self> {
65        Ok((output, shutdown))
66    }
67}
68
69/// Application output and the failure observed while releasing its session.
70#[derive(Debug)]
71pub enum Error<T, E = Infallible> {
72    Drive(drive::Error),
73    Quiesce {
74        output: T,
75        drive: drive::Error,
76        disarm: Option<Errno>,
77    },
78    Install {
79        cause: InstallError<E>,
80        disarm: Option<Errno>,
81    },
82}
83
84impl<T, E> Error<T, E> {
85    /// Borrows the application result retained through a later session failure.
86    pub fn output(&self) -> Option<&T> {
87        match self {
88            Self::Quiesce { output, .. } => Some(output),
89            Self::Drive(_) | Self::Install { .. } => None,
90        }
91    }
92
93    /// Borrows the failure without moving or formatting application output.
94    pub fn cause(&self) -> session::Cause<'_, E> {
95        match self {
96            Self::Drive(error) => session::Cause::Drive(error),
97            Self::Quiesce { drive, disarm, .. } => session::Cause::Quiesce {
98                drive,
99                disarm: *disarm,
100            },
101            Self::Install { cause, disarm } => session::Cause::Install {
102                cause,
103                disarm: *disarm,
104            },
105        }
106    }
107}
108
109impl<T, E> From<drive::Error> for Error<T, E> {
110    fn from(error: drive::Error) -> Self {
111        Self::Drive(error)
112    }
113}
114
115impl<T, X> From<lending::Error<Infallible, T, X>> for Error<T, X> {
116    fn from(error: lending::Error<Infallible, T, X>) -> Self {
117        match error {
118            lending::Error::Run(error) => error,
119            lending::Error::Build(never) => match never {},
120        }
121    }
122}