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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
use crate::{
    testsuite::{BatchTestSuite, Match, PartialBatchTestCase, TestSuite},
    web::{
        codeforces::api::SessionMutExt as _, CookieStorage, Exec, Login, LoginOutcome, Participate,
        ParticipateOutcome, Platform, ResponseExt as _, RetrieveLanguages,
        RetrieveLanguagesOutcome, RetrieveTestCases, RetrieveTestCasesOutcome,
        RetrieveTestCasesOutcomeContest, RetrieveTestCasesOutcomeProblem, Session, SessionMut,
        Shell, Submit, SubmitOutcome,
    },
};
use anyhow::{bail, Context as _};
use easy_ext::ext;
use indexmap::{indexmap, IndexMap};
use itertools::Itertools as _;
use once_cell::sync::Lazy;
use scraper::{ElementRef, Html, Node, Selector};
use std::{
    collections::{BTreeSet, HashMap},
    convert::Infallible,
    marker::PhantomData,
    time::Duration,
};
use url::Url;

static BASE_URL: Lazy<Url> = lazy_url!("https://codeforces.com");

#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum Codeforces<'closures> {
    Infallible(Infallible, PhantomData<fn() -> &'closures ()>),
}

impl<'closures> Platform for Codeforces<'closures> {
    type CookieStorage = CookieStorage;
    type LoginCredentials = CodeforcesLoginCredentials<'closures>;
    type ParticipateTarget = CodeforcesParticipateTarget;
    type ParticipateCredentials = CodeforcesParticipateCredentials<'closures>;
    type RetrieveLanguagesTarget = CodeforcesRetrieveLanguagesTarget;
    type RetrieveLanguagesCredentials = CodeforcesRetrieveLanguagesCredentials<'closures>;
    type RetrieveTestCasesTargets = CodeforcesRetrieveTestCasesTargets;
    type RetrieveTestCasesCredentials = CodeforcesRetrieveSampleTestCasesCredentials<'closures>;
    type RetrieveFullTestCasesCredentials = Infallible;
    type RetrieveSubmissionSummariesTarget = Infallible;
    type RetrieveSubmissionSummariesCredentials = Infallible;
    type WatchSubmissionsTarget = Infallible;
    type WatchSubmissionsCredentials = Infallible;
    type SubmitTarget = CodeforcesSubmitTarget;
    type SubmitCredentials = CodeforcesSubmitCredentials<'closures>;
}

impl Codeforces<'_> {
    pub fn exec<A>(args: A) -> anyhow::Result<<Self as Exec<A>>::Output>
    where
        Self: Exec<A>,
    {
        <Self as Exec<_>>::exec(args)
    }
}

impl<S: Shell> Exec<Login<Self, S>> for Codeforces<'_> {
    type Output = LoginOutcome;

    fn exec(args: Login<Self, S>) -> anyhow::Result<LoginOutcome> {
        let Login {
            credentials:
                CodeforcesLoginCredentials {
                    username_and_password,
                },
            cookie_storage,
            timeout,
            shell,
        } = args;

        let sess = Session::new(timeout, Some(cookie_storage), shell)?;
        let (outcome, _) = login(sess, username_and_password)?;
        Ok(outcome)
    }
}

impl<S: Shell> Exec<Participate<Self, S>> for Codeforces<'_> {
    type Output = ParticipateOutcome;

    fn exec(args: Participate<Self, S>) -> anyhow::Result<ParticipateOutcome> {
        let Participate {
            target: CodeforcesParticipateTarget { contest },
            credentials:
                CodeforcesParticipateCredentials {
                    username_and_password,
                },
            cookie_storage,
            timeout,
            shell,
        } = args;

        let contest = parse_contest_id(&contest)?;
        let sess = Session::new(timeout, Some(cookie_storage), shell)?;
        let (outcome, _, _) = participate(sess, username_and_password, contest)?;
        Ok(outcome)
    }
}

impl<S: Shell> Exec<RetrieveLanguages<Self, S>> for Codeforces<'_> {
    type Output = RetrieveLanguagesOutcome;

    fn exec(args: RetrieveLanguages<Self, S>) -> anyhow::Result<RetrieveLanguagesOutcome> {
        let RetrieveLanguages {
            target: CodeforcesRetrieveLanguagesTarget { contest },
            credentials:
                CodeforcesRetrieveLanguagesCredentials {
                    username_and_password,
                },
            cookie_storage,
            timeout,
            shell,
        } = args;

        let contest = parse_contest_id(&contest)?;

        let mut sess = Session::new(timeout, Some(cookie_storage), shell)?;

        participate(&mut sess, username_and_password, contest)?;

        let names_by_id = sess
            .get(url!("/contest/{}/submit", contest))
            .colorize_status_code(&[200], (), ..)
            .send()?
            .ensure_status(&[200])?
            .html()?
            .extract_available_langs()?;

        Ok(RetrieveLanguagesOutcome { names_by_id })
    }
}

impl<S: Shell> Exec<RetrieveTestCases<Self, S>> for Codeforces<'_> {
    type Output = RetrieveTestCasesOutcome;

    fn exec(args: RetrieveTestCases<Self, S>) -> anyhow::Result<RetrieveTestCasesOutcome> {
        let RetrieveTestCases {
            targets: CodeforcesRetrieveTestCasesTargets { contest, problems },
            credentials:
                CodeforcesRetrieveSampleTestCasesCredentials {
                    username_and_password,
                },
            full: _,
            cookie_storage,
            timeout,
            shell,
        } = args;

        let contest = parse_contest_id(&contest)?;

        let mut sess = Session::new(timeout, Some(cookie_storage), shell)?;

        let (_, contest_name, _) = participate(&mut sess, username_and_password, contest)?;

        let mut problem_indices = problems.map(|ps| {
            ps.iter()
                .map(AsRef::as_ref)
                .map(str::to_uppercase)
                .collect::<BTreeSet<_>>()
        });

        let problems = sess
            .get(url!("/contest/{}", contest))
            .colorize_status_code(&[200], (), ..)
            .send()?
            .ensure_status(&[200])?
            .html()?
            .extract_problems()?
            .into_iter()
            .map(|(index, display_name, url)| {
                if let Some(problem_indices) = &mut problem_indices {
                    if !problem_indices.remove(&index) {
                        return Ok(None);
                    }
                }

                let test_suite = sess
                    .get(url.clone())
                    .colorize_status_code(&[200], (), ..)
                    .send()?
                    .html()?
                    .extract_test_cases()?;

                Ok(Some(RetrieveTestCasesOutcomeProblem {
                    index,
                    url,
                    screen_name: None,
                    display_name,
                    test_suite,
                    text_files: indexmap!(),
                }))
            })
            .flat_map(Result::transpose)
            .collect::<anyhow::Result<_>>()?;

        if let Some(problem_indices) = problem_indices {
            if !problem_indices.is_empty() {
                bail!("No such problem indices: {:?}", problem_indices);
            }
        }

        Ok(RetrieveTestCasesOutcome {
            contest: Some(RetrieveTestCasesOutcomeContest {
                id: contest.to_string(),
                display_name: contest_name,
                url: url!("/contest/{}", contest),
                submissions_url: url!("/contest/{}/my", contest),
            }),
            problems,
        })
    }
}

