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
/// Unique participant identifier
#[derive(Clone, Default, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct ParticipantId(pub String);

/// A participant type enumeration.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ParticipantType {
    /// Implies the tournament is played by teams
    Team,
    /// Means the tournament is played by players
    Single,
}

/// Logo of the participant.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct ParticipantLogo {
    /// Url to a picture of 48x48px.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub icon_large_square: Option<String>,
    /// Url to a picture of 100x100px.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extra_small_square: Option<String>,
    /// Url to a picture of 200x200px.>
    #[serde(skip_serializing_if = "Option::is_none")]
    pub medium_small_square: Option<String>,
    /// Url to a picture of 400x400px.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub medium_large_square: Option<String>,
}

/// A type of a participant's custom field
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum CustomFieldType {
    /// Participant's steam id
    #[serde(rename = "steam_player_id")]
    SteamId,
    /// Participant's birth date
    #[serde(rename = "birth_date")]
    Birthdate,
    /// Participant's facebook page
    #[serde(rename = "facebook")]
    Facebook,
    /// Participant's full name
    #[serde(rename = "full_name")]
    Fullname,
    /// Participant's instagram page
    #[serde(rename = "instagram")]
    Instagram,
    /// Participant's snapchat
    #[serde(rename = "snapchat")]
    Snapchat,
    /// Participant's text statement
    #[serde(rename = "text")]
    Text,
    /// Participant's twitch stream
    #[serde(rename = "twitch")]
    Twitch,
    /// Participant's twitter account
    #[serde(rename = "twitter")]
    Twitter,
    /// Participant's vimeo account
    #[serde(rename = "vimeo")]
    Vimeo,
    /// Participant's website
    #[serde(rename = "website")]
    Website,
    /// Participant's youtube channel
    #[serde(rename = "youtube")]
    Youtube,
}

/// A participant's custom fields
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct CustomField {
    /// Type of field.
    #[serde(rename = "type")]
    pub field_type: CustomFieldType,
    /// Label of field.
    pub label: String,
    /// Value informed.
    pub value: String,
}

/// A list of participant's custom fields
#[derive(Clone, Default, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct CustomFields(pub Vec<CustomField>);

/// An opponent involved in a match/tournament.
#[derive(Clone, Default, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Participant {
    /// Unique identifier for this participant.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<ParticipantId>,
    /// Participant name (maximum 40 characters).
    pub name: String,
    /// Logo of the participant.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logo: Option<ParticipantLogo>,
    /// This property is only available when the participant type is "team".
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lineup: Option<Participants>,
    /// List of public custom fields
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_fields: Option<CustomFields>,
    /// Country of the participant. This property is only available when the "country"
    /// option is enabled for this tournament. This value is represented as an ISO 3166-1
    /// alpha-2 country code.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub country: Option<String>,
    /// Participant email.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    /// Participant check-in. This property is only available when "check-in" option is
    /// enabled for this tournament.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub check_in: Option<bool>,
    /// This property is only available when the query parameter 'with_custom_fields' is true.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_fields_private: Option<CustomFields>,
}
impl Participant {
    /// Create participant object for adding for a tournament
    /// (Toornament::create_tournament_participant)
    pub fn create<S: Into<String>>(name: S) -> Participant {
        Participant {
            name: name.into(),
            ..Default::default()
        }
    }

    builder_o!(id, ParticipantId);
    builder_s!(name);
    builder_o!(logo, ParticipantLogo);
    builder_o!(lineup, Participants);
    builder_o!(custom_fields, CustomFields);
    builder_o!(country, String);
    builder_o!(email, String);
    builder_o!(check_in, bool);
    builder_o!(custom_fields_private, CustomFields);
}

/// A list of participants
#[derive(Clone, Default, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Participants(pub Vec<Participant>);

#[cfg(test)]
mod tests {
    use serde_json;
    use {CustomFieldType, Participants};

