feat: support generic
This commit is contained in:
committed by
Marc-Antoine Arnaud
parent
177fa8e5a7
commit
58d81c7a87
100
examples/src/generic.rs
Normal file
100
examples/src/generic.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
use yaserde::*;
|
||||
|
||||
#[derive(YaSerialize, YaDeserialize, Debug, Default, Clone, Eq, PartialEq)]
|
||||
pub struct Header {}
|
||||
|
||||
#[derive(YaSerialize, YaDeserialize, Debug, Default, Clone, Eq, PartialEq)]
|
||||
#[yaserde(
|
||||
rename = "Envelope",
|
||||
namespace = "s: http://schemas.xmlsoap.org/soap/envelope/",
|
||||
prefix = "s"
|
||||
)]
|
||||
pub struct SoapEnvelope<BODY>
|
||||
where
|
||||
BODY: YaSerialize + YaDeserialize + Default,
|
||||
{
|
||||
#[yaserde(rename = "encodingStyle", prefix = "s", attribute)]
|
||||
pub encoding_style: String,
|
||||
#[yaserde(rename = "u", prefix = "xmlns", attribute)]
|
||||
pub tnsattr: Option<String>,
|
||||
#[yaserde(rename = "urn", prefix = "xmlns", attribute)]
|
||||
pub urnattr: Option<String>,
|
||||
#[yaserde(rename = "xsi", prefix = "xmlns", attribute)]
|
||||
pub xsiattr: Option<String>,
|
||||
#[yaserde(rename = "Header", prefix = "s")]
|
||||
pub header: Option<Header>,
|
||||
#[yaserde(rename = "Body", prefix = "s")]
|
||||
pub body: BODY,
|
||||
}
|
||||
|
||||
#[derive(YaSerialize, YaDeserialize, Debug, Default, Clone, Eq, PartialEq)]
|
||||
pub struct SoapPlay {
|
||||
#[yaserde(rename = "Play", prefix = "u", default)]
|
||||
pub body: Play,
|
||||
}
|
||||
|
||||
#[derive(YaSerialize, YaDeserialize, Debug, Default, Clone, Eq, PartialEq)]
|
||||
#[yaserde(rename = "Play", prefix = "u")]
|
||||
pub struct Play {
|
||||
#[yaserde(flatten, default)]
|
||||
pub parameters: Play2,
|
||||
}
|
||||
|
||||
#[derive(YaSerialize, YaDeserialize, Debug, Default, Clone, Eq, PartialEq)]
|
||||
#[yaserde(
|
||||
rename = "Play",
|
||||
namespace = "u: urn:schemas-upnp-org:service:AVTransport:1",
|
||||
prefix = "u"
|
||||
)]
|
||||
pub struct Play2 {
|
||||
#[yaserde(rename = "InstanceID", default)]
|
||||
pub instance_id: i32,
|
||||
#[yaserde(rename = "Speed", default)]
|
||||
pub speed: i32,
|
||||
}
|
||||
|
||||
#[derive(PrimitiveYaSerde, Debug, Default, Eq, PartialEq)]
|
||||
struct Meters(i32);
|
||||
|
||||
#[test]
|
||||
fn test_for_generic_newtype() {
|
||||
let a = SoapEnvelope {
|
||||
encoding_style: "".to_string(),
|
||||
tnsattr: None,
|
||||
urnattr: None,
|
||||
xsiattr: None,
|
||||
header: None,
|
||||
body: Meters(10),
|
||||
};
|
||||
|
||||
let s = ser::to_string(&a).unwrap();
|
||||
let b: SoapEnvelope<Meters> = de::from_str(&s).unwrap();
|
||||
|
||||
assert_eq!(a, b);
|
||||
println!("{:#?}", b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_for_generic_nested_struct() {
|
||||
let a = SoapEnvelope {
|
||||
encoding_style: "".to_string(),
|
||||
tnsattr: None,
|
||||
urnattr: None,
|
||||
xsiattr: None,
|
||||
header: None,
|
||||
body: SoapPlay {
|
||||
body: Play {
|
||||
parameters: Play2 {
|
||||
instance_id: 20,
|
||||
speed: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
let s = ser::to_string(&a).unwrap();
|
||||
let b: SoapEnvelope<SoapPlay> = de::from_str(&s).unwrap();
|
||||
|
||||
assert_eq!(a, b);
|
||||
println!("{:#?}", b);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
mod bbigras_namespace;
|
||||
mod boscop;
|
||||
mod generic;
|
||||
mod ln_dom;
|
||||
mod svd;
|
||||
|
||||
@@ -1,124 +1,123 @@
|
||||
use yaserde::YaSerialize;
|
||||
|
||||
#[derive(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(PartialEq, Debug, YaSerialize)]
|
||||
struct Field {
|
||||
name: String,
|
||||
#[yaserde(child)]
|
||||
description: String,
|
||||
#[yaserde(child)]
|
||||
bitrange: String,
|
||||
#[yaserde(child)]
|
||||
access: String,
|
||||
}
|
||||
|
||||
#[derive(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(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(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(PartialEq, Debug, YaSerialize)]
|
||||
#[yaserde(rename = "device")]
|
||||
struct Device {
|
||||
#[yaserde(attribute)]
|
||||
schemaversion: String,
|
||||
#[yaserde(attribute)]
|
||||
xmlns: String,
|
||||
#[yaserde(attribute)]
|
||||
xsnonamespaceschemalocation: String,
|
||||
#[yaserde(child)]
|
||||
devattributes: DevAttrs,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parsing_svd() {
|
||||
use std::fs;
|
||||
use yaserde::YaSerialize;
|
||||
|
||||
#[derive(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(PartialEq, Debug, YaSerialize)]
|
||||
struct Field {
|
||||
name: String,
|
||||
#[yaserde(child)]
|
||||
description: String,
|
||||
#[yaserde(child)]
|
||||
bitrange: String,
|
||||
#[yaserde(child)]
|
||||
access: String,
|
||||
}
|
||||
|
||||
#[derive(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(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(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(PartialEq, Debug, YaSerialize)]
|
||||
#[yaserde(rename = "device")]
|
||||
struct Device {
|
||||
#[yaserde(attribute)]
|
||||
schemaversion: String,
|
||||
#[yaserde(attribute)]
|
||||
xmlns: String,
|
||||
#[yaserde(attribute)]
|
||||
xsnonamespaceschemalocation: String,
|
||||
#[yaserde(child)]
|
||||
devattributes: DevAttrs,
|
||||
}
|
||||
|
||||
let register = Register {
|
||||
name: "PRCMD".to_string(),
|
||||
|
||||
Reference in New Issue
Block a user