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

This commit is contained in:
Roman Valls Guimera
2021-02-08 21:50:27 +11:00
parent e17dbbb605
commit 037dd58453
11 changed files with 13 additions and 1 deletions

View File

@@ -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<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"
)]
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);
}

124
examples/src/boscop.rs Normal file
View File

@@ -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<Tabpage>,
}
#[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<Control>,
}
#[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<Midi>,
#[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);
}

10
examples/src/lib.rs Normal file
View File

@@ -0,0 +1,10 @@
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;

86
examples/src/ln_dom.rs Normal file
View File

@@ -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<DOMLayer>
}
#[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);
}

View File

@@ -0,0 +1,13 @@
[package]
name = "v850-svd"
version = "0.1.0"
authors = ["Roman Valls Guimera <brainstorm@nopcode.org>"]
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"

184
examples/src/svd/main.rs Normal file
View File

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