obf_rs/
lib.rs

1use serde::{Deserialize, Serialize};
2use serde_with::skip_serializing_none;
3use std::collections::HashMap;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6#[serde(rename_all = "camelCase")]
7pub struct Tournament {
8    pub event: Event,
9    pub sets: Vec<Set>,
10    pub entrants: Vec<Entrant>,
11}
12
13#[skip_serializing_none]
14#[derive(Debug, Clone, Serialize, Deserialize)]
15#[serde(rename_all = "camelCase")]
16pub struct Event {
17    pub name: String,
18    pub date: Option<String>,
19    pub game_name: Option<String>,
20    pub tournament_structure: Option<String>,
21    pub phases: Option<Vec<Phase>>,
22    pub ruleset: Option<String>,
23    pub origin_u_r_l: Option<String>,
24    pub number_entrants: Option<u32>,
25    pub other: Option<HashMap<String, String>>,
26}
27
28#[skip_serializing_none]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(rename_all = "camelCase")]
31pub struct Entrant {
32    pub entrant_i_d: String,
33    pub entrant_tag: Option<String>,
34    pub initial_seed: Option<u32>,
35    pub final_placement: Option<u32>,
36    pub personal_information: Option<PersonalInformation>,
37    pub other: Option<HashMap<String, String>>,
38}
39
40#[skip_serializing_none]
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(rename_all = "camelCase")]
43pub struct PersonalInformation {
44    pub name: Option<String>,
45    pub country: Option<String>,
46    pub other: Option<HashMap<String, String>>,
47}
48
49#[skip_serializing_none]
50#[derive(Debug, Clone, Serialize, Deserialize)]
51#[serde(rename_all = "camelCase")]
52pub struct Set {
53    pub set_i_d: String,
54    pub entrant_1_i_d: Option<String>,
55    pub entrant_2_i_d: Option<String>,
56    pub status: Option<Status>,
57    pub entrant_1_result: Option<EntrantResult>,
58    pub entrant_2_result: Option<EntrantResult>,
59    pub entrant_1_score: Option<i32>,
60    pub entrant_2_score: Option<i32>,
61    pub winner_next_set_i_d: Option<String>,
62    pub loser_next_set_i_d: Option<String>,
63    pub entrant_1_prev_set_i_d: Option<String>,
64    pub entrant_2_prev_set_i_d: Option<String>,
65    pub set_format: Option<String>,
66    pub phase_i_d: Option<String>,
67    pub round_i_d: Option<String>,
68    pub games: Option<Vec<Game>>,
69    pub other: Option<HashMap<String, String>>,
70}
71
72#[skip_serializing_none]
73#[derive(Debug, Clone, Serialize, Deserialize)]
74#[serde(rename_all = "camelCase")]
75pub struct Game {
76    pub game_number: u32,
77    pub entrant_1_character: Option<String>,
78    pub entrant_2_character: Option<String>,
79    pub stage: Option<String>,
80    pub entrant_1_result: Option<EntrantResult>,
81    pub entrant_2_result: Option<EntrantResult>,
82    pub other: Option<HashMap<String, String>>,
83}
84
85#[skip_serializing_none]
86#[derive(Debug, Clone, Serialize, Deserialize)]
87#[serde(rename_all = "camelCase")]
88pub struct Phase {
89    pub phase_i_d: String,
90    pub phase_structure: Option<String>,
91    pub other: Option<HashMap<String, String>>,
92}
93
94// #[derive(Serialize, Deserialize)]
95#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
96#[serde(rename_all = "camelCase")]
97pub enum Status {
98    Completed,
99    Started,
100    Pending,
101}
102
103// #[derive(Serialize, Deserialize)]
104#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
105#[serde(rename_all = "camelCase")]
106pub enum EntrantResult {
107    Win,
108    Lose,
109    Draw,
110}
111
112#[cfg(test)]
113mod tests {
114    #[test]
115    fn minimal() {
116        let minimal_str = include_str!("example_json/minimal.json");
117
118        let minimal_de: super::Tournament = serde_json::from_str(minimal_str).unwrap();
119
120        let minimal_se = serde_json::to_string_pretty(&minimal_de).unwrap();
121
122        assert!(minimal_str
123            .split_whitespace()
124            .eq((&minimal_se[..]).split_whitespace()))
125    }
126
127    #[test]
128    fn example() {
129        let example_str = include_str!("example_json/example.json");
130
131        let example_de: super::Tournament = serde_json::from_str(example_str).unwrap();
132
133        let example_se = serde_json::to_string_pretty(&example_de).unwrap();
134
135        assert!(example_str
136            .split_whitespace()
137            .eq((&example_se[..]).split_whitespace()))
138    }
139}