improve contribution to add examples, fix some clippy warns

This commit is contained in:
Marc-Antoine Arnaud
2021-02-08 14:10:08 +01:00
parent a65fa84f75
commit 6492efcfdc
12 changed files with 320 additions and 281 deletions

View File

@@ -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<Row>
rows: Vec<Row>,
}
#[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);

View File

@@ -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);

View File

@@ -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;

View File

@@ -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<DOMLayer>
dom_layer: Vec<DOMLayer>,
}
#[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);

View File

@@ -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<Field>
#[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<Field>,
}
#[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<Register>
#[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<Register>,
}
#[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<Peripheral>
#[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<Peripheral>,
}
#[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<Peripheral> = Vec::new();
let mut vec_registers: Vec<Register> = Vec::new();
let vec_fields: Vec<Field> = 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)
}