lichess-api 1.0.0

A client library for the Lichess API
Documentation
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
pub mod account;
pub mod analysis;
pub mod arena_tournaments;
pub mod board;
pub mod bot;
pub mod broadcasts;
pub mod bulk_pairings;
pub mod challenges;
pub mod external_engine;
pub mod fide;
pub mod games;
pub mod messaging;
#[cfg(feature = "oauth")]
pub mod oauth;
pub mod openings;
pub mod puzzles;
pub mod relations;
pub mod simuls;
pub mod studies;
pub mod swiss_tournaments;
pub mod tablebase;
pub mod teams;
pub mod tv;
pub mod users;

use crate::error;
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use serde_with::skip_serializing_none;

pub trait BodyBounds: Serialize {}
impl<B: Serialize> BodyBounds for B {}

pub trait QueryBounds: Serialize + Default {}
impl<Q: Serialize + Default> QueryBounds for Q {}

pub trait ModelBounds: DeserializeOwned {}
impl<M: DeserializeOwned> ModelBounds for M {}

#[derive(Default, Clone, Debug)]
pub enum Body<B: BodyBounds> {
    Form(B),
    Json(B),
    PlainText(String),
    #[default]
    Empty,
}

impl<B: BodyBounds> Body<B> {
    fn as_mime(&self) -> Option<mime::Mime> {
        match &self {
            Body::Form(_) => Some(mime::APPLICATION_WWW_FORM_URLENCODED),
            Body::Json(_) => Some(mime::APPLICATION_JSON),
            Body::PlainText(_) => Some(mime::TEXT_PLAIN),
            Body::Empty => None,
        }
    }

    fn as_encoded_string(&self) -> error::Result<String> {
        let body = match &self {
            Body::Form(form) => to_form_string(&form)?,
            Body::Json(json) => to_json_string(&json)?,
            Body::PlainText(text) => text.to_string(),
            Body::Empty => "".to_string(),
        };
        Ok(body)
    }
}

#[derive(Clone, Copy, Debug, Default)]
pub enum Domain {
    #[default]
    Lichess,
    Tablebase,
    Engine,
    Explorer,
}

impl AsRef<str> for Domain {
    fn as_ref(&self) -> &str {
        match self {
            Domain::Lichess => "lichess.org",
            Domain::Tablebase => "tablebase.lichess.ovh",
            Domain::Engine => "engine.lichess.ovh",
            Domain::Explorer => "explorer.lichess.ovh",
        }
    }
}

#[derive(Clone, Debug)]
pub struct Request<Q, B = ()>
where
    Q: QueryBounds,
    B: BodyBounds,
{
    pub(crate) domain: Domain,
    pub(crate) method: http::Method,
    pub(crate) path: String,
    pub(crate) query: Option<Q>,
    pub(crate) body: Body<B>,
}

impl<Q, B> Request<Q, B>
where
    Q: QueryBounds + Default,
    B: BodyBounds,
{
    pub(crate) fn create(
        path: impl Into<String>,
        query: impl Into<Option<Q>>,
        body: impl Into<Option<Body<B>>>,
        domain: impl Into<Option<Domain>>,
        method: http::Method,
    ) -> Self {
        Self {
            domain: domain.into().unwrap_or_default(),
            method,
            path: path.into(),
            query: query.into(),
            body: body.into().unwrap_or_default(),
        }
    }

    pub(crate) fn get(
        path: impl Into<String>,
        query: impl Into<Option<Q>>,
        domain: impl Into<Option<Domain>>,
    ) -> Self {
        Self::create(path, query, None, domain, http::Method::GET)
    }

    pub(crate) fn head(
        path: impl Into<String>,
        query: impl Into<Option<Q>>,
        domain: impl Into<Option<Domain>>,
    ) -> Self {
        Self::create(path, query, None, domain, http::Method::HEAD)
    }

    pub(crate) fn post(
        path: impl Into<String>,
        query: impl Into<Option<Q>>,
        body: impl Into<Option<Body<B>>>,
        domain: impl Into<Option<Domain>>,
    ) -> Self {
        Self::create(path, query, body, domain, http::Method::POST)
    }

    pub(crate) fn put(
        path: impl Into<String>,
        query: impl Into<Option<Q>>,
        body: impl Into<Option<Body<B>>>,
        domain: impl Into<Option<Domain>>,
    ) -> Self {
        Self::create(path, query, body, domain, http::Method::PUT)
    }

    pub(crate) fn delete(
        path: impl Into<String>,
        query: impl Into<Option<Q>>,
        body: impl Into<Option<Body<B>>>,
        domain: Option<Domain>,
    ) -> Self {
        Self::create(path, query, body, domain, http::Method::DELETE)
    }
}