impl<S: Shell> Exec<Submit<Self, S>> for Codeforces<'_> {
    type Output = SubmitOutcome;

    fn exec(args: Submit<Self, S>) -> anyhow::Result<SubmitOutcome> {
        let Submit {
            target:
                CodeforcesSubmitTarget {
                    contest: contest_id,
                    problem: problem_index,
                },
            credentials:
                CodeforcesSubmitCredentials {
                    username_and_password,
                    api_key,
                    api_secret,
                },
            language_id,
            code,
            watch_submission,
            cookie_storage,
            timeout,
            mut shell,
        } = args;

        if watch_submission {
            shell.warn("`watch_submissions` in Codeforces is not yet supported")?;
        }

        let contest_id = parse_contest_id(&contest_id)?;

        let mut sess = Session::new(timeout, Some(cookie_storage), shell)?;

        let (_, _, handle) = participate(&mut sess, username_and_password, contest_id)?;

        let (_, problems, _) = sess.api_contest_standings(contest_id, None, None, "", "", false)?;

        let problem = problems
            .into_iter()
            .find(|api::Problem { index, .. }| index.eq_ignore_ascii_case(problem_index.as_ref()))
            .with_context(|| {
                format!("No such problem index: {:?}", problem_index.to_uppercase())
            })?;

        let url = url!("/contest/{}/submit", contest_id);

        let mut payload = sess
            .get(url.clone())
            .colorize_status_code(&[200], (), ..)
            .send()?
            .ensure_status(&[200])?
            .html()?
            .extract_hidden_values(static_selector!("form.submit-form"))?;

        payload.insert("contestId".to_owned(), contest_id.to_string());
        payload.insert("submittedProblemIndex".to_owned(), problem.index);
        payload.insert("tabSize".to_owned(), "4".to_owned());
        payload.insert("programTypeId".to_owned(), language_id);
        payload.insert("source".to_owned(), code);

        let res = sess
            .post(url)
            .form(&payload)
            .colorize_status_code(&[302], (), ..)
            .send()?
            .ensure_status(&[200, 302])?;

        if res.status() == 200 {
            bail!("Submission rejected");
        } else {
            let submissions_url = res.location_url()?;

            let submissions =
                sess.api_contest_status(&api_key, &api_secret, contest_id, &handle, 1, Some(1))?;

            let submission = submissions
                .get(0)
                .with_context(|| "Recieved no submission")?;

            let submission_url = url!("/contest/{}/submission/{}", contest_id, submission.id);

            Ok(SubmitOutcome {
                problem_screen_name: None,
                submission_url,
                submissions_url,
            })
        }
    }
}

pub struct CodeforcesLoginCredentials<'closures> {
    pub username_and_password: &'closures mut dyn FnMut() -> anyhow::Result<(String, String)>,
}

#[derive(Debug)]
pub struct CodeforcesParticipateTarget {
    pub contest: String,
}

pub struct CodeforcesParticipateCredentials<'closures> {
    pub username_and_password: &'closures mut dyn FnMut() -> anyhow::Result<(String, String)>,
}

#[derive(Debug)]
pub struct CodeforcesRetrieveLanguagesTarget {
    pub contest: String,
}

pub struct CodeforcesRetrieveLanguagesCredentials<'closures> {
    pub username_and_password: &'closures mut dyn FnMut() -> anyhow::Result<(String, String)>,
}

#[derive(Debug)]
pub struct CodeforcesRetrieveTestCasesTargets {
    pub contest: String,
    pub problems: Option<BTreeSet<String>>,
}

pub struct CodeforcesRetrieveSampleTestCasesCredentials<'closures> {
    pub username_and_password: &'closures mut dyn FnMut() -> anyhow::Result<(String, String)>,
}

#[derive(Debug)]
pub struct CodeforcesSubmitTarget {
    pub contest: String,
    pub problem: String,
}

pub struct CodeforcesSubmitCredentials<'closures> {
    pub username_and_password: &'closures mut dyn FnMut() -> anyhow::Result<(String, String)>,
    pub api_key: String,
    pub api_secret: String,
}

fn parse_contest_id(s: &str) -> anyhow::Result<u64> {
    s.parse().with_context(|| {
        format!(
            "A contest ID for Codeforces must be unsigned integer: {:?}",
            s,
        )
    })
}

fn login(
    mut sess: impl SessionMut,
    mut username_and_password: impl FnMut() -> anyhow::Result<(String, String)>,
) -> anyhow::Result<(LoginOutcome, String)> {
    let url = url!("/enter");

    let mut res = sess
        .get(url.clone())
        .colorize_status_code(&[200, 302], (), ..)
        .send()?
        .ensure_status(&[200, 302])?;

    if res.status() == 302 {
        let handle = handle(&res.location_url()?).to_owned();
        return Ok((LoginOutcome::AlreadyLoggedIn, handle));
    }

    return loop {
        let (handle_or_email, password) = username_and_password()?;

        let mut payload = res
            .html()?
            .extract_hidden_values(static_selector!("#enterForm"))?;

        payload.insert("handleOrEmail".to_owned(), handle_or_email);
        payload.insert("password".to_owned(), password);
        payload.insert("remember".to_owned(), "on".to_owned());

        res = sess
            .post(url.clone())
            .form(&payload)
            .colorize_status_code(&[200, 302], (), ..)
            .send()?
            .ensure_status(&[200, 302])?;

        if res.status() == 302 {
            let handle = handle(&res.location_url()?).to_owned();
            break Ok((LoginOutcome::Success, handle));
        }

        sess.shell().warn("Failed to login. Try again")?;
    };

    fn handle(url: &Url) -> &str {
        url.path_segments().and_then(Iterator::last).unwrap_or("")
    }
}

