refactor: merge the console into the live desk and shrink the desk trait to what screens differ in
This commit is contained in:
1 parent
734988247d
commit
f8f1b54d75
6 files changed
+298
-344
No files matched your search
@@ -1,6 +1,8 @@
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
io,
|
||||
iter::once,
|
||||
mem,
|
||||
ops::ControlFlow,
|
||||
time::Duration,
|
||||
};
|
||||
@@ -12,17 +14,16 @@ use zeroize::Zeroizing;
|
||||
|
||||
use crate::{
|
||||
boot_model::{
|
||||
ActiveState, Activity, BootState, JobKind, JobResult, Phase, UnitFacts, UnitTimeouts,
|
||||
boot_origin::BootOrigin,
|
||||
ActiveState, Activity, BootChange, BootState, JobKind, JobResult, Phase, UnitFacts,
|
||||
UnitTimeouts, boot_origin::BootOrigin,
|
||||
},
|
||||
boot_view::PasswordPrompt,
|
||||
console_session::{
|
||||
console_palette::ConsolePalette,
|
||||
passphrase_input,
|
||||
stream_sources::{frame_inputs, key_inputs},
|
||||
watch_screen::{ConsoleDesk, WatchEnding, WatchScreen, watch_until_ending},
|
||||
},
|
||||
screen_settings::{color_scheme::Colors, config_file::Timing, font_glyphs::Glyphs},
|
||||
screen_settings::{color_scheme::Colors, config_file::Timing},
|
||||
systemd_link::monotonic_clock::monotonic_now,
|
||||
};
|
||||
|
||||
@@ -178,6 +179,7 @@ impl ScriptedJob {
|
||||
}
|
||||
|
||||
struct Script {
|
||||
steps: &'static [Step],
|
||||
jobs: Vec<ScriptedJob>,
|
||||
started: HashMap<&'static str, Duration>,
|
||||
finished: HashSet<&'static str>,
|
||||
@@ -191,32 +193,39 @@ struct Answer {
|
||||
}
|
||||
|
||||
impl Script {
|
||||
fn begin(
|
||||
steps: &'static [Step],
|
||||
describe: fn(&str) -> Option<&'static str>,
|
||||
first_job: u32,
|
||||
state: &mut BootState,
|
||||
now: Duration,
|
||||
) -> Self {
|
||||
let jobs: Vec<ScriptedJob> = steps
|
||||
.iter()
|
||||
.zip(first_job..)
|
||||
.map(|(step, id)| ScriptedJob { id, step })
|
||||
.collect();
|
||||
state.set_root(steps[0].unit.to_owned());
|
||||
jobs.iter().for_each(|job| {
|
||||
state.track_unit(job.facts(steps, describe), job.initial_activity());
|
||||
});
|
||||
jobs.iter()
|
||||
.for_each(|job| state.queue_job(job.id, job.step.unit, job.kind(), now));
|
||||
fn begin(steps: &'static [Step], first_job: u32) -> Self {
|
||||
Self {
|
||||
jobs,
|
||||
steps,
|
||||
jobs: steps
|
||||
.iter()
|
||||
.zip(first_job..)
|
||||
.map(|(step, id)| ScriptedJob { id, step })
|
||||
.collect(),
|
||||
started: HashMap::new(),
|
||||
finished: HashSet::new(),
|
||||
answer: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn opening_changes(
|
||||
&self,
|
||||
describe: fn(&str) -> Option<&'static str>,
|
||||
now: Duration,
|
||||
) -> Vec<BootChange> {
|
||||
once(BootChange::SetRoot(self.steps[0].unit.to_owned()))
|
||||
.chain(self.jobs.iter().map(|job| BootChange::TrackUnit {
|
||||
facts: job.facts(self.steps, describe),
|
||||
activity: job.initial_activity(),
|
||||
}))
|
||||
.chain(self.jobs.iter().map(|job| BootChange::QueueJob {
|
||||
id: job.id,
|
||||
unit: job.step.unit.to_owned(),
|
||||
kind: job.kind(),
|
||||
at: now,
|
||||
}))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn asking(&self) -> Option<&'static Step> {
|
||||
self.jobs.iter().map(|job| job.step).find(|step| {
|
||||
step.asks.is_some()
|
||||
@@ -281,74 +290,92 @@ impl Script {
|
||||
!self.finished.contains(job.step.unit) && done_at.is_some_and(|done_at| now >= done_at)
|
||||
}
|
||||
|
||||
fn advance(&mut self, state: &mut BootState, now: Duration) {
|
||||
fn advance(&mut self, now: Duration) -> Vec<BootChange> {
|
||||
let starting: HashSet<&'static str> = self
|
||||
.jobs
|
||||
.iter()
|
||||
.filter(|job| self.is_ready(job))
|
||||
.map(|job| job.step.unit)
|
||||
.collect();
|
||||
self.jobs
|
||||
.iter()
|
||||
.filter(|job| starting.contains(job.step.unit))
|
||||
.for_each(|job| {
|
||||
self.started.insert(job.step.unit, now);
|
||||
state.update_activity(job.step.unit, job.started_activity(now));
|
||||
});
|
||||
self.started
|
||||
.extend(starting.iter().map(|unit| (*unit, now)));
|
||||
let due: HashSet<&'static str> = self
|
||||
.jobs
|
||||
.iter()
|
||||
.filter(|job| self.is_due(job, now))
|
||||
.map(|job| job.step.unit)
|
||||
.collect();
|
||||
self.jobs
|
||||
let changes = self
|
||||
.jobs
|
||||
.iter()
|
||||
.filter(|job| due.contains(job.step.unit))
|
||||
.for_each(|job| {
|
||||
state.update_activity(job.step.unit, job.settled_activity(now));
|
||||
state.finish_job(job.id, job.outcome(), now);
|
||||
});
|
||||
.filter(|job| starting.contains(job.step.unit))
|
||||
.map(|job| BootChange::UpdateActivity {
|
||||
unit: job.step.unit.to_owned(),
|
||||
activity: job.started_activity(now),
|
||||
})
|
||||
.chain(
|
||||
self.jobs
|
||||
.iter()
|
||||
.filter(|job| due.contains(job.step.unit))
|
||||
.flat_map(|job| {
|
||||
[
|
||||
BootChange::UpdateActivity {
|
||||
unit: job.step.unit.to_owned(),
|
||||
activity: job.settled_activity(now),
|
||||
},
|
||||
BootChange::FinishJob {
|
||||
id: job.id,
|
||||
result: job.outcome(),
|
||||
at: now,
|
||||
},
|
||||
]
|
||||
}),
|
||||
)
|
||||
.collect();
|
||||
self.finished.extend(due);
|
||||
changes
|
||||
}
|
||||
}
|
||||
|
||||
struct DemoDesk {
|
||||
script: Script,
|
||||
after_switch_root: Option<&'static [Step]>,
|
||||
describe: fn(&str) -> Option<&'static str>,
|
||||
next_stage: &'static [Step],
|
||||
settled_at: Option<Duration>,
|
||||
typed: Zeroizing<String>,
|
||||
glyphs: Glyphs,
|
||||
}
|
||||
|
||||
impl DemoDesk {
|
||||
fn enter_next_stage(&mut self, now: Duration) -> Vec<BootChange> {
|
||||
if !self.script.is_done() || self.next_stage.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
self.script = Script::begin(mem::take(&mut self.next_stage), 1000);
|
||||
once(BootChange::SwitchRoot { at: now })
|
||||
.chain(self.script.opening_changes(self.describe, now))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl ConsoleDesk for DemoDesk {
|
||||
fn frame_passed(&mut self, state: &mut BootState, now: Duration) -> ControlFlow<WatchEnding> {
|
||||
self.script.advance(state, now);
|
||||
if self.script.is_done()
|
||||
&& let Some(steps) = self.after_switch_root.take()
|
||||
{
|
||||
state.switch_root(now);
|
||||
self.script = Script::begin(steps, self.describe, 1000, state, now);
|
||||
}
|
||||
fn frame_passed(&mut self, now: Duration) -> ControlFlow<WatchEnding, Vec<BootChange>> {
|
||||
let changes: Vec<BootChange> = self
|
||||
.script
|
||||
.advance(now)
|
||||
.into_iter()
|
||||
.chain(self.enter_next_stage(now))
|
||||
.collect();
|
||||
if !self.script.is_done() {
|
||||
return ControlFlow::Continue(());
|
||||
return ControlFlow::Continue(changes);
|
||||
}
|
||||
let settled_at = *self.settled_at.get_or_insert(now);
|
||||
if now.saturating_sub(settled_at) >= FINAL_FRAME {
|
||||
ControlFlow::Break(WatchEnding::BootCompleted)
|
||||
} else {
|
||||
ControlFlow::Continue(())
|
||||
ControlFlow::Continue(changes)
|
||||
}
|
||||
}
|
||||
|
||||
fn glyphs(&self) -> &Glyphs {
|
||||
&self.glyphs
|
||||
}
|
||||
|
||||
fn palette(&self) -> ConsolePalette {
|
||||
ConsolePalette::Terminal
|
||||
}
|
||||
|
||||
fn prompt(&self, now: Duration) -> Option<PasswordPrompt<'_>> {
|
||||
self.script.prompt(&self.typed, now)
|
||||
}
|
||||
@@ -378,27 +405,23 @@ fn demo_origin() -> BootOrigin {
|
||||
}
|
||||
|
||||
async fn play_scenario(terminal: &mut DefaultTerminal, scenario: &Scenario) -> io::Result<()> {
|
||||
let script = Script::begin(scenario.steps, 1);
|
||||
let mut state = BootState::new(scenario.phase);
|
||||
let script = Script::begin(
|
||||
scenario.steps,
|
||||
scenario.describe,
|
||||
1,
|
||||
&mut state,
|
||||
Duration::ZERO,
|
||||
);
|
||||
script
|
||||
.opening_changes(scenario.describe, Duration::ZERO)
|
||||
.into_iter()
|
||||
.for_each(|change| state.apply(change));
|
||||
let desk = DemoDesk {
|
||||
script,
|
||||
after_switch_root: (!scenario.after_switch_root.is_empty())
|
||||
.then_some(scenario.after_switch_root),
|
||||
describe: scenario.describe,
|
||||
next_stage: scenario.after_switch_root,
|
||||
settled_at: None,
|
||||
typed: passphrase_input::empty(),
|
||||
glyphs: Glyphs::default(),
|
||||
};
|
||||
let mut screen = WatchScreen::new(
|
||||
state,
|
||||
desk,
|
||||
demo_origin(),
|
||||
demo_origin,
|
||||
Colors::default(),
|
||||
Timing::default(),
|
||||
monotonic_now(),
|
||||
@@ -430,16 +453,10 @@ mod tests {
|
||||
KEY_DERIVATION,
|
||||
)];
|
||||
|
||||
struct AskingDemo {
|
||||
script: Script,
|
||||
state: BootState,
|
||||
}
|
||||
|
||||
fn asking_demo() -> AskingDemo {
|
||||
let mut state = BootState::new(Phase::Initrd);
|
||||
let mut script = Script::begin(UNLOCK_ONLY, |_| None, 1, &mut state, Duration::ZERO);
|
||||
script.advance(&mut state, Duration::ZERO);
|
||||
AskingDemo { script, state }
|
||||
fn asking_demo() -> Script {
|
||||
let mut script = Script::begin(UNLOCK_ONLY, 1);
|
||||
script.advance(Duration::ZERO);
|
||||
script
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -449,7 +466,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn a_wrong_answer_is_checked_then_called_out() {
|
||||
let AskingDemo { mut script, .. } = asking_demo();
|
||||
let mut script = asking_demo();
|
||||
let asked = script.prompt("", Duration::ZERO).expect("asking");
|
||||
assert_eq!(asked.hint, Some(DEMO_PASSPHRASE_HINT));
|
||||
assert!(!asked.rejected);
|
||||
@@ -462,12 +479,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn the_right_answer_unlocks_and_closes_the_prompt() {
|
||||
let AskingDemo {
|
||||
mut script,
|
||||
mut state,
|
||||
} = asking_demo();
|
||||
let mut script = asking_demo();
|
||||
script.answer(DEMO_PASSPHRASE, Duration::ZERO);
|
||||
script.advance(&mut state, KEY_DERIVATION);
|
||||
script.advance(KEY_DERIVATION);
|
||||
assert!(script.is_done());
|
||||
assert!(script.prompt("", KEY_DERIVATION).is_none());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user