leetcode_cli/cmds/
exec.rs

1//! Exec command
2use super::Command;
3use crate::{Error, Result};
4use async_trait::async_trait;
5use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command as ClapCommand};
6
7/// Abstract Exec Command
8///
9/// ```sh
10/// leetcode-exec
11/// Submit solution
12///
13/// USAGE:
14///     leetcode exec <id>
15///
16/// FLAGS:
17///     -h, --help       Prints help information
18///     -V, --version    Prints version information
19///
20/// ARGS:
21///     <id>    question id
22/// ```
23pub struct ExecCommand;
24
25#[async_trait]
26impl Command for ExecCommand {
27    /// `exec` usage
28    fn usage() -> ClapCommand {
29        ClapCommand::new("exec")
30            .about("Submit solution")
31            .visible_alias("x")
32            .arg(
33                Arg::new("id")
34                    .num_args(1)
35                    .required(true)
36                    .value_parser(clap::value_parser!(i32))
37                    .help("question id"),
38            )
39            .arg(
40                Arg::new("daily")
41                    .short('d')
42                    .long("daily")
43                    .help("Exec today's daily challenge")
44                    .action(ArgAction::SetTrue),
45            )
46            .group(
47                ArgGroup::new("question-id")
48                    .args(["id", "daily"])
49                    .multiple(false)
50                    .required(true),
51            )
52    }
53
54    /// `exec` handler
55    async fn handler(m: &ArgMatches) -> Result<()> {
56        use crate::cache::{Cache, Run};
57
58        let cache = Cache::new()?;
59
60        let daily = m.get_one::<bool>("daily").unwrap_or(&false);
61        let daily_id = if *daily {
62            Some(cache.get_daily_problem_id().await?)
63        } else {
64            None
65        };
66
67        let id = m
68            .get_one::<i32>("id")
69            .copied()
70            .or(daily_id)
71            .ok_or(Error::NoneError)?;
72
73        let res = cache.exec_problem(id, Run::Submit, None).await?;
74
75        println!("{}", res);
76        Ok(())
77    }
78}