fn participate(
    mut sess: impl SessionMut,
    username_and_password: impl FnMut() -> anyhow::Result<(String, String)>,
    contest_id: u64,
) -> anyhow::Result<(ParticipateOutcome, String, String)> {
    let (_, handle) = login(&mut sess, username_and_password)?;

    let api::Contest { name, phase, .. } = sess
        .api_contest_list(is_gym(contest_id))?
        .into_iter()
        .find(|&api::Contest { id, .. }| id == contest_id)
        .with_context(|| format!("No such contest: `{}`", contest_id))?;

    if phase == api::ContestPhase::Finished {
        Ok((ParticipateOutcome::ContestIsFinished, name, handle))
    } else {
        let url = url!("/contestRegistration/{}", contest_id);

        let status = sess
            .get(url.clone())
            .colorize_status_code(&[200, 302], (), ..)
            .send()?
            .ensure_status(&[200, 302])?
            .status();

        let outcome = if status == 200 {
            todo!("Contest registration for Codeforces is not yet implemented. Please open {} in browser", url);
        } else {
            ParticipateOutcome::AlreadyParticipated
        };

        Ok((outcome, name, handle))
    }
}

fn is_gym(contest_id: u64) -> bool {
    contest_id >= 100_000
}

#[ext]
impl Html {
    fn extract_hidden_values(&self, form: &Selector) -> anyhow::Result<HashMap<String, String>> {
        let mut values = self
            .select(form)
            .flat_map(|r| r.select(static_selector!("input[type=\"hidden\"]")))
            .flat_map(|input| {
                let input = input.value();
                let name = input.attr("name")?.to_owned();
                let value = input.attr("value")?.to_owned();
                Some((name, value))
            })
            .collect::<HashMap<String, String>>();

        if values.is_empty() {
            bail!("Could not extract the `name` and `value`");
        }

        if let Some(ftaa) = values.get_mut("ftaa") {
            *ftaa = "0".repeat(18);
        }
        if let Some(bfaa) = values.get_mut("bfaa") {
            *bfaa = "n/a".to_owned();
        }
        Ok(values)
    }

    fn extract_available_langs(&self) -> anyhow::Result<IndexMap<String, String>> {
        self.select(static_selector!(
            "form.submit-form > table > tbody > tr > td"
        ))
        .find(|td| {
            td.select(static_selector!("select[name=\"programTypeId\"]"))
                .next()
                .is_some()
        })
        .with_context(|| "Could not find `select[name=\"programTypeId\"]`")?
        .select(static_selector!("option"))
        .map(|option| {
            let id = option.value().attr("value")?.to_owned();
            let name = option.text().next()?.to_owned();
            Some((id, name))
        })
        .collect::<Option<IndexMap<_, _>>>()
        .filter(|ls| !ls.is_empty())
        .with_context(|| "Could not extract the available languages")
    }

    fn extract_problems(&self) -> anyhow::Result<Vec<(String, String, Url)>> {
        self.select(static_selector!("table.problems > tbody > tr"))
            .skip(1)
            .map(|tr| {
                let a1 = tr.select(static_selector!("td.id > a")).next()?;
                let index = a1.text().next()?.trim().to_owned();
                let href1 = a1.value().attr("href")?;

                let a2 = tr.select(static_selector!("td > div > div > a")).next()?;
                let display = a2.text().next()?.trim().to_owned();
                let href2 = a2.value().attr("href")?;

                if href1 != href2 {
                    return None;
                }

                let url = "https://codeforces.com"
                    .parse::<Url>()
                    .unwrap()
                    .join(href1)
                    .ok()?;

                Some((index, display, url))
            })
            .collect::<Option<Vec<_>>>()
            .filter(|ss| !ss.is_empty())
            .with_context(|| "Could not extract problem names")
    }

