betfair_xml_parser/
data_type.rs1use serde::{Deserialize, Serialize};
3
4use crate::common::{Description, Parameter};
5
6#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
8#[serde(rename_all = "camelCase")]
9pub struct DataType {
10 pub name: String,
12 #[serde(rename = "$value")]
14 pub values: Vec<DataTypeItems>,
15}
16
17#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
19#[serde(rename_all = "camelCase")]
20#[expect(clippy::module_name_repetitions)]
21pub enum DataTypeItems {
22 Description(Description),
24 Parameter(Parameter),
26}
27
28#[cfg(test)]
29mod tests {
30 use rstest::rstest;
31 use serde_xml_rs::from_str;
32
33 use super::*;
34
35 #[rstest]
36 fn test_parse_data_type() {
37 let xml = r#"
38 <dataType name="RunnerCatalog">
39 <description>Information about the Runners (selections) in a market</description>
40 <parameter mandatory="true" name="selectionId" type="SelectionId">
41 <description>The unique id for the selection.</description>
42 </parameter>
43 <parameter name="runnerName" type="string" mandatory="true">
44 <description>The name of the runner</description>
45 </parameter>
46 <parameter mandatory="true" name="handicap" type="double">
47 <description>The handicap</description>
48 </parameter>
49 <parameter mandatory="true" name="sortPriority" type="i32">
50 <description>The sort priority of this runner</description>
51 </parameter>
52 <parameter name="metadata" type="map(string,string)">
53 <description>Metadata associated with the runner</description>
54 </parameter>
55 </dataType>
56
57 "#;
58
59 match from_str::<DataType>(xml) {
60 Ok(req) => {
61 assert_eq!(req.values.len(), 6);
62 assert_eq!(req.name, "RunnerCatalog");
63
64 assert!(req
66 .values
67 .first()
68 .map_or(false, |val| matches!(val, &DataTypeItems::Description(_))));
69 assert!(req
70 .values
71 .get(1)
72 .map_or(false, |val| matches!(val, &DataTypeItems::Parameter(_))));
73 assert!(req
74 .values
75 .get(2)
76 .map_or(false, |val| matches!(val, &DataTypeItems::Parameter(_))));
77 assert!(req
78 .values
79 .get(3)
80 .map_or(false, |val| matches!(val, &DataTypeItems::Parameter(_))));
81 assert!(req
82 .values
83 .get(4)
84 .map_or(false, |val| matches!(val, &DataTypeItems::Parameter(_))));
85 assert!(req
86 .values
87 .get(5)
88 .map_or(false, |val| matches!(val, &DataTypeItems::Parameter(_))));
89 }
90 Err(err) => {
91 log::error!("Failed to parse XML: {err}");
92 }
93 }
94 }
95}