use std::ffi::OsString;
use sim_kernel::{CapabilityName, Cx, Lib};
use crate::{CliError, LibSourceSpec, LoadSession, parse_args, run_command_with_session};
pub struct Bootloader {
session: LoadSession,
}
impl Bootloader {
pub fn standard() -> Self {
Self {
session: LoadSession::new(),
}
}
pub fn host_verb<F>(self, verb: &str, name: &str, factory: F) -> Self
where
F: Fn() -> Box<dyn Lib> + Send + Sync + 'static,
{
Self {
session: self
.session
.with_host_factory(name.to_owned(), factory)
.with_default_verb_sources(
verb.to_owned(),
vec![LibSourceSpec::Host(name.to_owned())],
),
}
}
pub fn host_lib<F>(self, name: &str, factory: F) -> Self
where
F: Fn() -> Box<dyn Lib> + Send + Sync + 'static,
{
Self {
session: self.session.with_host_factory(name.to_owned(), factory),
}
}
pub fn with_capability(self, capability: CapabilityName) -> Self {
Self {
session: self.session.with_capability(capability),
}
}
pub fn with_context<F>(self, configure: F) -> Self
where
F: FnOnce(&mut Cx),
{
Self {
session: self.session.with_context(configure),
}
}
pub fn run<I, S>(self, args: I) -> Result<i32, CliError>
where
I: IntoIterator<Item = S>,
S: Into<OsString>,
{
let mut session = self.session;
let command = parse_args(args)?;
run_command_with_session(command, &mut session)
}
}