1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
mod commands;
mod config;
mod fs;
mod judge;
pub mod shell;
mod web;

pub use crate::commands::{
    init::OptInit, judge::OptJudge, login::OptLogin, participate::OptParticipate,
    retrieve_languages::OptRetrieveLanguages,
    retrieve_submission_summaries::OptRetrieveSubmissionSummaries,
    retrieve_testcases::OptRetrieveTestcases, submit::OptSubmit,
    watch_submissions::OptWatchSubmissions, xtask::OptXtask,
};
use std::{env, io::BufRead, path::PathBuf};
use structopt::{
    clap::{self, AppSettings},
    StructOpt,
};
use strum::{EnumString, EnumVariantNames};
use termcolor::WriteColor;

pub const STACK_SIZE: usize = 128 * 1024 * 1024;

#[derive(StructOpt, Debug)]
#[structopt(author, about, global_setting = AppSettings::DeriveDisplayOrder)]
pub enum Opt {
    /// Create a new config file
    #[structopt(author, visible_alias("i"))]
    Init(OptInit),

    /// Logges in to a service
    #[structopt(author, visible_alias("l"))]
    Login(OptLogin),

    /// Participates in a contest
    Participate(OptParticipate),

    /// Retrieves data
    #[structopt(author, visible_alias("r"))]
    Retrieve(OptRetrieve),

    /// Alias for `retrieve testcases`
    #[structopt(author, visible_alias("d"))]
    Download(OptRetrieveTestcases),

    /// Watches data
    #[structopt(author, visible_alias("w"))]
    Watch(OptWatch),

    /// Tests code
    #[structopt(author, visible_aliases(&["j", "test", "t"]))]
    Judge(OptJudge),

    /// Submits code
    #[structopt(author, visible_alias("s"))]
    Submit(OptSubmit),

    /// Runs a custom subcommand written in the config file
    #[structopt(author, visible_alias("x"), setting = AppSettings::TrailingVarArg)]
    Xtask(OptXtask),
}

#[derive(StructOpt, Debug)]
pub enum OptRetrieve {
    /// Retrieves list of languages
    #[structopt(author, visible_alias("l"))]
    Languages(OptRetrieveLanguages),

    /// Retrieves test cases
    #[structopt(author, visible_alias("t"))]
    Testcases(OptRetrieveTestcases),

    /// Retrieves submission summaries
    #[structopt(author, visible_alias("ss"))]
    SubmissionSummaries(OptRetrieveSubmissionSummaries),
}

#[derive(StructOpt, Debug)]
pub enum OptWatch {
    /// Watches your submissions
    #[structopt(author, visible_alias("s"))]
    Submissions(OptWatchSubmissions),
}

impl Opt {
    pub fn from_args_with_workaround_for_clap_issue_1538() -> Self {
        let mut args = env::args_os().collect::<Vec<_>>();

        Self::from_iter_safe(&args).unwrap_or_else(|clap::Error { kind, .. }| {
            if matches!(
                args.get(1).and_then(|s| s.to_str()),
                Some("x") | Some("xtask")
            ) && matches!(args.get(2).and_then(|s| s.to_str()), Some(s) if !s.starts_with('-'))
                && matches!(
                    kind,
                    clap::ErrorKind::UnknownArgument
                        | clap::ErrorKind::HelpDisplayed
                        | clap::ErrorKind::VersionDisplayed
                )
            {
                args.insert(3, "--".into());
            }

            Self::from_iter(args)
        })
    }

    pub fn color(&self) -> crate::ColorChoice {
        match *self {
            Self::Init(OptInit { color, .. })
            | Self::Login(OptLogin { color, .. })
            | Self::Participate(OptParticipate { color, .. })
            | Self::Retrieve(OptRetrieve::Languages(OptRetrieveLanguages { color, .. }))
            | Self::Retrieve(OptRetrieve::Testcases(OptRetrieveTestcases { color, .. }))
            | Self::Retrieve(OptRetrieve::SubmissionSummaries(OptRetrieveSubmissionSummaries {
                color,
                ..
            }))
            | Self::Download(OptRetrieveTestcases { color, .. })
            | Self::Watch(OptWatch::Submissions(OptWatchSubmissions { color, .. }))
            | Self::Judge(OptJudge { color, .. })
            | Self::Submit(OptSubmit { color, .. }) => color,
            Self::Xtask(_) => crate::ColorChoice::Auto,
        }
    }
}

#[derive(EnumVariantNames, EnumString, strum::Display, Debug, Clone, Copy)]
#[strum(serialize_all = "lowercase")]
pub enum ColorChoice {
    Auto,
    Always,
    Never,
}

pub struct Context<R, W1, W2> {
    pub cwd: PathBuf,
    pub shell: crate::shell::Shell<R, W1, W2>,
}

pub fn run<R: BufRead, W1: WriteColor, W2: WriteColor>(
    opt: Opt,
    ctx: Context<R, W1, W2>,
) -> anyhow::Result<()> {
    match opt {
        Opt::Init(opt) => commands::init::run(opt, ctx),
        Opt::Login(opt) => commands::login::run(opt, ctx),
        Opt::Participate(opt) => commands::participate::run(opt, ctx),
        Opt::Retrieve(OptRetrieve::Languages(opt)) => commands::retrieve_languages::run(opt, ctx),
        Opt::Retrieve(OptRetrieve::Testcases(opt)) => commands::retrieve_testcases::run(opt, ctx),
        Opt::Retrieve(OptRetrieve::SubmissionSummaries(opt)) => {
            commands::retrieve_submission_summaries::run(opt, ctx)
        }
        Opt::Download(opt) => commands::retrieve_testcases::run(opt, ctx),
        Opt::Watch(OptWatch::Submissions(opt)) => commands::watch_submissions::run(opt, ctx),
        Opt::Judge(opt) => commands::judge::run(opt, ctx),
        Opt::Submit(opt) => commands::submit::run(opt, ctx),
        Opt::Xtask(opt) => commands::xtask::run(opt, ctx),
    }
}