From d8d84313df77ff08857d22a57cd19e4893dea666 Mon Sep 17 00:00:00 2001 From: Roman Valls Guimera Date: Mon, 8 Feb 2021 20:27:16 +1100 Subject: [PATCH 01/12] Initial serialize documentation, showcasing XML attributes and children. Left to document or fix are: dependencies and pretty-printing --- examples/svd/Cargo.toml | 13 +++ examples/svd/main.rs | 184 ++++++++++++++++++++++++++++++++++++++++ yaserde/src/lib.rs | 72 ++++++++++++++++ 3 files changed, 269 insertions(+) create mode 100644 examples/svd/Cargo.toml create mode 100644 examples/svd/main.rs diff --git a/examples/svd/Cargo.toml b/examples/svd/Cargo.toml new file mode 100644 index 0000000..ae31f81 --- /dev/null +++ b/examples/svd/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "v850-svd" +version = "0.1.0" +authors = ["Roman Valls Guimera "] +edition = "2018" + +[dependencies] +# serde = { version = "1.0.123", features = [ "derive" ] } +# quick-xml = { version = "0.21.0", features = [ "serialize" ] } +yaserde = "0.5.1" +yaserde_derive = "0.5.1" +xml-rs = "0.8.3" +log = "0.4" \ No newline at end of file diff --git a/examples/svd/main.rs b/examples/svd/main.rs new file mode 100644 index 0000000..f988281 --- /dev/null +++ b/examples/svd/main.rs @@ -0,0 +1,184 @@ +use yaserde_derive::YaSerialize; + +#[derive(Default, PartialEq, Debug, YaSerialize)] +struct CpuDef { + #[yaserde(child)] + name: String, + #[yaserde(child)] + revision: String, + #[yaserde(child)] + endian: String, // enum {LE, BE, ME} + #[yaserde(child)] + mpupresent: bool, + #[yaserde(child)] + fpupresent: bool, + //#[yaserde(child)] + //nvicpriobits: enum {8, 16, 32, 64, 128}, + #[yaserde(child)] + vendorsystickconfig: bool +} + +#[derive(Default, PartialEq, Debug, YaSerialize)] +struct Field { + name: String, + #[yaserde(child)] + description: String, + #[yaserde(child)] + bitrange: String, + #[yaserde(child)] + access: String, +} + +#[derive(Default, PartialEq, Debug, YaSerialize)] +struct Register { + #[yaserde(child)] + name: String, + #[yaserde(child)] + description: String, + #[yaserde(child)] + addressoffset: String, + #[yaserde(child)] + size: u8, + #[yaserde(child)] + access: String, + #[yaserde(child)] + resetvalue: String, + #[yaserde(child)] + resetmask: String, + #[yaserde(child)] + fields: Vec +} + +#[derive(Default, PartialEq, Debug, YaSerialize)] +struct Peripheral { + #[yaserde(child)] + name: String, + #[yaserde(child)] + version: String, + #[yaserde(child)] + description: String, + #[yaserde(child)] + groupname: String, + #[yaserde(child)] + baseaddress: String, + #[yaserde(child)] + size: u8, + #[yaserde(child)] + access: String, + #[yaserde(child)] + registers: Vec +} + +#[derive(Default, PartialEq, Debug, YaSerialize)] +struct DevAttrs { + #[yaserde(child)] + vendor: String, + #[yaserde(child)] + vendorid: String, + #[yaserde(child)] + name: String, + #[yaserde(child)] + series: String, + #[yaserde(child)] + version: String, + #[yaserde(child)] + description: String, + #[yaserde(child)] + licensetext: String, + #[yaserde(child)] + cpu: CpuDef, + #[yaserde(child)] + addressunitbits: u8, + #[yaserde(child)] + width: u8, + #[yaserde(child)] + size: u8, + #[yaserde(child)] + access: String, + #[yaserde(child)] + resetvalue: String, + #[yaserde(child)] + resetmask: String, + #[yaserde(child)] + peripherals: Vec +} + +#[derive(Default, PartialEq, Debug, YaSerialize)] +#[yaserde(rename = "device")] +struct Device { + #[yaserde(attribute)] + schemaversion: String, + #[yaserde(attribute)] + xmlns: String, + #[yaserde(attribute)] + xsnonamespaceschemalocation: String, + #[yaserde(child)] + devattributes: DevAttrs +} + +fn main() { + + let mut vec_peripherals: Vec = Vec::new(); + let mut vec_registers: Vec = Vec::new(); + let vec_fields: Vec = Vec::new(); + + let register = Register { + name: "PRCMD".to_string(), + description: "This command register (PRCMD) is to protect the registers that may have a significant influence on the application system (PSC, PSM) from an inadvertent write access, so that the system does not stop in case of a program hang-up.".to_string(), + addressoffset: "0xFFFFF1FC".to_string(), + size: 8, + access: "read-write".to_string(), + resetvalue: "0x0000".to_string(), + resetmask: "0xFFFF".to_string(), + fields: vec_fields + }; + vec_registers.push(register); + + let peripheral = Peripheral { + name: "Specific Registers".to_string(), + version: "1.0".to_string(), + description: "Specific Registers".to_string(), + groupname: "MCU".to_string(), + baseaddress: "0xFFFFF1FC".to_string(), + size: 16, + access: "read-write".to_string(), + registers: vec_registers + }; + vec_peripherals.push(peripheral); + + let cpu_def = CpuDef { + name: "V850".to_string(), + revision: "r1".to_string(), + endian: "LE".to_string(), // enum {LE, BE, ME} + mpupresent: false, + fpupresent: false, + //nvicpriobits: enum {8, 16, 32, 64, 128}, + vendorsystickconfig: false + }; + + let dev_attrs = DevAttrs { + vendor: "Renesas".to_string(), + vendorid: "Renesas".to_string(), + name: "V850".to_string(), + series: "E1/E2/CA2".to_string(), + version: "1.2".to_string(), + description: "NEC/Renesas V850 automotive grade ICs".to_string(), + licensetext: "GPLv3".to_string(), + cpu: cpu_def, + addressunitbits: 8, + width: 32, + size: 32, + access: "read-write".to_string(), + resetvalue: "0x00000000".to_string(), + resetmask: "0xFFFFFFFF".to_string(), + peripherals: vec_peripherals + }; + + let dev = Device { schemaversion: "foo".to_string(), + xmlns: "http://www.w3.org/2001/XMLSchema-instance".to_string(), + xsnonamespaceschemalocation: "CMSIS-SVD.xsd".to_string(), + devattributes: dev_attrs + }; + + println!("{:?}", yaserde::ser::to_string(&dev).ok().unwrap()); +} \ No newline at end of file diff --git a/yaserde/src/lib.rs b/yaserde/src/lib.rs index 5adce88..005d3a7 100644 --- a/yaserde/src/lib.rs +++ b/yaserde/src/lib.rs @@ -2,6 +2,78 @@ //! //! YaSerDe is a framework for ***ser***ializing and ***de***serializing Rust data //! structures efficiently and generically from and into XML. +//! +//! YaSerDe makes it easy to serialize XML documents given an properly annotated struct. +//! Please refer to the `examples` directory for the complete code shown below. +//! +//! # Serialize +//! +//! For instance, let's say that one wants to generate a XML file for the +//! [Rust-Embedded community](https://github.com/rust-embedded/). A well known XML +//! file for microcontrollers is called [SVD](https://github.com/rust-embedded/svd/) +//! and it can be defined on YaSerDe via structs like so: +//! +//!``` +//! use yaserde_derive::YaSerialize; +//! +//! #[derive(Default, PartialEq, Debug, YaSerialize)] +//! #[yaserde(rename = "device")] +//! struct Device { +//! #[yaserde(attribute)] +//! schemaversion: String, +//! #[yaserde(attribute)] +//! xmlns: String, +//! #[yaserde(attribute)] +//! xsnonamespaceschemalocation: String, +//! #[yaserde(child)] +//! devattributes: DevAttrs +//! } +//! (...) +//!``` +//! +//! The interspersed `#[yaserde()]` macros give some indication of what the resulting XML +//! Will look like, namely, a short snippet of the struct above in XML would be depending on +//! concrete values passed to the struct (not shown): +//! +//!``` +//! +//! +//! +//! (...) +//!``` +//! +//! Notice the important difference in **XML output representation between `attributes` vs +//! `child`**, since SVD expects information in that particular arrangement. YaSerDe allows that +//! serialized XML to be valid unlike other Rust XML (de)serialization crates (i.e quick-xml). +//! +//! Also the last `DevAttrs` struct field is indeed another struct, so one can chain several +//! structs to compose the XML structure (again, see examples folder for the complete +//! example). +//! +//! Be mindful that the **Cargo.toml** should not only include `yaserde` and +//! `yaserde_derive`, but also a few necessary dependencies... [FIXME: THAT FOR SOME +//! USER-UNFRIENDLY REASON ARE NOT AUTOMATICALLY PULLED IN AS ONE WOULD EXPECT ;P ;P +//! ... I'm sure there are good reasons, just wanted to leave this like this so that the author +//! can chip in and comment about the reasons behind that decision.](https://github.com/media-io/yaserde/issues/22) ... **I personally think that issue #22 should be reopened and fixed properly (as in only requiring yaserde as a dependency, adding yaserde_derive as a feature).** +//! +//! ``` +//! [dependencies] +//! # serde = { version = "1.0.123", features = [ "derive" ] } +//! # quick-xml = { version = "0.21.0", features = [ "serialize" ] } +//! yaserde = "0.5.1" +//! yaserde_derive = "0.5.1" +//! xml-rs = "0.8.3" +//! log = "0.4" +//! ``` +//! +//! Last but not least, in order to have a nice, pretty printed XML output one can do +//! +//! ``` +//! PLEASE LET THE USERS KNOW HOW TO DO THAT CLEARLY ON YASERDE??? +//! ``` +//! +//! FIXME: For now I'm just resorting to online XML linters and formatters :_S #[macro_use] extern crate log; From db005b568fa64b18617f837b1077814cb950f53d Mon Sep 17 00:00:00 2001 From: Roman Valls Guimera Date: Mon, 8 Feb 2021 20:45:45 +1100 Subject: [PATCH 02/12] Cargo fmt --- yaserde/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yaserde/src/lib.rs b/yaserde/src/lib.rs index 005d3a7..cefa452 100644 --- a/yaserde/src/lib.rs +++ b/yaserde/src/lib.rs @@ -8,9 +8,9 @@ //! //! # Serialize //! -//! For instance, let's say that one wants to generate a XML file for the -//! [Rust-Embedded community](https://github.com/rust-embedded/). A well known XML -//! file for microcontrollers is called [SVD](https://github.com/rust-embedded/svd/) +//! For instance, let's say that one wants to generate a XML file for the +//! [Rust-Embedded community](https://github.com/rust-embedded/). A well known XML +//! file for microcontrollers is called [SVD](https://github.com/rust-embedded/svd/) //! and it can be defined on YaSerDe via structs like so: //! //!``` From e412777f36482eae2cde6e687c5a3307401ac696 Mon Sep 17 00:00:00 2001 From: Roman Valls Guimera Date: Mon, 8 Feb 2021 21:36:03 +1100 Subject: [PATCH 03/12] Avoid rustdoc testing the code under rust crate-level block --- yaserde/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/yaserde/src/lib.rs b/yaserde/src/lib.rs index cefa452..185ff5e 100644 --- a/yaserde/src/lib.rs +++ b/yaserde/src/lib.rs @@ -13,7 +13,7 @@ //! file for microcontrollers is called [SVD](https://github.com/rust-embedded/svd/) //! and it can be defined on YaSerDe via structs like so: //! -//!``` +//!```verbatim //! use yaserde_derive::YaSerialize; //! //! #[derive(Default, PartialEq, Debug, YaSerialize)] @@ -28,14 +28,14 @@ //! #[yaserde(child)] //! devattributes: DevAttrs //! } -//! (...) //!``` //! //! The interspersed `#[yaserde()]` macros give some indication of what the resulting XML //! Will look like, namely, a short snippet of the struct above in XML would be depending on //! concrete values passed to the struct (not shown): //! -//!``` +//!```xml +//! (...) //! //! @@ -57,7 +57,7 @@ //! ... I'm sure there are good reasons, just wanted to leave this like this so that the author //! can chip in and comment about the reasons behind that decision.](https://github.com/media-io/yaserde/issues/22) ... **I personally think that issue #22 should be reopened and fixed properly (as in only requiring yaserde as a dependency, adding yaserde_derive as a feature).** //! -//! ``` +//!```toml //! [dependencies] //! # serde = { version = "1.0.123", features = [ "derive" ] } //! # quick-xml = { version = "0.21.0", features = [ "serialize" ] } @@ -69,7 +69,7 @@ //! //! Last but not least, in order to have a nice, pretty printed XML output one can do //! -//! ``` +//! ```shell //! PLEASE LET THE USERS KNOW HOW TO DO THAT CLEARLY ON YASERDE??? //! ``` //! From e17dbbb605d5f6626bab1b322ffdd112ac81904e Mon Sep 17 00:00:00 2001 From: Roman Valls Guimera Date: Mon, 8 Feb 2021 21:45:01 +1100 Subject: [PATCH 04/12] 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 @@ + + + + + + + + + + + + From 037dd58453e2d7bc59dd3b1b187af0f40b886754 Mon Sep 17 00:00:00 2001 From: Roman Valls Guimera Date: Mon, 8 Feb 2021 21:50:27 +1100 Subject: [PATCH 05/12] Restructured as @MarcAntoine-Arnaud suggested: examples workspace, more amenable to CI and examples are easier to find than in https://github.com/media-io/yaserde_examples --- Cargo.toml | 1 + examples/Cargo.toml | 12 ++++++++++++ examples/{ => src}/bbigras_namespace.rs | 0 examples/{ => src}/boscop.rs | 0 examples/{ => src}/lib.rs | 1 - examples/{ => src}/ln_dom.rs | 0 examples/{ => src}/svd/Cargo.toml | 0 examples/{ => src}/svd/main.rs | 0 {tests => examples/tests}/data/bbigras-namespace.xml | 0 {tests => examples/tests}/data/boscop.xml | 0 {tests => examples/tests}/data/ln-dom.xml | 0 11 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 examples/Cargo.toml rename examples/{ => src}/bbigras_namespace.rs (100%) rename examples/{ => src}/boscop.rs (100%) rename examples/{ => src}/lib.rs (93%) rename examples/{ => src}/ln_dom.rs (100%) rename examples/{ => src}/svd/Cargo.toml (100%) rename examples/{ => src}/svd/main.rs (100%) rename {tests => examples/tests}/data/bbigras-namespace.xml (100%) rename {tests => examples/tests}/data/boscop.xml (100%) rename {tests => examples/tests}/data/ln-dom.xml (100%) diff --git a/Cargo.toml b/Cargo.toml index fe4f64d..801550b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,4 +2,5 @@ members = [ "yaserde", "yaserde_derive", + "examples" ] diff --git a/examples/Cargo.toml b/examples/Cargo.toml new file mode 100644 index 0000000..8b797c3 --- /dev/null +++ b/examples/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "yaserde-examples" +version = "0.0.1" +authors = ["Marc-Antoine Arnaud "] +license = "MIT" + +[dependencies] +yaserde_derive = { version = "0.3.7" } +yaserde = { version = "0.3.7" } +xml-rs = "0.8.0" +log = "0.4" +simple_logger = "1.0" diff --git a/examples/bbigras_namespace.rs b/examples/src/bbigras_namespace.rs similarity index 100% rename from examples/bbigras_namespace.rs rename to examples/src/bbigras_namespace.rs diff --git a/examples/boscop.rs b/examples/src/boscop.rs similarity index 100% rename from examples/boscop.rs rename to examples/src/boscop.rs diff --git a/examples/lib.rs b/examples/src/lib.rs similarity index 93% rename from examples/lib.rs rename to examples/src/lib.rs index ae21c69..1943c57 100644 --- a/examples/lib.rs +++ b/examples/src/lib.rs @@ -1,4 +1,3 @@ -#[macro_use] extern crate log; extern crate simple_logger; extern crate xml; diff --git a/examples/ln_dom.rs b/examples/src/ln_dom.rs similarity index 100% rename from examples/ln_dom.rs rename to examples/src/ln_dom.rs diff --git a/examples/svd/Cargo.toml b/examples/src/svd/Cargo.toml similarity index 100% rename from examples/svd/Cargo.toml rename to examples/src/svd/Cargo.toml diff --git a/examples/svd/main.rs b/examples/src/svd/main.rs similarity index 100% rename from examples/svd/main.rs rename to examples/src/svd/main.rs diff --git a/tests/data/bbigras-namespace.xml b/examples/tests/data/bbigras-namespace.xml similarity index 100% rename from tests/data/bbigras-namespace.xml rename to examples/tests/data/bbigras-namespace.xml diff --git a/tests/data/boscop.xml b/examples/tests/data/boscop.xml similarity index 100% rename from tests/data/boscop.xml rename to examples/tests/data/boscop.xml diff --git a/tests/data/ln-dom.xml b/examples/tests/data/ln-dom.xml similarity index 100% rename from tests/data/ln-dom.xml rename to examples/tests/data/ln-dom.xml From 9ba62812c6a7725d1fbddc5de0540c234c13acb1 Mon Sep 17 00:00:00 2001 From: Roman Valls Guimera Date: Mon, 8 Feb 2021 22:02:38 +1100 Subject: [PATCH 06/12] Remove sub-sub projects, thx @MarcAntoine-Aranaud ;) --- examples/src/{svd/main.rs => svd.rs} | 0 examples/src/svd/Cargo.toml | 13 ------------- 2 files changed, 13 deletions(-) rename examples/src/{svd/main.rs => svd.rs} (100%) delete mode 100644 examples/src/svd/Cargo.toml diff --git a/examples/src/svd/main.rs b/examples/src/svd.rs similarity index 100% rename from examples/src/svd/main.rs rename to examples/src/svd.rs diff --git a/examples/src/svd/Cargo.toml b/examples/src/svd/Cargo.toml deleted file mode 100644 index ae31f81..0000000 --- a/examples/src/svd/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "v850-svd" -version = "0.1.0" -authors = ["Roman Valls Guimera "] -edition = "2018" - -[dependencies] -# serde = { version = "1.0.123", features = [ "derive" ] } -# quick-xml = { version = "0.21.0", features = [ "serialize" ] } -yaserde = "0.5.1" -yaserde_derive = "0.5.1" -xml-rs = "0.8.3" -log = "0.4" \ No newline at end of file From 69cccbc4b8f806528147329f473e1057535e3f09 Mon Sep 17 00:00:00 2001 From: Roman Valls Guimera Date: Mon, 8 Feb 2021 22:10:40 +1100 Subject: [PATCH 07/12] Use local link path for examples --- examples/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 8b797c3..dab26f2 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -5,8 +5,8 @@ authors = ["Marc-Antoine Arnaud "] license = "MIT" [dependencies] -yaserde_derive = { version = "0.3.7" } -yaserde = { version = "0.3.7" } +yaserde = {version = "0.5.1", path = "../../yaserde" } +yaserde_derive = {version = "0.5.1", path = "../../yaserde_derive" } xml-rs = "0.8.0" log = "0.4" simple_logger = "1.0" From 3ffdd9bdcdb4355c04c0a52a5dd05158d9f60ccd Mon Sep 17 00:00:00 2001 From: Roman Valls Guimera Date: Mon, 8 Feb 2021 22:12:39 +1100 Subject: [PATCH 08/12] Rust 1.36 and 1.37 are ancient history ;P --- .travis.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.travis.yml b/.travis.yml index e15070e..d56f24c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,11 @@ language: rust rust: + # error on #[non_exhaustive] being "experimental" + # - 1.36.0 + # - 1.37.0 + # error on cfg(doctest) + # - 1.38.0 + # - 1.39.0 - 1.40.0 - 1.41.0 - 1.42.0 From 4f52732a713c7df9b6cc05b4631d45d791cd1919 Mon Sep 17 00:00:00 2001 From: Roman Valls Guimera Date: Mon, 8 Feb 2021 22:33:26 +1100 Subject: [PATCH 09/12] Ignore this rust(doc) code, thanks https://doc.rust-lang.org/rustdoc/documentation-tests.html#attributes --- yaserde/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yaserde/src/lib.rs b/yaserde/src/lib.rs index 185ff5e..a9c73e1 100644 --- a/yaserde/src/lib.rs +++ b/yaserde/src/lib.rs @@ -13,7 +13,7 @@ //! file for microcontrollers is called [SVD](https://github.com/rust-embedded/svd/) //! and it can be defined on YaSerDe via structs like so: //! -//!```verbatim +//!```ignore //! use yaserde_derive::YaSerialize; //! //! #[derive(Default, PartialEq, Debug, YaSerialize)] From e506cb1e926fcfc3e39a64c37b2d34fef963f171 Mon Sep 17 00:00:00 2001 From: Roman Valls Guimera Date: Mon, 8 Feb 2021 22:42:49 +1100 Subject: [PATCH 10/12] Add in the prettyprinting bit in the svd example, which still doesn't work yet properly in master. --- examples/src/svd.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/src/svd.rs b/examples/src/svd.rs index f988281..ac3986e 100644 --- a/examples/src/svd.rs +++ b/examples/src/svd.rs @@ -180,5 +180,10 @@ fn main() { devattributes: dev_attrs }; - println!("{:?}", yaserde::ser::to_string(&dev).ok().unwrap()); -} \ No newline at end of file + // Display pretty printed XML + let yaserde_cfg = yaserde::ser::Config{ + perform_indent: true, + .. Default::default() + }; + println!("{:#?}", yaserde::ser::to_string_with_config(&dev, &yaserde_cfg)); +} From a65fa84f75cbcdde708abf6da27814f40c9554e0 Mon Sep 17 00:00:00 2001 From: Roman Valls Guimera Date: Mon, 8 Feb 2021 22:53:01 +1100 Subject: [PATCH 11/12] @MarcAntoine-Arnaud, the local link path breaks CI if I add examples in the workspace --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 801550b..ab39b3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,5 +2,5 @@ members = [ "yaserde", "yaserde_derive", - "examples" +# "examples" ] From 6492efcfdcd68f880f66fc1b406d10d17263ae59 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Arnaud Date: Mon, 8 Feb 2021 14:10:08 +0100 Subject: [PATCH 12/12] improve contribution to add examples, fix some clippy warns --- Cargo.toml | 2 +- examples/Cargo.toml | 5 +- examples/src/bbigras_namespace.rs | 65 +++---- examples/src/boscop.rs | 12 +- examples/src/lib.rs | 5 +- examples/src/ln_dom.rs | 35 ++-- examples/src/svd.rs | 307 +++++++++++++++--------------- examples/tests/data/svd.xml | 44 +++++ yaserde/src/lib.rs | 10 +- yaserde/tests/option.rs | 56 +++--- yaserde/tests/skip_if.rs | 4 +- yaserde/tests/types.rs | 56 +++--- 12 files changed, 320 insertions(+), 281 deletions(-) create mode 100644 examples/tests/data/svd.xml diff --git a/Cargo.toml b/Cargo.toml index ab39b3b..d5e249a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,5 +2,5 @@ members = [ "yaserde", "yaserde_derive", -# "examples" + "examples", ] diff --git a/examples/Cargo.toml b/examples/Cargo.toml index dab26f2..049a067 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -3,10 +3,11 @@ name = "yaserde-examples" version = "0.0.1" authors = ["Marc-Antoine Arnaud "] license = "MIT" +edition = "2018" [dependencies] -yaserde = {version = "0.5.1", path = "../../yaserde" } -yaserde_derive = {version = "0.5.1", path = "../../yaserde_derive" } +yaserde = {version = "0.5.1", path = "../yaserde" } +yaserde_derive = {version = "0.5.1", path = "../yaserde_derive" } xml-rs = "0.8.0" log = "0.4" simple_logger = "1.0" diff --git a/examples/src/bbigras_namespace.rs b/examples/src/bbigras_namespace.rs index 7d9b228..2093db1 100644 --- a/examples/src/bbigras_namespace.rs +++ b/examples/src/bbigras_namespace.rs @@ -1,14 +1,12 @@ // 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" + 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")] @@ -17,11 +15,11 @@ struct Workbook { #[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" + 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")] @@ -32,11 +30,11 @@ struct 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" + 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")] @@ -55,17 +53,16 @@ struct Table { default_column_height: f32, #[yaserde(rename = "Row")] - rows: Vec + 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" + 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")] @@ -76,17 +73,14 @@ struct Row { #[test] fn parsing_bbigras_namespace() { - use std::fs::File; + use std::fs; 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 content = fs::read_to_string(filename).expect("something went wrong reading the file"); - let loaded: Workbook = from_str(&contents).unwrap(); + let loaded: Workbook = from_str(&content).unwrap(); println!("{:?}", loaded); let reference = Workbook { @@ -100,11 +94,12 @@ fn parsing_bbigras_namespace() { 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 } - ] - } - } + rows: vec![Row { + auto_fit_height: 0.0, + height: 33.0, + }], + }, + }, }; assert_eq!(loaded, reference); diff --git a/examples/src/boscop.rs b/examples/src/boscop.rs index 3e40963..ff0d9cf 100644 --- a/examples/src/boscop.rs +++ b/examples/src/boscop.rs @@ -1,6 +1,4 @@ // 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")] @@ -104,17 +102,13 @@ pub struct Midi { #[test] fn parsing_bbigras_namespace() { - use std::fs::File; + use std::fs; use yaserde::de::from_str; let filename = "tests/data/boscop.xml"; - let mut f = File::open(filename).expect("file not found"); + let content = fs::read_to_string(filename).expect("something went wrong reading the file"); - 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(); + let loaded: Layout = from_str(&content).unwrap(); assert_eq!(loaded.tabpage.len(), 4); assert_eq!(loaded.tabpage[0].control.len(), 13); diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 1943c57..af42128 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -1,10 +1,7 @@ -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; +mod svd; diff --git a/examples/src/ln_dom.rs b/examples/src/ln_dom.rs index 657134d..1264afa 100644 --- a/examples/src/ln_dom.rs +++ b/examples/src/ln_dom.rs @@ -1,9 +1,7 @@ // 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")] +#[yaserde(root = "DOMSymbolItem")] struct Level { #[yaserde(attribute)] last_modified: u64, @@ -15,7 +13,7 @@ struct Level { #[derive(YaDeserialize, Default, Debug, PartialEq)] struct Timeline { #[yaserde(rename = "DOMTimeline")] - timeline: DOMTimeline + timeline: DOMTimeline, } #[derive(YaDeserialize, Default, Debug, PartialEq)] @@ -26,13 +24,13 @@ struct DOMTimeline { current_frame: u64, #[yaserde(attribute)] guides: u64, - layers: Layers + layers: Layers, } #[derive(YaDeserialize, Default, Debug, PartialEq)] struct Layers { #[yaserde(rename = "DOMLayer")] - dom_layer: Vec + dom_layer: Vec, } #[derive(YaDeserialize, Default, Debug, PartialEq)] @@ -40,22 +38,19 @@ struct DOMLayer { #[yaserde(attribute, rename = "name")] named: String, #[yaserde(attribute)] - name2: String + name2: String, } #[test] fn parsing_ln_dom() { - use std::fs::File; + use std::fs; 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 content = fs::read_to_string(filename).expect("something went wrong reading the file"); - let loaded: Level = from_str(&contents).unwrap(); + let loaded: Level = from_str(&content).unwrap(); println!("{:?}", loaded); let reference = Level { @@ -70,16 +65,16 @@ fn parsing_ln_dom() { dom_layer: vec![ DOMLayer { named: "Layer 2".to_string(), - name2: "Lalayer 2".to_string() + name2: "Lalayer 2".to_string(), }, DOMLayer { named: "Layer 1".to_string(), - name2: "Lalayer 1".to_string() - } - ] - } - } - } + name2: "Lalayer 1".to_string(), + }, + ], + }, + }, + }, }; assert_eq!(loaded, reference); diff --git a/examples/src/svd.rs b/examples/src/svd.rs index ac3986e..a4628a9 100644 --- a/examples/src/svd.rs +++ b/examples/src/svd.rs @@ -2,127 +2,125 @@ use yaserde_derive::YaSerialize; #[derive(Default, PartialEq, Debug, YaSerialize)] struct CpuDef { - #[yaserde(child)] - name: String, - #[yaserde(child)] - revision: String, - #[yaserde(child)] - endian: String, // enum {LE, BE, ME} - #[yaserde(child)] - mpupresent: bool, - #[yaserde(child)] - fpupresent: bool, - //#[yaserde(child)] - //nvicpriobits: enum {8, 16, 32, 64, 128}, - #[yaserde(child)] - vendorsystickconfig: bool + #[yaserde(child)] + name: String, + #[yaserde(child)] + revision: String, + #[yaserde(child)] + endian: String, // enum {LE, BE, ME} + #[yaserde(child)] + mpupresent: bool, + #[yaserde(child)] + fpupresent: bool, + //#[yaserde(child)] + //nvicpriobits: enum {8, 16, 32, 64, 128}, + #[yaserde(child)] + vendorsystickconfig: bool, } #[derive(Default, PartialEq, Debug, YaSerialize)] struct Field { - name: String, - #[yaserde(child)] - description: String, - #[yaserde(child)] - bitrange: String, - #[yaserde(child)] - access: String, + name: String, + #[yaserde(child)] + description: String, + #[yaserde(child)] + bitrange: String, + #[yaserde(child)] + access: String, } #[derive(Default, PartialEq, Debug, YaSerialize)] struct Register { - #[yaserde(child)] - name: String, - #[yaserde(child)] - description: String, - #[yaserde(child)] - addressoffset: String, - #[yaserde(child)] - size: u8, - #[yaserde(child)] - access: String, - #[yaserde(child)] - resetvalue: String, - #[yaserde(child)] - resetmask: String, - #[yaserde(child)] - fields: Vec + #[yaserde(child)] + name: String, + #[yaserde(child)] + description: String, + #[yaserde(child)] + addressoffset: String, + #[yaserde(child)] + size: u8, + #[yaserde(child)] + access: String, + #[yaserde(child)] + resetvalue: String, + #[yaserde(child)] + resetmask: String, + #[yaserde(child)] + fields: Vec, } #[derive(Default, PartialEq, Debug, YaSerialize)] struct Peripheral { - #[yaserde(child)] - name: String, - #[yaserde(child)] - version: String, - #[yaserde(child)] - description: String, - #[yaserde(child)] - groupname: String, - #[yaserde(child)] - baseaddress: String, - #[yaserde(child)] - size: u8, - #[yaserde(child)] - access: String, - #[yaserde(child)] - registers: Vec + #[yaserde(child)] + name: String, + #[yaserde(child)] + version: String, + #[yaserde(child)] + description: String, + #[yaserde(child)] + groupname: String, + #[yaserde(child)] + baseaddress: String, + #[yaserde(child)] + size: u8, + #[yaserde(child)] + access: String, + #[yaserde(child)] + registers: Vec, } #[derive(Default, PartialEq, Debug, YaSerialize)] struct DevAttrs { - #[yaserde(child)] - vendor: String, - #[yaserde(child)] - vendorid: String, - #[yaserde(child)] - name: String, - #[yaserde(child)] - series: String, - #[yaserde(child)] - version: String, - #[yaserde(child)] - description: String, - #[yaserde(child)] - licensetext: String, - #[yaserde(child)] - cpu: CpuDef, - #[yaserde(child)] - addressunitbits: u8, - #[yaserde(child)] - width: u8, - #[yaserde(child)] - size: u8, - #[yaserde(child)] - access: String, - #[yaserde(child)] - resetvalue: String, - #[yaserde(child)] - resetmask: String, - #[yaserde(child)] - peripherals: Vec + #[yaserde(child)] + vendor: String, + #[yaserde(child)] + vendorid: String, + #[yaserde(child)] + name: String, + #[yaserde(child)] + series: String, + #[yaserde(child)] + version: String, + #[yaserde(child)] + description: String, + #[yaserde(child)] + licensetext: String, + #[yaserde(child)] + cpu: CpuDef, + #[yaserde(child)] + addressunitbits: u8, + #[yaserde(child)] + width: u8, + #[yaserde(child)] + size: u8, + #[yaserde(child)] + access: String, + #[yaserde(child)] + resetvalue: String, + #[yaserde(child)] + resetmask: String, + #[yaserde(child)] + peripherals: Vec, } #[derive(Default, PartialEq, Debug, YaSerialize)] #[yaserde(rename = "device")] struct Device { - #[yaserde(attribute)] - schemaversion: String, - #[yaserde(attribute)] - xmlns: String, - #[yaserde(attribute)] - xsnonamespaceschemalocation: String, - #[yaserde(child)] - devattributes: DevAttrs + #[yaserde(attribute)] + schemaversion: String, + #[yaserde(attribute)] + xmlns: String, + #[yaserde(attribute)] + xsnonamespaceschemalocation: String, + #[yaserde(child)] + devattributes: DevAttrs, } -fn main() { +#[test] +fn parsing_svd() { + use std::fs; - let mut vec_peripherals: Vec = Vec::new(); - let mut vec_registers: Vec = Vec::new(); - let vec_fields: Vec = Vec::new(); - - let register = Register { + let register = Register { name: "PRCMD".to_string(), description: "This command register (PRCMD) is to protect the registers that may have a significant influence on the application system (PSC, PSM) from an inadvertent write access, so that the system does not stop in case of a program hang-up.".to_string(), addressoffset: "0xFFFFF1FC".to_string(), @@ -130,60 +128,69 @@ fn main() { access: "read-write".to_string(), resetvalue: "0x0000".to_string(), resetmask: "0xFFFF".to_string(), - fields: vec_fields - }; - vec_registers.push(register); - - let peripheral = Peripheral { - name: "Specific Registers".to_string(), - version: "1.0".to_string(), - description: "Specific Registers".to_string(), - groupname: "MCU".to_string(), - baseaddress: "0xFFFFF1FC".to_string(), - size: 16, - access: "read-write".to_string(), - registers: vec_registers - }; - vec_peripherals.push(peripheral); - - let cpu_def = CpuDef { - name: "V850".to_string(), - revision: "r1".to_string(), - endian: "LE".to_string(), // enum {LE, BE, ME} - mpupresent: false, - fpupresent: false, - //nvicpriobits: enum {8, 16, 32, 64, 128}, - vendorsystickconfig: false + fields: vec![], }; - let dev_attrs = DevAttrs { - vendor: "Renesas".to_string(), - vendorid: "Renesas".to_string(), - name: "V850".to_string(), - series: "E1/E2/CA2".to_string(), - version: "1.2".to_string(), - description: "NEC/Renesas V850 automotive grade ICs".to_string(), - licensetext: "GPLv3".to_string(), - cpu: cpu_def, - addressunitbits: 8, - width: 32, - size: 32, - access: "read-write".to_string(), - resetvalue: "0x00000000".to_string(), - resetmask: "0xFFFFFFFF".to_string(), - peripherals: vec_peripherals - }; + let vec_registers = vec![register]; - let dev = Device { schemaversion: "foo".to_string(), - xmlns: "http://www.w3.org/2001/XMLSchema-instance".to_string(), - xsnonamespaceschemalocation: "CMSIS-SVD.xsd".to_string(), - devattributes: dev_attrs - }; - - // Display pretty printed XML - let yaserde_cfg = yaserde::ser::Config{ - perform_indent: true, - .. Default::default() - }; - println!("{:#?}", yaserde::ser::to_string_with_config(&dev, &yaserde_cfg)); + let peripheral = Peripheral { + name: "Specific Registers".to_string(), + version: "1.0".to_string(), + description: "Specific Registers".to_string(), + groupname: "MCU".to_string(), + baseaddress: "0xFFFFF1FC".to_string(), + size: 16, + access: "read-write".to_string(), + registers: vec_registers, + }; + + let vec_peripherals = vec![peripheral]; + + let cpu_def = CpuDef { + name: "V850".to_string(), + revision: "r1".to_string(), + endian: "LE".to_string(), // enum {LE, BE, ME} + mpupresent: false, + fpupresent: false, + //nvicpriobits: enum {8, 16, 32, 64, 128}, + vendorsystickconfig: false, + }; + + let dev_attrs = DevAttrs { + vendor: "Renesas".to_string(), + vendorid: "Renesas".to_string(), + name: "V850".to_string(), + series: "E1/E2/CA2".to_string(), + version: "1.2".to_string(), + description: "NEC/Renesas V850 automotive grade ICs".to_string(), + licensetext: "GPLv3".to_string(), + cpu: cpu_def, + addressunitbits: 8, + width: 32, + size: 32, + access: "read-write".to_string(), + resetvalue: "0x00000000".to_string(), + resetmask: "0xFFFFFFFF".to_string(), + peripherals: vec_peripherals, + }; + + let device = Device { + schemaversion: "foo".to_string(), + xmlns: "http://www.w3.org/2001/XMLSchema-instance".to_string(), + xsnonamespaceschemalocation: "CMSIS-SVD.xsd".to_string(), + devattributes: dev_attrs, + }; + + // Display pretty printed XML + let yaserde_cfg = yaserde::ser::Config { + perform_indent: true, + ..Default::default() + }; + + let serialized = yaserde::ser::to_string_with_config(&device, &yaserde_cfg).unwrap(); + + let reference = + fs::read_to_string("tests/data/svd.xml").expect("something went wrong reading the file"); + + assert_eq!(reference, serialized) } diff --git a/examples/tests/data/svd.xml b/examples/tests/data/svd.xml new file mode 100644 index 0000000..8743645 --- /dev/null +++ b/examples/tests/data/svd.xml @@ -0,0 +1,44 @@ + + + + Renesas + Renesas + V850 + E1/E2/CA2 + 1.2 + NEC/Renesas V850 automotive grade ICs + GPLv3 + + V850 + r1 + LE + false + false + false + + 8 + 32 + 32 + read-write + 0x00000000 + 0xFFFFFFFF + + Specific Registers + 1.0 + Specific Registers + MCU + 0xFFFFF1FC + 16 + read-write + + PRCMD + This command register (PRCMD) is to protect the registers that may have a significant influence on the application system (PSC, PSM) from an inadvertent write access, so that the system does not stop in case of a program hang-up. + 0xFFFFF1FC + 8 + read-write + 0x0000 + 0xFFFF + + + + \ No newline at end of file diff --git a/yaserde/src/lib.rs b/yaserde/src/lib.rs index a9c73e1..9aaafce 100644 --- a/yaserde/src/lib.rs +++ b/yaserde/src/lib.rs @@ -13,7 +13,7 @@ //! file for microcontrollers is called [SVD](https://github.com/rust-embedded/svd/) //! and it can be defined on YaSerDe via structs like so: //! -//!```ignore +//!```rust //! use yaserde_derive::YaSerialize; //! //! #[derive(Default, PartialEq, Debug, YaSerialize)] @@ -26,7 +26,13 @@ //! #[yaserde(attribute)] //! xsnonamespaceschemalocation: String, //! #[yaserde(child)] -//! devattributes: DevAttrs +//! attributes: DeviceAttributes +//! } +//! +//! #[derive(Default, PartialEq, Debug, YaSerialize)] +//! struct DeviceAttributes { +//! #[yaserde(child)] +//! vendor: String, //! } //!``` //! diff --git a/yaserde/tests/option.rs b/yaserde/tests/option.rs index 32bf492..fe59cb0 100644 --- a/yaserde/tests/option.rs +++ b/yaserde/tests/option.rs @@ -15,29 +15,29 @@ fn basic_option_types() { test_for_type!(Option::, None, None); test_for_type!(Option::, Some(true), Some("true")); test_for_type!(Option::, None, None); - test_for_type!(Option::, Some(12 as u8), Some("12")); + test_for_type!(Option::, Some(12_u8), Some("12")); test_for_type!(Option::, None, None); - test_for_type!(Option::, Some(12 as i8), Some("12")); - test_for_type!(Option::, Some(-12 as i8), Some("-12")); + test_for_type!(Option::, Some(12_i8), Some("12")); + test_for_type!(Option::, Some(-12_i8), Some("-12")); test_for_type!(Option::, None, None); - test_for_type!(Option::, Some(12 as u16), Some("12")); + test_for_type!(Option::, Some(12_u16), Some("12")); test_for_type!(Option::, None, None); - test_for_type!(Option::, Some(12 as i16), Some("12")); - test_for_type!(Option::, Some(-12 as i16), Some("-12")); + test_for_type!(Option::, Some(12_i16), Some("12")); + test_for_type!(Option::, Some(-12_i16), Some("-12")); test_for_type!(Option::, None, None); - test_for_type!(Option::, Some(12 as u32), Some("12")); + test_for_type!(Option::, Some(12_u32), Some("12")); test_for_type!(Option::, None, None); - test_for_type!(Option::, Some(12 as i32), Some("12")); - test_for_type!(Option::, Some(-12 as i32), Some("-12")); + test_for_type!(Option::, Some(12_i32), Some("12")); + test_for_type!(Option::, Some(-12_i32), Some("-12")); test_for_type!(Option::, None, None); - test_for_type!(Option::, Some(12 as u64), Some("12")); + test_for_type!(Option::, Some(12_u64), Some("12")); test_for_type!(Option::, None, None); - test_for_type!(Option::, Some(12 as i64), Some("12")); - test_for_type!(Option::, Some(-12 as i64), Some("-12")); + test_for_type!(Option::, Some(12_i64), Some("12")); + test_for_type!(Option::, Some(-12_i64), Some("-12")); test_for_type!(Option::, None, None); - test_for_type!(Option::, Some(-12.5 as f32), Some("-12.5")); + test_for_type!(Option::, Some(-12.5_f32), Some("-12.5")); test_for_type!(Option::, None, None); - test_for_type!(Option::, Some(-12.5 as f64), Some("-12.5")); + test_for_type!(Option::, Some(-12.5_f64), Some("-12.5")); test_for_type!(Option::, None, None); // TODO @@ -50,29 +50,29 @@ fn basic_option_types() { test_for_attribute_type!(Option::, None, None); test_for_attribute_type!(Option::, Some(true), Some("true")); test_for_attribute_type!(Option::, None, None); - test_for_attribute_type!(Option::, Some(12 as u8), Some("12")); + test_for_attribute_type!(Option::, Some(12_u8), Some("12")); test_for_attribute_type!(Option::, None, None); - test_for_attribute_type!(Option::, Some(12 as i8), Some("12")); - test_for_attribute_type!(Option::, Some(-12 as i8), Some("-12")); + test_for_attribute_type!(Option::, Some(12_i8), Some("12")); + test_for_attribute_type!(Option::, Some(-12_i8), Some("-12")); test_for_attribute_type!(Option::, None, None); - test_for_attribute_type!(Option::, Some(12 as u16), Some("12")); + test_for_attribute_type!(Option::, Some(12_u16), Some("12")); test_for_attribute_type!(Option::, None, None); - test_for_attribute_type!(Option::, Some(12 as i16), Some("12")); - test_for_attribute_type!(Option::, Some(-12 as i16), Some("-12")); + test_for_attribute_type!(Option::, Some(12_i16), Some("12")); + test_for_attribute_type!(Option::, Some(-12_i16), Some("-12")); test_for_attribute_type!(Option::, None, None); - test_for_attribute_type!(Option::, Some(12 as u32), Some("12")); + test_for_attribute_type!(Option::, Some(12_u32), Some("12")); test_for_attribute_type!(Option::, None, None); - test_for_attribute_type!(Option::, Some(12 as i32), Some("12")); - test_for_attribute_type!(Option::, Some(-12 as i32), Some("-12")); + test_for_attribute_type!(Option::, Some(12_i32), Some("12")); + test_for_attribute_type!(Option::, Some(-12_i32), Some("-12")); test_for_attribute_type!(Option::, None, None); - test_for_attribute_type!(Option::, Some(12 as u64), Some("12")); + test_for_attribute_type!(Option::, Some(12_u64), Some("12")); test_for_attribute_type!(Option::, None, None); - test_for_attribute_type!(Option::, Some(12 as i64), Some("12")); - test_for_attribute_type!(Option::, Some(-12 as i64), Some("-12")); + test_for_attribute_type!(Option::, Some(12_i64), Some("12")); + test_for_attribute_type!(Option::, Some(-12_i64), Some("-12")); test_for_attribute_type!(Option::, None, None); - test_for_attribute_type!(Option::, Some(-12.5 as f32), Some("-12.5")); + test_for_attribute_type!(Option::, Some(-12.5_f32), Some("-12.5")); test_for_attribute_type!(Option::, None, None); - test_for_attribute_type!(Option::, Some(-12.5 as f64), Some("-12.5")); + test_for_attribute_type!(Option::, Some(-12.5_f64), Some("-12.5")); test_for_attribute_type!(Option::, None, None); } diff --git a/yaserde/tests/skip_if.rs b/yaserde/tests/skip_if.rs index 641e737..de62a7d 100644 --- a/yaserde/tests/skip_if.rs +++ b/yaserde/tests/skip_if.rs @@ -34,7 +34,7 @@ fn skip_serializing_if_for_struct() { } impl XmlStruct { - fn check_string_function(&self, value: &String) -> bool { + fn check_string_function(&self, value: &str) -> bool { value == "something" } @@ -47,7 +47,7 @@ fn skip_serializing_if_for_struct() { } fn check_f32_function(&self, value: &f32) -> bool { - value == &0.0 + (value - 0.0).abs() < f32::EPSILON } } diff --git a/yaserde/tests/types.rs b/yaserde/tests/types.rs index 3b2ec70..db5c82d 100644 --- a/yaserde/tests/types.rs +++ b/yaserde/tests/types.rs @@ -13,37 +13,37 @@ fn ser_type() { test_for_type!(String, "test".to_string(), Some("test")); test_for_type!(bool, true, Some("true")); - test_for_type!(u8, 12 as u8, Some("12")); - test_for_type!(i8, 12 as i8, Some("12")); - test_for_type!(i8, -12 as i8, Some("-12")); - test_for_type!(u16, 12 as u16, Some("12")); - test_for_type!(i16, 12 as i16, Some("12")); - test_for_type!(i16, -12 as i16, Some("-12")); - test_for_type!(u32, 12 as u32, Some("12")); - test_for_type!(i32, 12 as i32, Some("12")); - test_for_type!(i32, -12 as i32, Some("-12")); - test_for_type!(u64, 12 as u64, Some("12")); - test_for_type!(i64, 12 as i64, Some("12")); - test_for_type!(i64, -12 as i64, Some("-12")); - test_for_type!(f32, -12.5 as f32, Some("-12.5")); - test_for_type!(f64, -12.5 as f64, Some("-12.5")); + test_for_type!(u8, 12_u8, Some("12")); + test_for_type!(i8, 12_i8, Some("12")); + test_for_type!(i8, -12_i8, Some("-12")); + test_for_type!(u16, 12_u16, Some("12")); + test_for_type!(i16, 12_i16, Some("12")); + test_for_type!(i16, -12_i16, Some("-12")); + test_for_type!(u32, 12_u32, Some("12")); + test_for_type!(i32, 12_i32, Some("12")); + test_for_type!(i32, -12_i32, Some("-12")); + test_for_type!(u64, 12_u64, Some("12")); + test_for_type!(i64, 12_i64, Some("12")); + test_for_type!(i64, -12_i64, Some("-12")); + test_for_type!(f32, -12.5_f32, Some("-12.5")); + test_for_type!(f64, -12.5_f64, Some("-12.5")); test_for_type!(Vec::, vec![], None); test_for_type!(Vec::, vec!["test".to_string()], Some("test")); test_for_attribute_type!(String, "test".to_string(), Some("test")); test_for_attribute_type!(bool, true, Some("true")); - test_for_attribute_type!(u8, 12 as u8, Some("12")); - test_for_attribute_type!(i8, 12 as i8, Some("12")); - test_for_attribute_type!(i8, -12 as i8, Some("-12")); - test_for_attribute_type!(u16, 12 as u16, Some("12")); - test_for_attribute_type!(i16, 12 as i16, Some("12")); - test_for_attribute_type!(i16, -12 as i16, Some("-12")); - test_for_attribute_type!(u32, 12 as u32, Some("12")); - test_for_attribute_type!(i32, 12 as i32, Some("12")); - test_for_attribute_type!(i32, -12 as i32, Some("-12")); - test_for_attribute_type!(u64, 12 as u64, Some("12")); - test_for_attribute_type!(i64, 12 as i64, Some("12")); - test_for_attribute_type!(i64, -12 as i64, Some("-12")); - test_for_attribute_type!(f32, -12.5 as f32, Some("-12.5")); - test_for_attribute_type!(f64, -12.5 as f64, Some("-12.5")); + test_for_attribute_type!(u8, 12_u8, Some("12")); + test_for_attribute_type!(i8, 12_i8, Some("12")); + test_for_attribute_type!(i8, -12_i8, Some("-12")); + test_for_attribute_type!(u16, 12_u16, Some("12")); + test_for_attribute_type!(i16, 12_i16, Some("12")); + test_for_attribute_type!(i16, -12_i16, Some("-12")); + test_for_attribute_type!(u32, 12_u32, Some("12")); + test_for_attribute_type!(i32, 12_i32, Some("12")); + test_for_attribute_type!(i32, -12_i32, Some("-12")); + test_for_attribute_type!(u64, 12_u64, Some("12")); + test_for_attribute_type!(i64, 12_i64, Some("12")); + test_for_attribute_type!(i64, -12_i64, Some("-12")); + test_for_attribute_type!(f32, -12.5_f32, Some("-12.5")); + test_for_attribute_type!(f64, -12.5_f64, Some("-12.5")); }