impl<Q, B> Request<Q, B>
where
    Q: QueryBounds,
    B: BodyBounds,
{
    pub(crate) fn into_http_request(
        self,
        accept: &str,
    ) -> error::Result<http::Request<bytes::Bytes>> {
        make_request(
            self.domain,
            self.method,
            self.path,
            self.query,
            self.body,
            accept,
        )
    }
}

fn make_request<Q, B>(
    domain: Domain,
    method: http::Method,
    path: String,
    query: Option<Q>,
    body: Body<B>,
    accept: &str,
) -> error::Result<http::Request<bytes::Bytes>>
where
    Q: QueryBounds,
    B: BodyBounds,
{
    let mut builder = http::Request::builder();

    if let Some(mime) = body.as_mime() {
        builder = builder.header(http::header::CONTENT_TYPE, mime.to_string());
    }
    let accept_header = http::HeaderValue::from_str(accept)
        .map_err(|e| error::Error::HttpRequestBuilder(http::Error::from(e)))?;
    builder = builder.header(http::header::ACCEPT, accept_header);

    let url = make_url(domain, path, query)?;
    let body = bytes::Bytes::from(body.as_encoded_string()?);

    let request = builder
        .method(method)
        .uri(url.as_str())
        .body(body)
        .map_err(error::Error::HttpRequestBuilder)?;

    Ok(request)
}

fn make_url<Q>(domain: Domain, path: String, query: Option<Q>) -> error::Result<url::Url>
where
    Q: QueryBounds,
{
    let base_url = format!("https://{}", domain.as_ref());
    let mut url = url::Url::parse(&base_url).expect("invalid base url");

    if let Some(query) = query {
        let mut query_pairs = url.query_pairs_mut();
        let query_serializer = serde_urlencoded::Serializer::new(&mut query_pairs);
        query.serialize(query_serializer)?;
    }

    url.set_path(&path.to_string());

    Ok(url)
}

fn to_json_string<B: BodyBounds>(body: &B) -> error::Result<String> {
    serde_json::to_string(&body).map_err(error::Error::Json)
}

fn to_form_string<B: BodyBounds>(body: &B) -> error::Result<String> {
    serde_urlencoded::to_string(body).map_err(error::Error::UrlEncoded)
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Ok {
    pub ok: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Response<M> {
    Model(M),
    Error { error: String },
}

#[derive(Default, Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Color {
    #[default]
    White,
    Black,
    Random,
}

#[derive(Default, Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum PlayerColor {
    #[default]
    White,
    Black,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum Speed {
    UltraBullet,
    Bullet,
    Blitz,
    Rapid,
    Classical,
    Correspondence,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, Hash)]
#[serde(rename_all = "camelCase")]
pub enum PerfType {
    UltraBullet,
    Bullet,
    Blitz,
    Rapid,
    Classical,
    Correspondence,
    Chess960,
    Crazyhouse,
    Antichess,
    Atomic,
    Horde,
    KingOfTheHill,
    RacingKings,
    ThreeCheck,
}

impl PerfType {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::UltraBullet => "ultraBullet",
            Self::Bullet => "bullet",
            Self::Blitz => "blitz",
            Self::Rapid => "rapid",
            Self::Classical => "classical",
            Self::Correspondence => "correspondence",
            Self::Chess960 => "chess960",
            Self::Crazyhouse => "crazyhouse",
            Self::Antichess => "antichess",
            Self::Atomic => "atomic",
            Self::Horde => "horde",
            Self::KingOfTheHill => "kingOfTheHill",
            Self::RacingKings => "racingKings",
            Self::ThreeCheck => "threeCheck",
        }
    }
}

impl std::fmt::Display for PerfType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LightUser {
    pub id: String,
    pub name: String,
    pub title: Option<Title>,
    pub flair: Option<String>,
    pub patron: Option<bool>,
    #[serde(rename = "patronColor")]
    pub patron_color: Option<u8>,
    pub online: Option<bool>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum Title {
    Gm,
    Wgm,
    Im,
    Wim,
    Fm,
    Wfm,
    Nm,
    Cm,
    Wcm,
    Wnm,
    Lm,
    Bot,
}

#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Variant {
    pub key: VariantKey,
    pub name: String,
    pub short: Option<String>,
    pub icon: Option<String>,
}

#[derive(Default, Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum VariantKey {
    #[default]
    Standard,
    Chess960,
    Crazyhouse,
    Antichess,
    Atomic,
    Horde,
    KingOfTheHill,
    RacingKings,
    ThreeCheck,
    FromPosition,
}

#[derive(Default, Clone, Debug, Serialize, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Room {
    #[default]
    Player,
    Spectator,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct GameCompat {
    pub bot: Option<bool>,
    pub board: Option<bool>,
}

