use std::fmt::{Display, Formatter};
use crate::{BundleInterface, BundleRegistry, Environment, KernelError};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum KernelPhase {
Created,
Compiled,
Booted,
Shutdown,
}
impl Display for KernelPhase {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Created => "created",
Self::Compiled => "compiled",
Self::Booted => "booted",
Self::Shutdown => "shutdown",
})
}
}
pub struct Kernel {
environment: Environment,
debug: bool,
registry: BundleRegistry,
bundles: Vec<Box<dyn BundleInterface>>,
phase: KernelPhase,
}
impl Kernel {
#[must_use]
pub fn new(environment: Environment) -> Self {
let debug = environment.is_debug();
Self {
environment,
debug,
registry: BundleRegistry::new(),
bundles: Vec::new(),
phase: KernelPhase::Created,
}
}
#[must_use]
pub const fn with_debug(mut self, debug: bool) -> Self {
self.debug = debug;
self
}
#[must_use]
pub const fn environment(&self) -> &Environment {
&self.environment
}
#[must_use]
pub const fn debug(&self) -> bool {
self.debug
}
#[must_use]
pub const fn phase(&self) -> KernelPhase {
self.phase
}
#[must_use]
pub fn bundle_names(&self) -> Vec<&'static str> {
if self.phase == KernelPhase::Created {
self.registry.names()
} else {
self.bundles.iter().map(|bundle| bundle.name()).collect()
}
}
pub fn register_bundle(
&mut self,
bundle: impl BundleInterface + 'static,
) -> Result<(), KernelError> {
self.ensure_phase("register", KernelPhase::Created)?;
self.registry.register(bundle)
}
pub fn compile(&mut self) -> Result<(), KernelError> {
self.ensure_phase("compile", KernelPhase::Created)?;
let registry = std::mem::take(&mut self.registry);
self.bundles = registry.sorted()?;
for bundle in &self.bundles {
bundle
.build()
.map_err(|error| wrap_bundle(bundle.name(), "build", error))?;
}
self.phase = KernelPhase::Compiled;
Ok(())
}
pub fn boot(&mut self) -> Result<(), KernelError> {
if self.phase == KernelPhase::Created {
self.compile()?;
}
self.ensure_phase("boot", KernelPhase::Compiled)?;
for bundle in &self.bundles {
bundle
.boot()
.map_err(|error| wrap_bundle(bundle.name(), "boot", error))?;
}
self.phase = KernelPhase::Booted;
Ok(())
}
pub fn shutdown(&mut self) -> Result<(), KernelError> {
self.ensure_phase("shutdown", KernelPhase::Booted)?;
for bundle in self.bundles.iter().rev() {
bundle
.shutdown()
.map_err(|error| wrap_bundle(bundle.name(), "shutdown", error))?;
}
self.phase = KernelPhase::Shutdown;
Ok(())
}
fn ensure_phase(&self, action: &'static str, expected: KernelPhase) -> Result<(), KernelError> {
if self.phase == expected {
Ok(())
} else {
Err(KernelError::InvalidState {
action,
state: self.phase,
})
}
}
}
impl std::fmt::Debug for Kernel {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Kernel")
.field("environment", &self.environment)
.field("debug", &self.debug)
.field("phase", &self.phase)
.field("bundles", &self.bundle_names())
.finish_non_exhaustive()
}
}
fn wrap_bundle(bundle: &'static str, phase: &'static str, error: KernelError) -> KernelError {
match error {
KernelError::Bundle { .. } => error,
other => KernelError::Bundle {
bundle,
phase,
message: other.to_string(),
},
}
}