use crate::{
    Context, Tool,
    util::{ToolResult, unknown_tool},
};

mod check;
mod expand;
mod format;
mod test;

use check::cargo_check;
use expand::cargo_expand;
use format::cargo_format;
use test::cargo_test;

pub async fn run(ctx: Context, t: Tool) -> ToolResult {
    match t.name.trim_start_matches("cargo_") {
        "check" => cargo_check(&ctx, t.opt("package")?).await,
        "expand" => cargo_expand(&ctx, t.req("item")?, t.opt("package")?).await,
        "test" => {
            cargo_test(
                &ctx,
                t.opt("package")?,
                t.opt("testname")?,
                t.opt("backtrace")?,
            )
            .await
        }
        "format" => cargo_format(&ctx, t.opt("package")?).await,
        _ => unknown_tool(t),
    }
}