    fn extract_test_cases(&self) -> anyhow::Result<TestSuite> {
        let timelimit = self
            .select(static_selector!("#pageContent div.time-limit"))
            .flat_map(|r| r.text())
            .flat_map(|text| {
                let caps = lazy_regex!(r#"\A([0-9]{1,9})(\.[0-9])? seconds?\z"#).captures(text)?;
                let secs = caps[1].parse::<u64>().unwrap();
                let nanos = caps
                    .get(2)
                    .map(|s| 100_000_000 * u32::from(s.as_str().as_bytes()[1] - b'0'))
                    .unwrap_or(0);
                Some(Duration::new(secs, nanos))
            })
            .exactly_one()
            .ok()
            .with_context(|| "Could not extract the timelimit")?;

        let input_file_text = self
            .select(static_selector!("#pageContent div.input-file"))
            .flat_map(|r| r.children())
            .flat_map(|r| match r.value() {
                Node::Text(t) => Some(&**t),
                _ => None,
            })
            .collect::<Vec<_>>();

        let output_file_text = self
            .select(static_selector!("#pageContent div.output-file"))
            .flat_map(|r| r.children())
            .flat_map(|r| match r.value() {
                Node::Text(t) => Some(&**t),
                _ => None,
            })
            .collect::<Vec<_>>();

        if !(input_file_text.contains(&"standard input")
            && output_file_text.contains(&"standard output"))
        {
            todo!();
        }
        let r#match = Match::Lines;

        let sample_test = self
            .select(static_selector!("#pageContent div.sample-test"))
            .exactly_one()
            .ok()
            .with_context(|| "Could not find `.sample-test`")?;

        let ins = sample_test
            .select(static_selector!("div.input > pre"))
            .map(|p| p.fold_text_and_br())
            .collect::<Vec<_>>();

        let outs = sample_test
            .select(static_selector!("div.output > pre"))
            .map(|p| p.fold_text_and_br())
            .collect::<Vec<_>>();

        if ins.is_empty() || ins.len() != outs.len() {
            bail!("in: {}, out: {}", ins.len(), outs.len());
        }

        let cases = ins
            .into_iter()
            .zip_eq(outs)
            .enumerate()
            .map(|(i, (r#in, out))| PartialBatchTestCase {
                name: Some(format!("example{}", i + 1)),
                r#in: r#in.into(),
                out: Some(out.into()),
                r#match: None,
                timelimit: None,
            })
            .collect();

        return Ok(TestSuite::Batch(BatchTestSuite {
            timelimit: Some(timelimit),
            r#match,
            cases,
            extend: vec![],
        }));

        #[ext]
        impl ElementRef<'_> {
            fn fold_text_and_br(&self) -> String {
                self.children().fold("".to_owned(), |mut ret, node| {
                    match node.value() {
                        Node::Text(t) => ret += t,
                        Node::Element(e) if e.name() == "br" => ret.push('\n'),
                        _ => {}
                    }
                    ret
                })
            }
        }
    }

    fn extract_meta_x_csrf_token(&self) -> anyhow::Result<String> {
        self.select(static_selector!("meta[name=\"X-Csrf-Token\"]"))
            .next()
            .and_then(|r| r.value().attr("content").map(ToOwned::to_owned))
            .with_context(|| "Could not extract the `X-Csrf-Token`")
    }
}

/// <https://codeforces.com/apiHelp>
mod api {
    use crate::web::SessionMut;
    use anyhow::anyhow;
    use rand::Rng as _;
    use serde::{
        de::{DeserializeOwned, Deserializer, Error as _},
        Deserialize,
    };
    use sha2::{Digest as _, Sha512};
    use std::time::SystemTime;
    use url::Url;

    /// "Represents a Codeforces user."
    ///
    /// <https://codeforces.com/apiHelp/objects#User>
    #[derive(Debug, Deserialize)]
    pub(super) struct User {
        // __rest: (),
    }

