1use std::{mem, pin};
3
4use dope_core::{
5 driver,
6 driver::{
7 life::domain::{Closing, Error, Rejected},
8 settings,
9 settings::{claim, errors},
10 storage,
11 },
12};
13
14use crate::{
15 executor::{self, create, drive, session},
16 random::{self, HashState},
17};
18
19pub mod app;
20mod domain;
21pub mod error;
22pub mod lending;
23pub mod ops;
24mod prepared;
25pub mod recipe;
26mod worker;
27
28pub use domain::Domain;
29pub use prepared::Prepared;
30pub use worker::Worker;
31
32#[repr(transparent)]
36pub struct Boot<'stage, 'scope, 'life: 'scope, 'p: 'life, 'd: 'p, S: storage::Plan + 'd, Q> {
37 session: &'stage session::Scope<'scope, 'life, 'p, 'd, S, Q>,
38}
39
40impl<'stage, 'scope, 'life: 'scope, 'p: 'life, 'd: 'p, S: storage::Plan + 'd, Q>
41 Boot<'stage, 'scope, 'life, 'p, 'd, S, Q>
42{
43 fn new(session: &'stage session::Scope<'scope, 'life, 'p, 'd, S, Q>) -> Self {
44 Self { session }
45 }
46
47 pub fn pinned(&self) -> pin::Pin<&'life S::Output<'p, 'd>> {
49 self.session.pinned()
50 }
51
52 pub fn storage(&self) -> &'life S::Output<'p, 'd> {
54 self.session.storage()
55 }
56}
57
58pub struct Stage<'stage, 'scope, 'life: 'scope, 'p: 'life, 'd: 'p, S: storage::Plan + 'd, Q> {
63 session: &'stage mut session::Scope<'scope, 'life, 'p, 'd, S, Q>,
64}
65
66impl<'stage, 'scope, 'life: 'scope, 'p: 'life, 'd: 'p, S: storage::Plan + 'd, Q>
67 Stage<'stage, 'scope, 'life, 'p, 'd, S, Q>
68{
69 fn new(session: &'stage mut session::Scope<'scope, 'life, 'p, 'd, S, Q>) -> Self {
70 Self { session }
71 }
72
73 pub fn storage(&self) -> &'life S::Output<'p, 'd> {
75 self.session.storage()
76 }
77
78 pub fn hash_state(&mut self, domain: random::Domain) -> HashState<'d> {
81 self.session.hash_state(domain)
82 }
83
84 pub fn driver(&mut self) -> driver::Context<'_, 'd> {
86 self.session.driver()
87 }
88}
89
90pub trait Installer<S: storage::Plan>: Sized {
92 type App<'life, 'p, 'd>: executor::raw::App<'d>
93 where
94 'd: 'p,
95 'p: 'life,
96 'd: 'life,
97 Self: 'd,
98 S: 'd;
99 type Error;
100
101 fn install<'scope, 'life, 'p, 'd, Q>(
102 self,
103 stage: &mut Stage<'_, 'scope, 'life, 'p, 'd, S, Q>,
104 ) -> Result<Self::App<'life, 'p, 'd>, Self::Error>
105 where
106 'd: 'p,
107 'p: 'life,
108 'd: 'life,
109 'life: 'scope,
110 Self: 'd,
111 S: 'd,
112 Q: executor::Startup;
113}
114
115pub enum EnterError<'owner, S, O, T, Q = ()> {
117 Runtime(Rejected<create::Error<'owner>, O>),
118 Storage(Rejected<S, O>),
119 Close(Closing<'owner, Rejected<S, O>, T, Q>),
120}
121
122impl<'owner, S, O, T, Q> EnterError<'owner, S, O, T, Q> {
123 pub fn output(&self) -> Option<&T> {
125 match self {
126 Self::Runtime(..) | Self::Storage(..) => None,
127 Self::Close(state) => state.result().as_ref().ok(),
128 }
129 }
130
131 pub fn cause(&self) -> error::Cause<'_, S> {
133 match self {
134 Self::Runtime(error) => error::Cause::Runtime(error.cause.kind()),
135 Self::Storage(error) => error::Cause::Storage(&error.cause),
136 Self::Close(state) => error::Cause::closing(state),
137 }
138 }
139
140 pub fn kind(&self) -> error::Kind<'_, S, &T> {
141 match self {
142 Self::Runtime(error) => error::Kind::Runtime(error.cause.kind()),
143 Self::Storage(error) => error::Kind::Storage(&error.cause),
144 Self::Close(state) => error::Kind::closing(state),
145 }
146 }
147}
148
149#[derive(Debug)]
150pub enum InstallError<E> {
151 Drive(drive::Error),
152 Install(E),
153}
154
155#[derive(Debug)]
156pub enum Failure<'report, I, T, E = drive::Error> {
157 Run(session::Error<'report, Result<T, E>>),
158 Execute(E),
159 Install(I),
160}
161
162impl<'report, I, T, E> Failure<'report, I, T, E> {
163 pub fn map_install<J>(self, map: impl FnOnce(I) -> J) -> Failure<'report, J, T, E> {
164 match self {
165 Self::Run(error) => Failure::Run(error),
166 Self::Execute(error) => Failure::Execute(error),
167 Self::Install(error) => Failure::Install(map(error)),
168 }
169 }
170
171 pub fn cause(&self) -> error::Failure<'_, I, E> {
173 match self {
174 Self::Run(error) => error::Failure::Run {
175 cause: error.cause(),
176 output: match error.output() {
177 Some(Err(error)) => Some(error),
178 Some(Ok(_)) | None => None,
179 },
180 },
181 Self::Execute(error) => error::Failure::Execute(error),
182 Self::Install(error) => error::Failure::Install(error),
183 }
184 }
185}
186
187pub enum RunError<'owner, S, I, O, T, Q = (), E = drive::Error> {
188 Create(Rejected<create::Error<'owner>, O>),
189 Storage(Rejected<S, O>),
190 Failure(Failure<'owner, I, T, E>),
191 Close(ClosingRun<'owner, S, I, O, T, Q, E>),
192}
193
194impl<'owner, S, I, O, T, Q, E> RunError<'owner, S, I, O, T, Q, E> {
195 pub fn cause(&self) -> error::Run<'_, S, I, E> {
197 match self {
198 Self::Create(error) => error::Run::Runtime(error.cause.kind()),
199 Self::Storage(error) => error::Run::Storage(&error.cause),
200 Self::Failure(error) => error::Run::Failure(error.cause()),
201 Self::Close(state) => error::Run::Close {
202 result: match state.result() {
203 Ok(Ok(_)) => Ok(Ok(())),
204 Ok(Err(error)) => Ok(Err(error.cause())),
205 Err(error) => Err(&error.cause),
206 },
207 failure: state.failure(),
208 },
209 }
210 }
211
212 pub fn kind(&self) -> RunKind<'_, 'owner, S, I, T, E> {
213 match self {
214 Self::Create(error) => error::Kind::Runtime(error.cause.kind()),
215 Self::Storage(error) => error::Kind::Storage(&error.cause),
216 Self::Failure(error) => error::Kind::Run(error),
217 Self::Close(state) => error::Kind::closing(state),
218 }
219 }
220}
221
222impl<'owner, S, O, T, Q> From<Error<'owner, Rejected<S, O>, T, Q>>
223 for EnterError<'owner, S, O, T, Q>
224{
225 fn from(error: Error<'owner, Rejected<S, O>, T, Q>) -> Self {
226 match error {
227 Error::Storage(source) => Self::Storage(source),
228 Error::Close(state) => Self::Close(state),
229 }
230 }
231}
232
233impl<'owner, S, I, O, T, Q, E>
234 From<EnterError<'owner, S, O, Result<T, Failure<'owner, I, T, E>>, Q>>
235 for RunError<'owner, S, I, O, T, Q, E>
236{
237 fn from(error: EnterError<'owner, S, O, Result<T, Failure<'owner, I, T, E>>, Q>) -> Self {
238 match error {
239 EnterError::Runtime(source) => Self::Create(source),
240 EnterError::Storage(source) => Self::Storage(source),
241 EnterError::Close(state) => Self::Close(state),
242 }
243 }
244}
245
246const _: () = {
247 assert!(
248 mem::size_of::<Boot<'static, 'static, 'static, 'static, 'static, (), ()>>()
249 == mem::size_of::<usize>()
250 );
251 assert!(
252 mem::align_of::<Boot<'static, 'static, 'static, 'static, 'static, (), ()>>()
253 == mem::align_of::<usize>()
254 );
255 assert!(
256 mem::size_of::<Stage<'static, 'static, 'static, 'static, 'static, (), ()>>()
257 == mem::size_of::<&mut session::Scope<'static, 'static, 'static, 'static, (), ()>>()
258 );
259};
260
261#[must_use = "a topology plan must be resolved before it can enter a driver"]
266#[repr(transparent)]
267pub struct Plan<S, I>((S, I));
268
269impl<S, I> Plan<S, I> {
270 pub const fn new(storage: S, install: I) -> Self {
271 Self((storage, install))
272 }
273
274 pub fn into_parts(self) -> (S, I) {
276 self.0
277 }
278
279 pub fn for_profile<P>(self) -> Result<Prepared<S, I>, errors::Claim>
280 where
281 P: settings::Profile,
282 S: storage::Plan + settings::Resource,
283 I: settings::Resource,
284 {
285 let (storage, install) = self.0;
286 let needs = claim::Needs::from_tree(&(&storage, &install))?;
287 let layout = needs.resolve::<P>()?;
288 Ok(Prepared::new(layout, storage, install))
289 }
290
291 pub fn with_config(self, baseline: settings::Config) -> Result<Prepared<S, I>, errors::Claim>
292 where
293 S: storage::Plan + settings::Resource,
294 I: settings::Resource,
295 {
296 let (storage, install) = self.0;
297 let needs = claim::Needs::from_tree(&(&storage, &install))?;
298 let layout = needs.apply_to(baseline)?;
299 Ok(Prepared::new(layout, storage, install))
300 }
301
302 pub fn for_receive<P>(self) -> Result<Prepared<S, I>, errors::Claim>
303 where
304 P: settings::Profile,
305 S: storage::Plan + settings::Resource,
306 I: settings::Resource,
307 {
308 let (storage, install) = self.0;
309 let needs = claim::Needs::from_tree(&(&storage, &install))?;
310 let layout = needs.exact::<P>()?;
311 Ok(Prepared::new(layout, storage, install))
312 }
313
314 pub fn for_install<P>(self) -> Result<Prepared<S, I>, errors::Claim>
315 where
316 P: settings::Profile,
317 S: storage::Claimless,
318 I: settings::Resource,
319 {
320 let (storage, install) = self.0;
321 let needs = claim::Needs::from_tree(&install)?;
322 let layout = needs.resolve::<P>()?;
323 Ok(Prepared::new(layout, storage, install))
324 }
325
326 pub fn with_install(self, baseline: settings::Config) -> Result<Prepared<S, I>, errors::Claim>
327 where
328 S: storage::Claimless,
329 I: settings::Resource,
330 {
331 let (storage, install) = self.0;
332 let needs = claim::Needs::from_tree(&install)?;
333 let layout = needs.apply_to(baseline)?;
334 Ok(Prepared::new(layout, storage, install))
335 }
336}
337
338const _: () = {
339 assert!(mem::size_of::<Plan<usize, u8>>() == mem::size_of::<(usize, u8)>());
340 assert!(mem::align_of::<Plan<usize, u8>>() == mem::align_of::<(usize, u8)>());
341 assert!(mem::needs_drop::<Plan<String, ()>>() == mem::needs_drop::<String>());
342};
343
344type ClosingRun<'owner, S, I, O, T, Q, E> =
345 Closing<'owner, Rejected<S, O>, Result<T, Failure<'owner, I, T, E>>, Q>;
346type RunKind<'a, 'owner, S, I, T, E> =
347 error::Kind<'a, S, &'a Result<T, Failure<'owner, I, T, E>>, Failure<'owner, I, T, E>>;