Migrate yaserde examples from its own repo to its main one: https://github.com/media-io/yaserde_examples
This commit is contained in:
111
examples/bbigras_namespace.rs
Normal file
111
examples/bbigras_namespace.rs
Normal 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/boscop.rs
Normal file
124
examples/boscop.rs
Normal 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);
|
||||
}
|
||||
11
examples/lib.rs
Normal file
11
examples/lib.rs
Normal file
@@ -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;
|
||||
86
examples/ln_dom.rs
Normal file
86
examples/ln_dom.rs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user