    /// > Represents a contest on Codeforces.
    ///
    /// <https://codeforces.com/apiHelp/objects#Contest>
    #[derive(Debug, Deserialize)]
    pub(super) struct Contest {
        /// > Integer.
        pub(super) id: u64,
        /// > String. Localized.
        pub(super) name: String,
        /// > Enum: BEFORE, CODING, PENDING_SYSTEM_TEST, SYSTEM_TEST, FINISHED.
        pub(super) phase: ContestPhase,
        // __rest: (),
    }

    /// > Enum: BEFORE, CODING, PENDING_SYSTEM_TEST, SYSTEM_TEST, FINISHED.
    ///
    /// <https://codeforces.com/apiHelp/objects#Contest>
    #[derive(Debug, PartialEq, Deserialize)]
    #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
    pub(super) enum ContestPhase {
        Before,
        Coding,
        PendingSystemTest,
        SystemTest,
        Finished,
    }

    /// > Represents a problem.
    ///
    /// <https://codeforces.com/apiHelp/objects#Problem>
    #[derive(Debug, Deserialize)]
    pub(super) struct Problem {
        /// > String. Usually a letter of a letter, followed by a digit, that represent a problem index in a contest.
        pub(super) index: String,
        /// > String. Localized.
        pub(super) name: String,
        // __rest: (),
    }

    /// "Represents a submission."
    ///
    /// <https://codeforces.com/apiHelp/objects#Submission>
    #[derive(Debug, Deserialize)]
    #[serde(rename_all = "camelCase")]
    pub(super) struct Submission {
        /// "Integer."
        pub(super) id: u64,
        /// "Integer. Time, when submission was created, in unix-format."
        pub(super) creation_time_seconds: i64,
        /// "Problem object."
        pub(super) problem: Problem,
        /// "String."
        pub(super) programming_language: String,
        /// "Enum: FAILED, OK, PARTIAL, COMPILATION_ERROR, RUNTIME_ERROR, WRONG_ANSWER, PRESENTATION_ERROR, TIME_LIMIT_EXCEEDED, MEMORY_LIMIT_EXCEEDED, IDLENESS_LIMIT_EXCEEDED, SECURITY_VIOLATED, CRASHED, INPUT_PREPARATION_CRASHED, CHALLENGED, SKIPPED, TESTING, REJECTED. Can be absent."
        pub(super) verdict: Option<SubmissionVerdict>,
        // __rest: (),
    }

    /// "Enum: FAILED, OK, PARTIAL, COMPILATION_ERROR, RUNTIME_ERROR, WRONG_ANSWER, PRESENTATION_ERROR, TIME_LIMIT_EXCEEDED, MEMORY_LIMIT_EXCEEDED, IDLENESS_LIMIT_EXCEEDED, SECURITY_VIOLATED, CRASHED, INPUT_PREPARATION_CRASHED, CHALLENGED, SKIPPED, TESTING, REJECTED. Can be absent."
    ///
    /// <https://codeforces.com/apiHelp/objects#Submission>
    #[derive(Debug, PartialEq, strum::Display, Deserialize)]
    #[strum(serialize_all = "shouty_snake_case")]
    #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
    pub(super) enum SubmissionVerdict {
        Failed,
        Ok,
        Partial,
        CompilationError,
        RuntimeError,
        WrongAnswer,
        PresentationError,
        TimeLimitExceeded,
        MemoryLimitExceeded,
        IdlenessLimitExceeded,
        SecurityViolated,
        Crashed,
        InputPreparationCrashed,
        Challenged,
        Skipped,
        Testing,
        Rejected,
    }

    #[derive(Debug, Deserialize)]
    pub(super) struct RanklistRow {
        //__rest: ()
    }

    pub(super) trait SessionMutExt: SessionMut {
        fn api_contest_list(&mut self, gym: bool) -> anyhow::Result<Vec<Contest>> {
            let mut url = "https://codeforces.com/api/contest.list"
                .parse::<Url>()
                .unwrap();
            url.query_pairs_mut().append_pair("gym", &gym.to_string());

            api(self, url)
        }

