litchee 0.1.1

Async, builder-pattern Rust client for the Lichess API: full endpoint coverage, NDJSON streaming, and OAuth2 PKCE ('Log in with Lichess').
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
//! The Arena Tournaments API: create, run, join, and export arena tournaments.
//!
//! Reached through [`LichessClient::arena`].

use futures_util::stream::BoxStream;
use reqwest::Method;
use serde::{Deserialize, Serialize};

use crate::api::gameplay::games::LichessGame;
use crate::client::LichessClient;
use crate::config::Host;
use crate::error::Result;
use crate::http;
use crate::model::{LichessLightUser, LichessTitle, LichessVariantKey};

/// Form body for creating an arena tournament.
#[derive(Debug, Serialize)]
struct CreateForm<'a> {
    name: &'a str,
    #[serde(rename = "clockTime")]
    clock_time: f32,
    #[serde(rename = "clockIncrement")]
    clock_increment: u32,
    minutes: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    rated: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    variant: Option<LichessVariantKey>,
    #[serde(rename = "waitMinutes", skip_serializing_if = "Option::is_none")]
    wait_minutes: Option<u32>,
}

/// Accessor for the Arena Tournaments API.
#[derive(Debug)]
pub struct ArenaApi<'a> {
    client: &'a LichessClient,
}

impl<'a> ArenaApi<'a> {
    /// Binds the accessor to a client.
    pub(crate) fn new(client: &'a LichessClient) -> Self {
        Self { client }
    }

    /// Lists current arena tournaments. `GET /api/tournament`
    pub async fn list(&self) -> Result<LichessArenaList> {
        let request = self
            .client
            .request(Method::GET, Host::Default, "/api/tournament");
        http::json(request, "LichessArenaList").await
    }

    /// Gets full details of a tournament. `GET /api/tournament/{id}`
    pub async fn get(&self, id: &str) -> Result<LichessArenaFull> {
        let path = format!("/api/tournament/{}", http::segment(id));
        let request = self.client.request(Method::GET, Host::Default, &path);
        http::json(request, "LichessArenaFull").await
    }

    /// Creates an arena tournament. `POST /api/tournament`
    #[must_use]
    pub fn create(
        &self,
        name: &'a str,
        clock_time: f32,
        clock_increment: u32,
        minutes: u32,
    ) -> CreateArenaRequest<'a> {
        CreateArenaRequest::new(
            self.client,
            None,
            name,
            clock_time,
            clock_increment,
            minutes,
        )
    }

    /// Updates an existing arena tournament. `POST /api/tournament/{id}`
    #[must_use]
    pub fn update(
        &self,
        id: &'a str,
        name: &'a str,
        clock_time: f32,
        clock_increment: u32,
        minutes: u32,
    ) -> CreateArenaRequest<'a> {
        CreateArenaRequest::new(
            self.client,
            Some(id),
            name,
            clock_time,
            clock_increment,
            minutes,
        )
    }

    /// Configures a team battle. `POST /api/tournament/team-battle/{id}`
    pub async fn setup_team_battle(
        &self,
        id: &str,
        team_ids: &[&str],
        nb_leaders: u32,
    ) -> Result<LichessArenaFull> {
        let path = format!("/api/tournament/team-battle/{}", http::segment(id));
        let request = self
            .client
            .request(Method::POST, Host::Default, &path)
            .form(&[
                ("teams", team_ids.join(",").as_str()),
                ("nbLeaders", nb_leaders.to_string().as_str()),
            ]);
        http::json(request, "LichessArenaFull").await
    }

    /// Gets the team standings of a team-battle arena.
    /// `GET /api/tournament/{id}/teams`
    pub async fn teams(&self, id: &str) -> Result<LichessTeamBattleStandings> {
        let path = format!("/api/tournament/{}/teams", http::segment(id));
        let request = self.client.request(Method::GET, Host::Default, &path);
        http::json(request, "LichessTeamBattleStandings").await
    }

    /// Streams the arenas created by a user (NDJSON).
    /// `GET /api/user/{username}/tournament/created`
    pub async fn created_by(
        &self,
        username: &str,
    ) -> Result<BoxStream<'static, Result<LichessArena>>> {
        let path = format!("/api/user/{}/tournament/created", http::segment(username));
        let request = self.client.request(Method::GET, Host::Default, &path);
        http::stream(request, self.client.max_line_bytes()).await
    }

    /// Streams the arenas a user has played (NDJSON).
    /// `GET /api/user/{username}/tournament/played`
    pub async fn played_by(
        &self,
        username: &str,
    ) -> Result<BoxStream<'static, Result<LichessArena>>> {
        let path = format!("/api/user/{}/tournament/played", http::segment(username));
        let request = self.client.request(Method::GET, Host::Default, &path);
        http::stream(request, self.client.max_line_bytes()).await
    }

    /// Joins a tournament. `POST /api/tournament/{id}/join`
    pub async fn join(&self, id: &str) -> Result<()> {
        let path = format!("/api/tournament/{}/join", http::segment(id));
        http::ok(self.client.request(Method::POST, Host::Default, &path)).await
    }

    /// Withdraws from a tournament. `POST /api/tournament/{id}/withdraw`
    pub async fn withdraw(&self, id: &str) -> Result<()> {
        let path = format!("/api/tournament/{}/withdraw", http::segment(id));
        http::ok(self.client.request(Method::POST, Host::Default, &path)).await
    }

    /// Terminates a tournament you created. `POST /api/tournament/{id}/terminate`
    pub async fn terminate(&self, id: &str) -> Result<()> {
        let path = format!("/api/tournament/{}/terminate", http::segment(id));
        http::ok(self.client.request(Method::POST, Host::Default, &path)).await
    }

    /// Streams a tournament's results. `GET /api/tournament/{id}/results`
    pub async fn results(
        &self,
        id: &str,
    ) -> Result<BoxStream<'static, Result<LichessArenaResult>>> {
        let path = format!("/api/tournament/{}/results", http::segment(id));
        let request = self.client.request(Method::GET, Host::Default, &path);
        http::stream(request, self.client.max_line_bytes()).await
    }

    /// Streams a tournament's games as NDJSON. `GET /api/tournament/{id}/games`
    pub async fn games(&self, id: &str) -> Result<BoxStream<'static, Result<LichessGame>>> {
        let path = format!("/api/tournament/{}/games", http::segment(id));
        let request = self
            .client
            .request(Method::GET, Host::Default, &path)
            .header(reqwest::header::ACCEPT, "application/x-ndjson");
        http::stream(request, self.client.max_line_bytes()).await
    }
}

