xtask_lib/tasks/
run_emulator.rs

1//! `cargo xtask run-emulator` — run the Cadmus emulator.
2//!
3//! Ensures MuPDF sources and the `mupdf_wrapper` C library are built for the
4//! native platform, then launches `cargo run -p emulator`.  Any extra
5//! arguments are forwarded to the emulator.
6
7use anyhow::Result;
8use clap::Args;
9
10use super::setup_native;
11use super::util::{cmd, mupdf_wrapper, workspace};
12
13/// Arguments for `cargo xtask run-emulator`.
14#[derive(Debug, Args)]
15pub struct RunEmulatorArgs {
16    /// Cargo feature flags forwarded to `cargo run -p emulator`.
17    #[arg(long)]
18    pub features: Option<String>,
19
20    /// Extra arguments forwarded to `cargo run -p emulator`.
21    #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
22    pub extra: Vec<String>,
23}
24
25/// Ensures prerequisites are built then launches the emulator.
26///
27/// # Errors
28///
29/// Returns an error if the MuPDF download, wrapper build, or emulator launch
30/// fails.
31pub fn run(args: RunEmulatorArgs) -> Result<()> {
32    let root = workspace::root()?;
33
34    setup_native::ensure_mupdf_sources(&root, false)?;
35    mupdf_wrapper::build_native_if_needed(&root)?;
36
37    let mut cargo_args = vec!["run", "-p", "emulator"];
38
39    if let Some(features) = args.features.as_deref() {
40        cargo_args.push("--features");
41        cargo_args.push(features);
42    }
43
44    let extra_refs: Vec<&str> = args.extra.iter().map(String::as_str).collect();
45    cargo_args.extend_from_slice(&extra_refs);
46
47    cmd::run("cargo", &cargo_args, &root, &[])
48}