        fn api_contest_standings(
            &mut self,
            contest_id: u64,
            from: Option<usize>,
            count: Option<usize>,
            handles: &str,
            room: &str,
            show_unofficial: bool,
        ) -> anyhow::Result<(Contest, Vec<Problem>, Vec<RanklistRow>)> {
            let mut url = "https://codeforces.com/api/contest.standings"
                .parse::<Url>()
                .unwrap();

            url.query_pairs_mut()
                .append_pair("contestId", &contest_id.to_string())
                .append_pair("from", &from.map(|n| n.to_string()).unwrap_or_default())
                .append_pair("count", &count.map(|n| n.to_string()).unwrap_or_default())
                .append_pair("handles", handles)
                .append_pair("room", room)
                .append_pair("showUnofficial", &show_unofficial.to_string());

            let ContestStandings {
                contest,
                problems,
                rows,
            } = api(self, url)?;

            return Ok((contest, problems, rows));

            #[derive(Debug, Deserialize)]
            struct ContestStandings {
                contest: Contest,
                problems: Vec<Problem>,
                rows: Vec<RanklistRow>,
            }
        }

        fn api_contest_status(
            &mut self,
            api_key: &str,
            api_secret: &str,
            contest_id: u64,
            handle: &str,
            from: usize,
            count: Option<usize>,
        ) -> anyhow::Result<Vec<Submission>> {
            let time = SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)?
                .as_secs()
                .to_string();

            let mut url = "https://codeforces.com/api/contest.status"
                .parse::<Url>()
                .unwrap();

            url.query_pairs_mut()
                .append_pair("apiKey", api_key)
                .append_pair("contestId", &contest_id.to_string())
                .append_pair("count", &count.map(|n| n.to_string()).unwrap_or_default())
                .append_pair("from", &from.to_string())
                .append_pair("handle", handle)
                .append_pair("time", &time);

            let api_sig = {
                let rand = rand::thread_rng().gen_range(100_000u32, 1_000_000u32);
                let repr = format!("{}{}#{}", rand, &url.as_str()[26..], api_secret);
                let digest = Sha512::digest(repr.as_ref());
                format!("{}{}", rand, hex::encode(digest))
            };

            url.query_pairs_mut().append_pair("apiSig", &api_sig);

            api(self, url)
        }
    }

    impl<S: SessionMut> SessionMutExt for S {}

    fn api<S: SessionMut, T: DeserializeOwned>(mut sess: S, url: Url) -> anyhow::Result<T> {
        let res = sess.get(url).colorize_status_code(&[200], (), ..).send()?;

        return if res.status() == 200 {
            let ApiOk(ok) = res.json()?;
            Ok(ok)
        } else {
            let ApiErr(msg) = res.json()?;
            Err(anyhow!("API error: {:?}", msg))
        };

        struct ApiOk<T: DeserializeOwned>(T);

        impl<'de, T: DeserializeOwned> Deserialize<'de> for ApiOk<T> {
            fn deserialize<D: Deserializer<'de>>(
                deserializer: D,
            ) -> std::result::Result<Self, D::Error> {
                #[derive(Deserialize)]
                struct Repr<E: DeserializeOwned> {
                    status: String,
                    #[serde(deserialize_with = "E::deserialize")]
                    result: E,
                }

                let repr = Repr::<T>::deserialize(deserializer)?;
                if repr.status == "OK" {
                    Ok(Self(repr.result))
                } else {
                    Err(D::Error::custom("`.status` must be \"OK\""))
                }
            }
        }

        struct ApiErr(String);

        impl<'de> Deserialize<'de> for ApiErr {
            fn deserialize<D: Deserializer<'de>>(
                deserializer: D,
            ) -> std::result::Result<Self, D::Error> {
                #[derive(Deserialize)]
                struct Repr {
                    status: String,
                    comment: String,
                }

                let repr = Repr::deserialize(deserializer)?;
                if repr.status == "FAILED" {
                    Ok(Self(repr.comment))
                } else {
                    Err(D::Error::custom("`.status` must be \"FAILED\""))
                }
            }
        }
    }
}