/// Builder for creating or updating an arena tournament.
#[derive(Debug)]
pub struct CreateArenaRequest<'a> {
    client: &'a LichessClient,
    id: Option<&'a str>,
    form: CreateForm<'a>,
}

impl<'a> CreateArenaRequest<'a> {
    /// Creates the request builder.
    fn new(
        client: &'a LichessClient,
        id: Option<&'a str>,
        name: &'a str,
        clock_time: f32,
        clock_increment: u32,
        minutes: u32,
    ) -> Self {
        Self {
            client,
            id,
            form: CreateForm {
                name,
                clock_time,
                clock_increment,
                minutes,
                rated: None,
                variant: None,
                wait_minutes: None,
            },
        }
    }

    /// Sets whether the tournament is rated.
    #[must_use]
    pub fn rated(mut self, rated: bool) -> Self {
        self.form.rated = Some(rated);
        self
    }

    /// Sets the variant.
    #[must_use]
    pub fn variant(mut self, variant: LichessVariantKey) -> Self {
        self.form.variant = Some(variant);
        self
    }

    /// Sets how many minutes to wait before starting.
    #[must_use]
    pub fn wait_minutes(mut self, minutes: u32) -> Self {
        self.form.wait_minutes = Some(minutes);
        self
    }

    /// Creates or updates the tournament.
    pub async fn send(self) -> Result<LichessArenaFull> {
        let path = match self.id {
            Some(id) => format!("/api/tournament/{}", http::segment(id)),
            None => "/api/tournament".to_owned(),
        };
        let request = self
            .client
            .request(Method::POST, Host::Default, &path)
            .form(&self.form);
        http::json(request, "LichessArenaFull").await
    }
}

impl LichessClient {
    /// Arena Tournaments API.
    #[must_use]
    pub fn arena(&self) -> ArenaApi<'_> {
        ArenaApi::new(self)
    }
}

/// An arena clock (`limit` + `increment`, in seconds).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LichessArenaClock {
    /// Initial time in seconds.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
    /// Increment per move in seconds.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub increment: Option<u32>,
}

/// Perf display info for an arena.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LichessArenaPerf {
    /// The perf key.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub key: Option<String>,
    /// The perf name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// The perf icon.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub icon: Option<String>,
}