#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Clock {
    pub initial: u32,
    pub increment: u32,
    pub total_time: Option<u32>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Days {
    One,
    Two,
    Three,
    Five,
    Seven,
    Ten,
    Fourteen,
}

impl Serialize for Days {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let value: u32 = (*self).into();
        value.serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for Days {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = u32::deserialize(deserializer)?;
        Ok(Days::from(value))
    }
}

impl From<u32> for Days {
    fn from(value: u32) -> Self {
        match value {
            1 => Days::One,
            2 => Days::Two,
            3 => Days::Three,
            5 => Days::Five,
            7 => Days::Seven,
            10 => Days::Ten,
            14 => Days::Fourteen,
            _ => panic!("Invalid days {}", value),
        }
    }
}

impl From<Days> for u32 {
    fn from(value: Days) -> Self {
        match value {
            Days::One => 1,
            Days::Two => 2,
            Days::Three => 3,
            Days::Five => 5,
            Days::Seven => 7,
            Days::Ten => 10,
            Days::Fourteen => 14,
        }
    }
}

#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ArenaTournament {
    pub id: String,
    pub created_by: String,
    pub system: String,
    pub minutes: u32,
    pub clock: ArenaClock,
    pub rated: bool,
    pub full_name: String,
    pub nb_players: u32,
    pub variant: Variant,
    pub starts_at: i64,
    pub finishes_at: i64,
    pub status: ArenaStatus,
    pub perf: ArenaPerf,
    pub seconds_to_start: Option<i32>,
    pub has_max_rating: Option<bool>,
    pub max_rating: Option<ArenaRatingObj>,
    pub min_rating: Option<ArenaRatingObj>,
    pub min_rated_games: Option<ArenaMinRatedGames>,
    pub bots_allowed: Option<bool>,
    pub min_account_age_in_days: Option<i32>,
    pub only_titled: Option<bool>,
    pub team_member: Option<String>,
    pub private: Option<bool>,
    pub position: Option<ArenaPosition>,
    pub schedule: Option<ArenaSchedule>,
    pub team_battle: Option<ArenaTeamBattle>,
    pub winner: Option<LightUser>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ArenaClock {
    pub limit: u32,
    pub increment: u32,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ArenaStatus {
    Created,
    Started,
    Finished,
}

impl Serialize for ArenaStatus {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let value: u8 = match self {
            ArenaStatus::Created => 10,
            ArenaStatus::Started => 20,
            ArenaStatus::Finished => 30,
        };
        value.serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for ArenaStatus {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = u8::deserialize(deserializer)?;
        match value {
            10 => Ok(ArenaStatus::Created),
            20 => Ok(ArenaStatus::Started),
            30 => Ok(ArenaStatus::Finished),
            _ => Err(serde::de::Error::custom(format!(
                "invalid arena tournament status {value}"
            ))),
        }
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ArenaStatusName {
    Created,
    Started,
    Finished,
}

#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ArenaPerf {
    pub key: PerfType,
    pub name: String,
    pub position: i32,
    pub icon: Option<String>,
}

#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ArenaRatingObj {
    pub perf: Option<PerfType>,
    pub rating: i32,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ArenaMinRatedGames {
    pub nb: Option<i32>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum ArenaPosition {
    Thematic {
        eco: String,
        name: String,
        fen: String,
        url: String,
    },
    Custom {
        name: String,
        fen: String,
    },
}

#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ArenaSchedule {
    pub freq: Option<String>,
    pub speed: Option<String>,
}

#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ArenaTeamBattle {
    pub teams: Option<Vec<String>>,
    pub nb_leaders: Option<i32>,
}

#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SwissTournament {
    pub id: String,
    pub created_by: String,
    pub starts_at: String,
    pub name: String,
    pub clock: SwissClock,
    pub variant: VariantKey,
    pub round: i32,
    pub nb_rounds: i32,
    pub nb_players: i32,
    pub nb_ongoing: i32,
    pub status: SwissStatus,
    pub stats: Option<SwissStats>,
    pub rated: bool,
    pub verdicts: Verdicts,
    pub next_round: Option<SwissNextRound>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SwissClock {
    pub limit: i32,
    pub increment: i32,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum SwissStatus {
    Created,
    Started,
    Finished,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SwissStats {
    pub games: i32,
    pub white_wins: i32,
    pub black_wins: i32,
    pub draws: i32,
    pub byes: i32,
    pub absences: i32,
    pub average_rating: f64,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SwissNextRound {
    pub at: String,
    #[serde(rename = "in")]
    pub in_seconds: i32,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Verdicts {
    pub accepted: bool,
    pub list: Vec<Verdict>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Verdict {
    pub condition: String,
    pub verdict: String,
}