Skip to content

Commit edcb403

Browse files
committed
skip Options serialize in Cpu, svd2yaml example
1 parent 1afd5cb commit edcb403

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

svd-parser/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ anyhow = "1.0.45"
2323
thiserror = "1.0.30"
2424

2525
[dev-dependencies]
26-
serde_json = "1.0"
26+
serde_json = { version = "1.0", features = ["preserve_order"] }
27+
serde_yaml = "0.8.23"
2728
svd-rs = { version = "0.12.0", path = "../svd-rs", features = ["serde"] }
2829

2930
[[example]]

svd-parser/examples/svd2yaml.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use serde_yaml as yaml;
2+
use svd_parser as svd;
3+
4+
use std::env::args;
5+
use std::fs::File;
6+
use std::io::{Read, Write};
7+
use yaml::Value;
8+
9+
fn main() {
10+
// Collect command-line arguments.
11+
let argv: Vec<String> = args().collect();
12+
// Expect exactly one argument, with the name of the SVD file.
13+
// (Arg #0 is this program's name, Arg #1 is the actual argument)
14+
if argv.len() != 2 {
15+
println!("Usage: (svd2yaml) file.svd");
16+
return;
17+
}
18+
let svd_fn: String = argv[1].clone();
19+
20+
// Open the XML-formatted SVD file and read it into a String.
21+
let svd_xml = &mut String::new();
22+
File::open(&svd_fn)
23+
.expect("Failed to open SVD input file")
24+
.read_to_string(svd_xml)
25+
.expect("Failed to read SVD input file to a String");
26+
27+
// Use the 'svd_parser' crate to parse the file.
28+
let device = svd::parse(svd_xml).expect("Failed to parse the SVD file into Rust structs");
29+
30+
// Convert the parsed data into YAML format.
31+
let v: Value = yaml::to_value(device).expect("Failed to parse Rust structs into YAML format");
32+
33+
// Write the YAML-formatted device description to a file.
34+
let yaml_fn: String = svd_fn + ".yaml";
35+
File::create(yaml_fn)
36+
.expect("Failed to open YAML output file")
37+
.write_all(yaml::to_string(&v).unwrap().as_bytes())
38+
.expect("Failed to write to YAML output file");
39+
}

svd-rs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## Unreleased
99

10+
- skip serializing optional fields in `Cpu` if empty
1011
- skip serializing `values` in `EnumeratedValues` if empty
1112
- add missing fields in `Device`, require `version`, `description`, `address_unit_bits` and `width`,
1213
also `schema_version` is required, but skipped during (de)serialization

svd-rs/src/cpu.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,53 @@ pub struct Cpu {
2525

2626
/// Indicate whether the processor is equipped with a double precision floating point unit.
2727
/// This element is valid only when `fpu_present` is set to `true`
28-
#[cfg_attr(feature = "serde", serde(rename = "fpuDP"))]
28+
#[cfg_attr(
29+
feature = "serde",
30+
serde(default, skip_serializing_if = "Option::is_none", rename = "fpuDP")
31+
)]
2932
pub fpu_double_precision: Option<bool>,
3033

3134
/// Indicates whether the processor implements the optional SIMD DSP extensions (DSP)
35+
#[cfg_attr(
36+
feature = "serde",
37+
serde(default, skip_serializing_if = "Option::is_none")
38+
)]
3239
pub dsp_present: Option<bool>,
3340

3441
/// Indicate whether the processor has an instruction cache
42+
#[cfg_attr(
43+
feature = "serde",
44+
serde(default, skip_serializing_if = "Option::is_none")
45+
)]
3546
pub icache_present: Option<bool>,
3647

3748
/// Indicate whether the processor has a data cache
49+
#[cfg_attr(
50+
feature = "serde",
51+
serde(default, skip_serializing_if = "Option::is_none")
52+
)]
3853
pub dcache_present: Option<bool>,
3954

4055
/// Indicate whether the processor has an instruction tightly coupled memory
56+
#[cfg_attr(
57+
feature = "serde",
58+
serde(default, skip_serializing_if = "Option::is_none")
59+
)]
4160
pub itcm_present: Option<bool>,
4261

4362
/// Indicate whether the processor has a data tightly coupled memory
63+
#[cfg_attr(
64+
feature = "serde",
65+
serde(default, skip_serializing_if = "Option::is_none")
66+
)]
4467
pub dtcm_present: Option<bool>,
4568

4669
/// Indicate whether the Vector Table Offset Register (VTOR) is implemented.
4770
/// If not specified, then VTOR is assumed to be present
71+
#[cfg_attr(
72+
feature = "serde",
73+
serde(default, skip_serializing_if = "Option::is_none")
74+
)]
4875
pub vtor_present: Option<bool>,
4976

5077
/// Define the number of bits available in the Nested Vectored Interrupt Controller (NVIC) for configuring priority
@@ -56,9 +83,17 @@ pub struct Cpu {
5683
pub has_vendor_systick: bool,
5784

5885
/// Add 1 to the highest interrupt number and specify this number in here
86+
#[cfg_attr(
87+
feature = "serde",
88+
serde(default, skip_serializing_if = "Option::is_none")
89+
)]
5990
pub device_num_interrupts: Option<u32>,
6091

6192
/// Indicate the amount of regions in the Security Attribution Unit (SAU)
93+
#[cfg_attr(
94+
feature = "serde",
95+
serde(default, skip_serializing_if = "Option::is_none")
96+
)]
6297
pub sau_num_regions: Option<u32>,
6398
// sauRegionsConfig
6499
}

0 commit comments

Comments
 (0)