|
| 1 | +// TODO: get rid of this debug command? clean it && make it permanent? |
| 2 | + |
| 3 | +//! Pick command |
| 4 | +use super::Command; |
| 5 | +use crate::err::Error; |
| 6 | +use async_trait::async_trait; |
| 7 | +use clap::{App, Arg, ArgMatches, SubCommand}; |
| 8 | +/// Abstract contest command |
| 9 | +/// |
| 10 | +/// ```sh |
| 11 | +/// leetcode-contest |
| 12 | +/// Pick a problem |
| 13 | +/// |
| 14 | +/// USAGE: |
| 15 | +/// leetcode contest [OPTIONS] [id] |
| 16 | +/// |
| 17 | +/// FLAGS: |
| 18 | +/// -h, --help Prints help information |
| 19 | +/// -V, --version Prints version information |
| 20 | +/// |
| 21 | +/// OPTIONS: |
| 22 | +/// -q, --query <query> Fliter questions by conditions: |
| 23 | +/// Uppercase means negative |
| 24 | +/// e = easy E = m+h |
| 25 | +/// m = medium M = e+h |
| 26 | +/// h = hard H = e+m |
| 27 | +/// d = done D = not done |
| 28 | +/// l = locked L = not locked |
| 29 | +/// s = starred S = not starred |
| 30 | +/// |
| 31 | +/// ARGS: |
| 32 | +/// <id> Problem id |
| 33 | +/// ``` |
| 34 | +pub struct FunCommand; |
| 35 | + |
| 36 | +static _QUERY_HELP: &str = r#"Fliter questions by conditions: |
| 37 | +Uppercase means negative |
| 38 | +e = easy E = m+h |
| 39 | +m = medium M = e+h |
| 40 | +h = hard H = e+m |
| 41 | +d = done D = not done |
| 42 | +l = locked L = not locked |
| 43 | +s = starred S = not starred"#; |
| 44 | + |
| 45 | +#[async_trait] |
| 46 | +impl Command for FunCommand { |
| 47 | + /// `contest` usage |
| 48 | + fn usage<'a, 'contest>() -> App<'a, 'contest> { |
| 49 | + SubCommand::with_name("fun") |
| 50 | + .about("fun") |
| 51 | + .visible_alias("f") |
| 52 | + .arg( |
| 53 | + Arg::with_name("query") |
| 54 | + .help("GraphQL query - MUST be of the format `query a { ... }`") |
| 55 | + .takes_value(true) |
| 56 | + .short("q") |
| 57 | + .conflicts_with("type") |
| 58 | + .required(true) |
| 59 | + ).arg( |
| 60 | + Arg::with_name("variables") |
| 61 | + .help("Variables to pass to the GraphQL query, e.g. `{'slug': 'two-sum'}`") |
| 62 | + .takes_value(true) |
| 63 | + .short("v") |
| 64 | + .requires("query") |
| 65 | + ).arg( |
| 66 | + Arg::with_name("type") |
| 67 | + .help("type to get the definition of, e.g. `ContestNode`") |
| 68 | + .takes_value(true) |
| 69 | + .short("t") |
| 70 | + .required(true) |
| 71 | + .conflicts_with("query") |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + /// `contest` handler |
| 76 | + async fn handler(m: &ArgMatches<'_>) -> Result<(), Error> { |
| 77 | + use crate::cache::Cache; |
| 78 | + |
| 79 | + let cache = Cache::new()?; |
| 80 | + let query = if let Some(q) = m.value_of("query") { q.to_string() } |
| 81 | + else if let Some(t) = m.value_of("type"){ |
| 82 | + "query a { |
| 83 | + __type(name: \"$type\") { |
| 84 | + name |
| 85 | + fields { |
| 86 | + name |
| 87 | + args { |
| 88 | + name |
| 89 | + description |
| 90 | + defaultValue |
| 91 | + type { |
| 92 | + name |
| 93 | + kind |
| 94 | + ofType { |
| 95 | + name |
| 96 | + kind |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + type { |
| 101 | + name |
| 102 | + kind |
| 103 | + ofType { |
| 104 | + name |
| 105 | + kind |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + }".replace("$type", t) |
| 111 | + } else { unreachable!() }; |
| 112 | + let vars = m.value_of("variables") |
| 113 | + .map(|v| v.to_string()); |
| 114 | + println!("{}", cache.0.get_graphql(query, vars).await?.text().await?); |
| 115 | + |
| 116 | + Ok(()) |
| 117 | + } |
| 118 | +} |
0 commit comments