#[derive(Clone, Default, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct ParticipantId(pub String);
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ParticipantType {
Team,
Single,
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct ParticipantLogo {
#[serde(skip_serializing_if = "Option::is_none")]
pub icon_large_square: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub extra_small_square: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub medium_small_square: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub medium_large_square: Option<String>,
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum CustomFieldType {
#[serde(rename = "steam_player_id")]
SteamId,
#[serde(rename = "birth_date")]
Birthdate,
#[serde(rename = "facebook")]
Facebook,
#[serde(rename = "full_name")]
Fullname,
#[serde(rename = "instagram")]
Instagram,
#[serde(rename = "snapchat")]
Snapchat,
#[serde(rename = "text")]
Text,
#[serde(rename = "twitch")]
Twitch,
#[serde(rename = "twitter")]
Twitter,
#[serde(rename = "vimeo")]
Vimeo,
#[serde(rename = "website")]
Website,
#[serde(rename = "youtube")]
Youtube,
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct CustomField {
#[serde(rename = "type")]
pub field_type: CustomFieldType,
pub label: String,
pub value: String,
}
#[derive(Clone, Default, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct CustomFields(pub Vec<CustomField>);
#[derive(Clone, Default, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Participant {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<ParticipantId>,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub logo: Option<ParticipantLogo>,
#[serde(skip_serializing_if = "Option::is_none")]
pub lineup: Option<Participants>,
#[serde(skip_serializing_if = "Option::is_none")]
pub custom_fields: Option<CustomFields>,
#[serde(skip_serializing_if = "Option::is_none")]
pub country: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub check_in: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub custom_fields_private: Option<CustomFields>,
}
impl 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);
}
#[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");
}
}
}