From e17dbbb605d5f6626bab1b322ffdd112ac81904e Mon Sep 17 00:00:00 2001 From: Roman Valls Guimera Date: Mon, 8 Feb 2021 21:45:01 +1100 Subject: [PATCH] Migrate yaserde examples from its own repo to its main one: https://github.com/media-io/yaserde_examples --- examples/bbigras_namespace.rs | 111 ++++++++++++ examples/boscop.rs | 124 +++++++++++++ examples/lib.rs | 11 ++ examples/ln_dom.rs | 86 +++++++++ tests/data/bbigras-namespace.xml | 9 + tests/data/boscop.xml | 291 +++++++++++++++++++++++++++++++ tests/data/ln-dom.xml | 12 ++ 7 files changed, 644 insertions(+) create mode 100644 examples/bbigras_namespace.rs create mode 100644 examples/boscop.rs create mode 100644 examples/lib.rs create mode 100644 examples/ln_dom.rs create mode 100644 tests/data/bbigras-namespace.xml create mode 100644 tests/data/boscop.xml create mode 100644 tests/data/ln-dom.xml diff --git a/examples/bbigras_namespace.rs b/examples/bbigras_namespace.rs new file mode 100644 index 0000000..7d9b228 --- /dev/null +++ b/examples/bbigras_namespace.rs @@ -0,0 +1,111 @@ +// related to issue https://github.com/media-io/yaserde/issues/15 +use std::io::Read; +use yaserde::YaDeserialize; + +#[derive(YaDeserialize, Default, Debug, PartialEq)] +#[yaserde( + prefix="ss", + namespace="x: urn:schemas-microsoft-com:office:excel", + namespace="ss: urn:schemas-microsoft-com:office:spreadsheet", + namespace="o: urn:schemas-microsoft-com:office:office", + namespace="html: http://www.w3.org/TR/REC-html40" +)] +struct Workbook { + #[yaserde(rename = "Worksheet")] + worksheet: Worksheet, +} + +#[derive(YaDeserialize, Default, Debug, PartialEq)] +#[yaserde( + prefix="ss", + namespace="x: urn:schemas-microsoft-com:office:excel", + namespace="ss: urn:schemas-microsoft-com:office:spreadsheet", + namespace="o: urn:schemas-microsoft-com:office:office", + namespace="html: http://www.w3.org/TR/REC-html40" +)] +struct Worksheet { + #[yaserde(rename = "Table")] + table: Table, + #[yaserde(attribute, rename = "Name", prefix = "ss")] + ws_name: String, +} + +#[derive(YaDeserialize, Default, Debug, PartialEq)] +#[yaserde( + prefix="ss", + namespace="x: urn:schemas-microsoft-com:office:excel", + namespace="ss: urn:schemas-microsoft-com:office:spreadsheet", + namespace="o: urn:schemas-microsoft-com:office:office", + namespace="html: http://www.w3.org/TR/REC-html40" +)] +struct Table { + #[yaserde(attribute, rename = "ExpandedColumnCount", prefix = "ss")] + expanded_column_count: u32, + #[yaserde(attribute, rename = "ExpandedRowCount", prefix = "ss")] + expanded_row_count: u32, + #[yaserde(attribute, rename = "FullColumns", prefix = "x")] + full_columns: u32, + #[yaserde(attribute, rename = "FullRows", prefix = "x")] + full_rows: u32, + #[yaserde(attribute, rename = "StyleID", prefix = "ss")] + style_id: String, + #[yaserde(attribute, rename = "DefaultColumnWidth", prefix = "ss")] + default_column_width: f32, + #[yaserde(attribute, rename = "DefaultRowHeight", prefix = "ss")] + default_column_height: f32, + + #[yaserde(rename = "Row")] + rows: Vec +} + + +#[derive(YaDeserialize, Default, Debug, PartialEq)] +#[yaserde( + prefix="ss", + namespace="x: urn:schemas-microsoft-com:office:excel", + namespace="ss: urn:schemas-microsoft-com:office:spreadsheet", + namespace="o: urn:schemas-microsoft-com:office:office", + namespace="html: http://www.w3.org/TR/REC-html40" +)] +struct Row { + #[yaserde(attribute, rename = "AutoFitHeight", prefix = "ss")] + auto_fit_height: f32, + #[yaserde(attribute, rename = "Height", prefix = "ss")] + height: f32, +} + +#[test] +fn parsing_bbigras_namespace() { + use std::fs::File; + use yaserde::de::from_str; + + let filename = "tests/data/bbigras-namespace.xml"; + let mut f = File::open(filename).expect("file not found"); + + let mut contents = String::new(); + f.read_to_string(&mut contents) + .expect("something went wrong reading the file"); + + let loaded: Workbook = from_str(&contents).unwrap(); + println!("{:?}", loaded); + + let reference = Workbook { + worksheet: Worksheet { + ws_name: "some_name".to_string(), + table: Table { + expanded_column_count: 11, + expanded_row_count: 195, + full_columns: 1, + full_rows: 1, + style_id: "s64".to_string(), + default_column_width: 60.75, + default_column_height: 15.0, + rows: vec![ + Row { auto_fit_height: 0.0, height: 33.0 } + ] + } + } + }; + + assert_eq!(loaded, reference); +} diff --git a/examples/boscop.rs b/examples/boscop.rs new file mode 100644 index 0000000..3e40963 --- /dev/null +++ b/examples/boscop.rs @@ -0,0 +1,124 @@ +// related to issue https://github.com/media-io/yaserde/issues/3 +use std::io::Read; +use yaserde::YaDeserialize; + +#[derive(Default, Debug, Clone, PartialEq, YaDeserialize)] +#[yaserde(root = "layout")] +pub struct Layout { + #[yaserde(attribute)] + pub version: u32, + #[yaserde(attribute)] + pub mode: u32, + #[yaserde(attribute)] + pub w: u32, + #[yaserde(attribute)] + pub h: u32, + #[yaserde(attribute)] + pub orientation: String, + pub tabpage: Vec, +} + +#[derive(Default, Debug, Clone, PartialEq, YaDeserialize)] +pub struct Tabpage { + #[yaserde(attribute, rename = "name")] + pub named: String, + #[yaserde(attribute)] + pub scalef: f32, + #[yaserde(attribute)] + pub scalet: f32, + #[yaserde(attribute)] + pub li_t: String, + #[yaserde(attribute)] + pub li_c: String, + #[yaserde(attribute)] + pub li_s: u32, + #[yaserde(attribute)] + pub li_o: bool, + #[yaserde(attribute)] + pub li_b: bool, + #[yaserde(attribute)] + pub la_t: String, + #[yaserde(attribute)] + pub la_c: String, + #[yaserde(attribute)] + pub la_s: u32, + #[yaserde(attribute)] + pub la_o: bool, + #[yaserde(attribute)] + pub la_b: bool, + pub control: Vec, +} + +#[derive(Default, Debug, Clone, PartialEq, YaDeserialize)] +pub struct Control { + #[yaserde(attribute, rename = "name")] + pub named: String, + #[yaserde(attribute)] + pub x: u32, + #[yaserde(attribute)] + pub y: u32, + #[yaserde(attribute)] + pub w: u32, + #[yaserde(attribute)] + pub h: u32, + #[yaserde(attribute)] + pub color: String, + #[yaserde(attribute)] + pub scalef: f32, + #[yaserde(attribute)] + pub scalet: f32, + #[yaserde(attribute)] + pub local_off: bool, + #[yaserde(attribute)] + pub sp: bool, + #[yaserde(attribute)] + pub sr: bool, + pub midi: Vec, + #[yaserde(attribute)] + pub response: String, + #[yaserde(attribute)] + pub inverted: String, + #[yaserde(attribute)] + pub centered: String, + #[yaserde(attribute)] + pub norollover: String, +} + +#[derive(Default, Debug, Clone, PartialEq, YaDeserialize)] +pub struct Midi { + #[yaserde(attribute)] + pub var: String, + #[yaserde(attribute, rename = "type")] + pub typ: String, + #[yaserde(attribute)] + pub channel: String, + #[yaserde(attribute)] + pub data1: String, + #[yaserde(attribute)] + pub data2f: String, + #[yaserde(attribute)] + pub data2t: String, + #[yaserde(attribute)] + pub sysex: String, +} + +#[test] +fn parsing_bbigras_namespace() { + use std::fs::File; + use yaserde::de::from_str; + + let filename = "tests/data/boscop.xml"; + let mut f = File::open(filename).expect("file not found"); + + let mut contents = String::new(); + f.read_to_string(&mut contents) + .expect("something went wrong reading the file"); + + let loaded: Layout = from_str(&contents).unwrap(); + + assert_eq!(loaded.tabpage.len(), 4); + assert_eq!(loaded.tabpage[0].control.len(), 13); + assert_eq!(loaded.tabpage[1].control.len(), 16); + assert_eq!(loaded.tabpage[2].control.len(), 65); + assert_eq!(loaded.tabpage[3].control.len(), 40); +} diff --git a/examples/lib.rs b/examples/lib.rs new file mode 100644 index 0000000..ae21c69 --- /dev/null +++ b/examples/lib.rs @@ -0,0 +1,11 @@ +#[macro_use] +extern crate log; +extern crate simple_logger; +extern crate xml; +extern crate yaserde; +#[macro_use] +extern crate yaserde_derive; + +mod bbigras_namespace; +mod boscop; +mod ln_dom; diff --git a/examples/ln_dom.rs b/examples/ln_dom.rs new file mode 100644 index 0000000..657134d --- /dev/null +++ b/examples/ln_dom.rs @@ -0,0 +1,86 @@ +// related to issue https://github.com/media-io/yaserde/issues/11 +use std::io::Read; +use yaserde::YaDeserialize; + +#[derive(YaDeserialize, Default, Debug, PartialEq)] +#[yaserde(root="DOMSymbolItem")] +struct Level { + #[yaserde(attribute)] + last_modified: u64, + #[yaserde(attribute, rename = "name")] + named: String, + timeline: Timeline, +} + +#[derive(YaDeserialize, Default, Debug, PartialEq)] +struct Timeline { + #[yaserde(rename = "DOMTimeline")] + timeline: DOMTimeline +} + +#[derive(YaDeserialize, Default, Debug, PartialEq)] +struct DOMTimeline { + #[yaserde(attribute, rename = "name")] + named: String, + #[yaserde(attribute)] + current_frame: u64, + #[yaserde(attribute)] + guides: u64, + layers: Layers +} + +#[derive(YaDeserialize, Default, Debug, PartialEq)] +struct Layers { + #[yaserde(rename = "DOMLayer")] + dom_layer: Vec +} + +#[derive(YaDeserialize, Default, Debug, PartialEq)] +struct DOMLayer { + #[yaserde(attribute, rename = "name")] + named: String, + #[yaserde(attribute)] + name2: String +} + +#[test] +fn parsing_ln_dom() { + use std::fs::File; + use yaserde::de::from_str; + + let filename = "tests/data/ln-dom.xml"; + let mut f = File::open(filename).expect("file not found"); + + let mut contents = String::new(); + f.read_to_string(&mut contents) + .expect("something went wrong reading the file"); + + let loaded: Level = from_str(&contents).unwrap(); + println!("{:?}", loaded); + + let reference = Level { + last_modified: 1414141442, + named: "dagger".to_string(), + timeline: Timeline { + timeline: DOMTimeline { + named: "dagger timeline name".to_string(), + current_frame: 7, + guides: 11, + layers: Layers { + dom_layer: vec![ + DOMLayer { + named: "Layer 2".to_string(), + name2: "Lalayer 2".to_string() + }, + DOMLayer { + named: "Layer 1".to_string(), + name2: "Lalayer 1".to_string() + } + ] + } + } + } + }; + + assert_eq!(loaded, reference); +} diff --git a/tests/data/bbigras-namespace.xml b/tests/data/bbigras-namespace.xml new file mode 100644 index 0000000..209b3b2 --- /dev/null +++ b/tests/data/bbigras-namespace.xml @@ -0,0 +1,9 @@ + + + + + + +
+
+
diff --git a/tests/data/boscop.xml b/tests/data/boscop.xml new file mode 100644 index 0000000..96e6f58 --- /dev/null +++ b/tests/data/boscop.xml @@ -0,0 +1,291 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/data/ln-dom.xml b/tests/data/ln-dom.xml new file mode 100644 index 0000000..81d14d8 --- /dev/null +++ b/tests/data/ln-dom.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + +