betfair_xml_parser/common.rs
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
//! Common elements of the XML files
use serde::{Deserialize, Serialize};
/// Valid values - used to represent an enum
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ValidValues {
/// Vector of possible values
#[serde(rename = "$value")]
pub items: Vec<Value>,
}
/// A value of a valid value
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Value {
/// The id of the value
pub id: Option<String>,
/// The name of the value
pub name: String,
/// The description of the value
pub description: Description,
}
/// The description tag
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Description {
/// The value of the description
#[serde(rename = "$value")]
pub value: Option<String>,
}
/// The parameter tag
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Parameter {
/// Whether the parameter is mandatory
pub mandatory: Option<bool>,
/// The name of the parameter
pub name: String,
/// The type of the parameter
pub r#type: String,
/// Vector of possible values enclosed within the parameter
#[serde(rename = "$value")]
pub items: Vec<ParameterItem>,
}
/// A child item of the <parameter> tag
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum ParameterItem {
/// The description tag
Description(Description),
/// The valid values tag
ValidValues(ValidValues),
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use serde_xml_rs::from_str;
use super::*;
#[rstest]
fn parameter_test() {
let xml = r#"
<parameter mandatory="false" name="total" type="double">
<description>Set a limit on total (matched + unmatched) bet exposure on market group</description>
</parameter>
"#;
let parameter = from_str::<Parameter>(xml);
match parameter {
Ok(req) => {
assert_eq!(
req,
Parameter {
mandatory: Some(false),
name: "total".to_owned(),
r#type: "double".to_owned(),
items: vec![ParameterItem::Description(Description {
value: Some(
"Set a limit on total (matched + unmatched) bet exposure on market group"
.to_owned()
)
})]
}
);
}
Err(err) => {
log::error!("Failed to parse XML: {err}");
}
}
}
#[rstest]
fn parameter_test_2() {
let xml = r#"
<parameter name="errorCode" type="string">
<description>the unique code for this error</description>
<validValues>
<value id="1" name="TOO_MUCH_DATA">
<description>The operation requested too much data</description>
</value>
<value id="2" name="INVALID_INPUT_DATA">
<description>Invalid input data</description>
</value>
</validValues>
</parameter>
"#;
let parameter = from_str::<Parameter>(xml);
match parameter {
Ok(req) => {
assert_eq!(
req,
Parameter {
mandatory: None,
name: "errorCode".to_owned(),
r#type: "string".to_owned(),
items: vec![
ParameterItem::Description(Description {
value: Some("the unique code for this error".to_owned())
}),
ParameterItem::ValidValues(ValidValues {
items: vec![
Value {
id: Some("1".to_owned()),
name: "TOO_MUCH_DATA".to_owned(),
description: Description {
value: Some(
"The operation requested too much data".to_owned()
)
}
},
Value {
id: Some("2".to_owned()),
name: "INVALID_INPUT_DATA".to_owned(),
description: Description {
value: Some("Invalid input data".to_owned())
}
}
]
})
]
}
);
}
Err(err) => {
log::error!("Failed to parse XML: {err}");
}
}
}
}