    #[test]
    fn test_participant_parse() {
        let s = r#"
[
    {
        "id": "378426939508809728",
        "name": "Evil Geniuses",
        "logo": {
            "icon_large_square": "http://api.toornament.com/id/icon_large_square",
            "extra_small_square": "http://api.toornament.com/id/extra_small_square",
            "medium_small_square": "http://api.toornament.com/id/medium_small_square",
            "medium_large_square": "http://api.toornament.com/id/medium_large_square"
        },
        "country": "US",
        "lineup": [
            {
                "name": "Storm Spirit",
                "country": "US",
                "custom_fields": [
                    {
                        "type": "steam_player_id",
                        "label": "Steam ID",
                        "value": "STEAM_0:1:1234567"
                    }
                ],
                "email": "player@oxent.net",
                "custom_fields_private": [
                    {
                        "type": "steam_player_id",
                        "label": "Steam ID",
                        "value": "STEAM_0:1:1234567"
                    }
                ]
            }
        ],
        "custom_fields": [
            {
                "type": "steam_player_id",
                "label": "Steam ID",
                "value": "STEAM_0:1:1234567"
            }
        ],
        "email": "contact@oxent.net",
        "check_in": true,
        "custom_fields_private": [
            {
                "type": "steam_player_id",
                "label": "Steam ID",
                "value": "STEAM_0:1:1234567"
            }
        ]
    }
]
        "#;

        let ps: Participants = serde_json::from_str(s).unwrap();
        assert_eq!(ps.0.len(), 1);
        let p = ps.0.iter().next().unwrap().clone();

        assert_eq!(p.id.unwrap().0, "378426939508809728");
        assert_eq!(p.name, "Evil Geniuses");
        let logo = p.logo.unwrap();
        assert_eq!(
            logo.icon_large_square,
            Some("http://api.toornament.com/id/icon_large_square".to_owned())
        );
        assert_eq!(
            logo.extra_small_square,
            Some("http://api.toornament.com/id/extra_small_square".to_owned())
        );
        assert_eq!(
            logo.medium_small_square,
            Some("http://api.toornament.com/id/medium_small_square".to_owned())
        );
        assert_eq!(
            logo.medium_large_square,
            Some("http://api.toornament.com/id/medium_large_square".to_owned())
        );
        assert_eq!(p.country, Some("US".to_owned()));
        let lineup = p.lineup.unwrap().0;
        assert_eq!(lineup.len(), 1);
        let lp = lineup.iter().next().unwrap();
        assert!(lp.id.is_none());
        assert_eq!(lp.name, "Storm Spirit");
        assert_eq!(lp.country, Some("US".to_owned()));
        {
            let lpcfs = lp.custom_fields.clone().unwrap().0;
            assert_eq!(lpcfs.len(), 1);
            let lpcf = lpcfs.iter().next().unwrap();
            assert_eq!(lpcf.field_type, CustomFieldType::SteamId);
            assert_eq!(lpcf.label, "Steam ID");
            assert_eq!(lpcf.value, "STEAM_0:1:1234567");
        }
        assert_eq!(lp.email, Some("player@oxent.net".to_owned()));
        {
            let lpcfsp = lp.custom_fields_private.clone().unwrap().0;
            assert_eq!(lpcfsp.len(), 1);
            let lpcfp = lpcfsp.iter().next().unwrap();
            assert_eq!(lpcfp.field_type, CustomFieldType::SteamId);
            assert_eq!(lpcfp.label, "Steam ID");
            assert_eq!(lpcfp.value, "STEAM_0:1:1234567");
        }
        assert_eq!(p.email, Some("contact@oxent.net".to_owned()));
        assert_eq!(p.check_in, Some(true));
        {
            let pcfs = p.custom_fields.clone().unwrap().0;
            assert_eq!(pcfs.len(), 1);
            let pcf = pcfs.iter().next().unwrap();
            assert_eq!(pcf.field_type, CustomFieldType::SteamId);
            assert_eq!(pcf.label, "Steam ID");
            assert_eq!(pcf.value, "STEAM_0:1:1234567");
        }
        {
            let pcfsp = p.custom_fields_private.clone().unwrap().0;
            assert_eq!(pcfsp.len(), 1);
            let pcfp = pcfsp.iter().next().unwrap();
            assert_eq!(pcfp.field_type, CustomFieldType::SteamId);
            assert_eq!(pcfp.label, "Steam ID");
            assert_eq!(pcfp.value, "STEAM_0:1:1234567");
        }
    }
}