mk_lib/schema/
use_cargo.rs1use hashbrown::HashMap;
2use serde::Deserialize;
3
4use super::{
5 CommandRunner,
6 LocalRun,
7 Task,
8 TaskArgs,
9};
10
11#[derive(Debug, Deserialize)]
12pub struct UseCargoArgs {
13 #[serde(default)]
15 pub work_dir: Option<String>,
16}
17
18#[derive(Debug, Deserialize)]
19#[serde(untagged)]
20pub enum UseCargo {
21 Bool(bool),
22 UseCargo(Box<UseCargoArgs>),
23}
24
25impl UseCargo {
26 pub fn capture(&self) -> anyhow::Result<HashMap<String, Task>> {
27 match self {
28 UseCargo::Bool(true) => self.capture_tasks(),
29 UseCargo::UseCargo(args) => args.capture_tasks(),
30 _ => Ok(HashMap::new()),
31 }
32 }
33
34 fn capture_tasks(&self) -> anyhow::Result<HashMap<String, Task>> {
35 UseCargoArgs { work_dir: None }.capture_tasks()
36 }
37}
38
39impl UseCargoArgs {
40 pub fn capture_tasks(&self) -> anyhow::Result<HashMap<String, Task>> {
41 let cargo_commands = [
42 "add",
43 "bench",
44 "build",
45 "check",
46 "clean",
47 "clippy",
48 "doc",
49 "fix",
50 "fmt",
51 "init",
52 "install",
53 "miri",
54 "new",
55 "publish",
56 "remove",
57 "report",
58 "run",
59 "search",
60 "test",
61 "uninstall",
62 "update",
63 ];
64
65 let hm: HashMap<String, Task> = cargo_commands
66 .iter()
67 .map(|cmd| {
68 let command = format!("cargo {}", cmd);
69 let task = Task::Task(Box::new(TaskArgs {
70 commands: vec![CommandRunner::LocalRun(LocalRun {
71 command,
72 shell: None,
73 test: None,
74 work_dir: self.work_dir.clone(),
75 interactive: Some(true),
76 ignore_errors: None,
77 verbose: None,
78 })],
79 ..Default::default()
80 }));
81 (cmd.to_string(), task)
82 })
83 .collect();
84 Ok(hm)
85 }
86}