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
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use std::collections::HashMap;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Tournament {
    pub event: Event,
    pub sets: Vec<Set>,
    pub entrants: Vec<Entrant>,
}

#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Event {
    pub name: String,
    pub date: Option<String>,
    pub game_name: Option<String>,
    pub tournament_structure: Option<String>,
    pub phases: Option<Vec<Phase>>,
    pub ruleset: Option<String>,
    pub origin_u_r_l: Option<String>,
    pub number_entrants: Option<u32>,
    pub other: Option<HashMap<String, String>>,
}

#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Entrant {
    pub entrant_i_d: String,
    pub entrant_tag: Option<String>,
    pub initial_seed: Option<u32>,
    pub final_placement: Option<u32>,
    pub personal_information: Option<PersonalInformation>,
    pub other: Option<HashMap<String, String>>,
}

#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PersonalInformation {
    pub name: Option<String>,
    pub country: Option<String>,
    pub other: Option<HashMap<String, String>>,
}

#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Set {
    pub set_i_d: String,
    pub entrant_1_i_d: Option<String>,
    pub entrant_2_i_d: Option<String>,
    pub status: Option<Status>,
    pub entrant_1_result: Option<EntrantResult>,
    pub entrant_2_result: Option<EntrantResult>,
    pub entrant_1_score: Option<i32>,
    pub entrant_2_score: Option<i32>,
    pub winner_next_set_i_d: Option<String>,
    pub loser_next_set_i_d: Option<String>,
    pub entrant_1_prev_set_i_d: Option<String>,
    pub entrant_2_prev_set_i_d: Option<String>,
    pub set_format: Option<String>,
    pub phase_i_d: Option<String>,
    pub round_i_d: Option<String>,
    pub games: Option<Vec<Game>>,
    pub other: Option<HashMap<String, String>>,
}

#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Game {
    pub game_number: u32,
    pub entrant_1_character: Option<String>,
    pub entrant_2_character: Option<String>,
    pub stage: Option<String>,
    pub entrant_1_result: Option<EntrantResult>,
    pub entrant_2_result: Option<EntrantResult>,
    pub other: Option<HashMap<String, String>>,
}

#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Phase {
    pub phase_i_d: String,
    pub phase_structure: Option<String>,
    pub other: Option<HashMap<String, String>>,
}

// #[derive(Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Status {
    Completed,
    Started,
    Pending,
}

// #[derive(Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum EntrantResult {
    Win,
    Lose,
    Draw,
}

#[cfg(test)]
mod tests {
    #[test]
    fn minimal() {
        let minimal_str = include_str!("example_json/minimal.json");

        let minimal_de: super::Tournament = serde_json::from_str(minimal_str).unwrap();

        let minimal_se = serde_json::to_string_pretty(&minimal_de).unwrap();

        assert!(minimal_str
            .split_whitespace()
            .eq((&minimal_se[..]).split_whitespace()))
    }

    #[test]
    fn example() {
        let example_str = include_str!("example_json/example.json");

        let example_de: super::Tournament = serde_json::from_str(example_str).unwrap();

        let example_se = serde_json::to_string_pretty(&example_de).unwrap();

        assert!(example_str
            .split_whitespace()
            .eq((&example_se[..]).split_whitespace()))
    }
}