Repository: google/comprehensive-rust
File: xtask/src/main.rs (Rust)

Code before:
        Task::InstallTools => install_tools()?,
        Task::WebTests { dir } => run_web_tests(dir)?,
        Task::RustTests => run_rust_tests()?,
        Task::Serve { language, output } => start_web_server(language, output)?,
        Task::Build { language, output } => build(language, output)?,
    }
    Ok(())
}

fn install_tools() -> Result<()> {
    println!("Installing project tools...");

    const PINNED_NIGHTLY: &str = "nightly-2025-09-01";

    let rustup_steps = [
        // Installe la toolchain sans les composants inutiles
        ["toolchain", "install", "--profile", "minimal", PINNED_NIGHTLY],
        // Ajoute rustfmt pour cette toolchain
        ["component", "add", "rustfmt", "--toolchain", PINNED_NIGHTLY],
    ];

    for args in rustup_steps {
        let status = std::process::Command::new("rustup")
            .args(args)
            .status()
            .expect("Failed to execute rustup");
        if !status.success() {
            return Err(anyhow!(
                "Command 'rustup {}' failed with status {:?}",
                args.join(" "),
                status.code()
            ));
        }
    }

    let path_to_mdbook_exerciser =
        Path::new(env!("CARGO_WORKSPACE_DIR")).join("mdbook-exerciser");
    let path_to_mdbook_course =
        Path::new(env!("CARGO_WORKSPACE_DIR")).join("mdbook-course");

    let install_args = vec![
        // The --locked flag is important for reproducible builds. It also
        // avoids breakage due to skews between mdbook and mdbook-svgbob.
        vec!["mdbook", "--locked", "--version", "0.4.51"],
        vec!["mdbook-svgbob", "--locked", "--version", "0.2.2"],
        vec!["mdbook-pandoc", "--locked", "--version", "0.10.4"],
        vec!["mdbook-i18n-helpers", "--locked", "--version", "0.3.6"],
        vec!["i18n-report", "--locked", "--version", "0.2.0"],
        vec!["mdbook-linkcheck2", "--locked", "--version", "0.9.1"],
        // Mdbook-exerciser and mdbook-course are located in this repository.
        // To make it possible to install them from any directory we need to

Review comment (suggestion):
Please don't use `expect` (or `unwrap`), instead return the `Result` back with `?`. @egithinji, can you review this part? We should (eventually) have some useful patterns in the `xtask` code to easily and quickly execute shell commands.

Code after:
        Task::InstallTools => install_tools()?,
        Task::WebTests { dir } => run_web_tests(dir)?,
        Task::RustTests => run_rust_tests()?,
        Task::Serve { language, output } => start_web_server(language, output)?,
        Task::Build { language, output } => build(language, output)?,
    }
    Ok(())
}

fn install_tools() -> Result<()> {
    println!("Installing project tools...");

    const PINNED_NIGHTLY: &str = "nightly-2025-09-01";

    let rustup_steps = [
        ["toolchain", "install", "--profile", "minimal", PINNED_NIGHTLY],
        ["component", "add", "rustfmt", "--toolchain", PINNED_NIGHTLY],
    ];

    for args in rustup_steps {
        let status = std::process::Command::new("rustup").args(args).status()?;
        if !status.success() {
            return Err(anyhow!(
                "Command 'rustup {}' failed with status {:?}",
                args.join(" "),
                status.code()
            ));
        }
    }

    let path_to_mdbook_exerciser =
        Path::new(env!("CARGO_WORKSPACE_DIR")).join("mdbook-exerciser");
    let path_to_mdbook_course =
        Path::new(env!("CARGO_WORKSPACE_DIR")).join("mdbook-course");

    let install_args = vec![
        // The --locked flag is important for reproducible builds. It also
        // avoids breakage due to skews between mdbook and mdbook-svgbob.
        vec!["mdbook", "--locked", "--version", "0.4.51"],
        vec!["mdbook-svgbob", "--locked", "--version", "0.2.2"],
        vec!["mdbook-pandoc", "--locked", "--version", "0.10.4"],
        vec!["mdbook-i18n-helpers", "--locked", "--version", "0.3.6"],
        vec!["i18n-report", "--locked", "--version", "0.2.0"],
        vec!["mdbook-linkcheck2", "--locked", "--version", "0.9.1"],
        // Mdbook-exerciser and mdbook-course are located in this repository.
        // To make it possible to install them from any directory we need to
        // specify their path from the workspace root.
        vec!["--path", path_to_mdbook_exerciser.to_str().unwrap(), "--locked"],
        vec!["--path", path_to_mdbook_course.to_str().unwrap(), "--locked"],
    ];