use std::collections::HashMap;
use crate::engine::dispatch_entry::FunctionKey;
use crate::ids::ExecId;
#[derive(Clone, Debug)]
pub struct ModuleBootstrap {
pub function_key: FunctionKey,
}
#[derive(Clone, Debug)]
pub enum BootstrapKind {
Module {
target: String,
},
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BootstrapStatus {
Idle,
Running,
WaitingForInput,
}
pub struct BootstrapInput<'a> {
pub target: &'a str,
pub inputs: &'a [(&'a str, &'a [u8])],
}
#[derive(Clone, Debug)]
pub struct OwnedBootstrapInput {
pub target: String,
pub inputs: Vec<(String, Vec<u8>)>,
}
pub(crate) struct BootstrapState {
pub(crate) module_bootstraps: HashMap<String, ModuleBootstrap>,
pub(crate) install_order: Vec<String>,
pub(crate) current_exec_id: Option<ExecId>,
pub(crate) next_idx: usize,
pub(crate) pending: bool,
}
impl BootstrapState {
pub(crate) fn new() -> Self {
Self {
module_bootstraps: HashMap::new(),
install_order: Vec::new(),
current_exec_id: None,
next_idx: 0,
pending: false,
}
}
pub(crate) fn clear_for_restore(&mut self) {
self.pending = false;
self.current_exec_id = None;
self.next_idx = self.install_order.len();
}
pub(crate) fn first_function_key(&self) -> Option<&FunctionKey> {
let name = self.install_order.first()?;
self.module_bootstraps.get(name).map(|m| &m.function_key)
}
pub(crate) fn function_keys(&self) -> Vec<FunctionKey> {
self.install_order
.iter()
.filter_map(|name| {
self.module_bootstraps
.get(name)
.map(|m| m.function_key.clone())
})
.collect()
}
pub(crate) fn register_module(&mut self, function_key: FunctionKey) {
let name = function_key.1.clone();
let entry = self
.module_bootstraps
.entry(name.clone())
.or_insert_with(|| ModuleBootstrap {
function_key: function_key.clone(),
});
entry.function_key = function_key;
if !self.install_order.iter().any(|n| n == &name) {
self.install_order.push(name);
}
}
pub(crate) fn arm_install_order(&mut self) -> bool {
if self.next_idx >= self.install_order.len() {
return false;
}
self.pending = true;
true
}
pub(crate) fn mark_module_in_flight(&mut self, _target: String, exec_id: ExecId) {
self.current_exec_id = Some(exec_id);
}
pub(crate) fn clear_in_flight(&mut self) {
self.current_exec_id = None;
}
}