/// A summary of an arena tournament.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct LichessArena {
    /// The tournament id.
    pub id: String,
    /// The full display name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub full_name: Option<String>,
    /// The creator's username.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub created_by: Option<String>,
    /// The tournament duration in minutes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub minutes: Option<u32>,
    /// The clock.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub clock: Option<LichessArenaClock>,
    /// Whether the tournament is rated.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rated: Option<bool>,
    /// The number of players.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub nb_players: Option<u32>,
    /// Start time (Unix milliseconds).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub starts_at: Option<i64>,
    /// Finish time (Unix milliseconds).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub finishes_at: Option<i64>,
    /// The status code (10 = created, 20 = started, 30 = finished).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<i32>,
    /// Perf display info.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub perf: Option<LichessArenaPerf>,
    /// Seconds until the tournament starts.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub seconds_to_start: Option<i64>,
    /// The winner, once finished.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub winner: Option<LichessLightUser>,
}

/// Tournaments grouped by lifecycle stage. `GET /api/tournament`
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LichessArenaList {
    /// Tournaments not yet started.
    #[serde(default)]
    pub created: Vec<LichessArena>,
    /// Tournaments in progress.
    #[serde(default)]
    pub started: Vec<LichessArena>,
    /// Recently finished tournaments.
    #[serde(default)]
    pub finished: Vec<LichessArena>,
}

/// A player row in an arena standings page.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct LichessArenaPlayer {
    /// The player's username.
    pub name: String,
    /// The player's title.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<LichessTitle>,
    /// The player's current rank.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rank: Option<u32>,
    /// The player's rating.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rating: Option<u32>,
    /// The player's score.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub score: Option<u32>,
}

/// A page of arena standings.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LichessArenaStanding {
    /// The page number.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page: Option<u32>,
    /// The players on this page.
    #[serde(default)]
    pub players: Vec<LichessArenaPlayer>,
}

/// Full details of an arena tournament. `GET /api/tournament/{id}`
///
/// Models the commonly-used fields; deeper aggregates (duels, stats, podium)
/// are not decoded.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct LichessArenaFull {
    /// The tournament id.
    pub id: String,
    /// The full display name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub full_name: Option<String>,
    /// Whether the tournament is rated.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rated: Option<bool>,
    /// The clock.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub clock: Option<LichessArenaClock>,
    /// The duration in minutes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub minutes: Option<u32>,
    /// The creator's username.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub created_by: Option<String>,
    /// Seconds until the tournament starts.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub seconds_to_start: Option<i64>,
    /// Seconds until the tournament finishes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub seconds_to_finish: Option<i64>,
    /// Whether the tournament has finished.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub is_finished: Option<bool>,
    /// Whether pairings are closed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pairings_closed: Option<bool>,
    /// Start time (Unix milliseconds).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub starts_at: Option<i64>,
    /// The number of players.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub nb_players: Option<u32>,
    /// Perf display info.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub perf: Option<LichessArenaPerf>,
    /// The current standings page.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub standing: Option<LichessArenaStanding>,
    /// The authenticated user's username, if entered.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub my_username: Option<String>,
}

/// One entry in an arena results stream.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LichessArenaResult {
    /// The player's final rank.
    pub rank: u32,
    /// The player's score.
    pub score: u32,
    /// The player's rating.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rating: Option<u32>,
    /// The player's username.
    pub username: String,
    /// The player's title.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<LichessTitle>,
    /// The player's tournament performance rating.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub performance: Option<u32>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_arena_list() {
        let json = r#"{"created":[],"started":[{"id":"abc","fullName":"Hourly",
            "clock":{"limit":300,"increment":0},"nbPlayers":50,"status":20}],
            "finished":[]}"#;
        let list: LichessArenaList = serde_json::from_str(json).unwrap();
        assert_eq!(list.started.len(), 1);
        assert_eq!(list.started[0].nb_players, Some(50));
    }

    #[test]
    fn parses_full_arena_ignoring_unknown_aggregates() {
        let json = r#"{"id":"abc","fullName":"Hourly","nbPlayers":50,
            "standing":{"page":1,"players":[{"name":"A","rank":1,"score":10}]},
            "duels":[{"whatever":true}],"stats":{"games":100}}"#;
        let full: LichessArenaFull = serde_json::from_str(json).unwrap();
        assert_eq!(full.standing.unwrap().players[0].name, "A");
    }
}

/// A team's standing in a team-battle arena.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LichessTeamBattleTeam {
    /// The team's rank.
    pub rank: u32,
    /// The team id.
    pub id: String,
    /// The team's score.
    pub score: u32,
}

/// The team standings of a team-battle arena.
/// `GET /api/tournament/{id}/teams`
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LichessTeamBattleStandings {
    /// The tournament id.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// The teams, best first.
    #[serde(default)]
    pub teams: Vec<LichessTeamBattleTeam>,
}