Skip to content

Commit de4ee9c

Browse files
bors[bot]burrbull
andauthored
Merge #188
188: skip Options serialize in Cpu, svd2yaml example r=Emilgardis a=burrbull Co-authored-by: Andrey Zgarbul <zgarbul.andrey@gmail.com>
2 parents 02d51d4 + d440730 commit de4ee9c

File tree

5 files changed

+95
-11
lines changed

5 files changed

+95
-11
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/svd2json.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,37 @@ use json::Value;
55
use std::env::args;
66
use std::fs::File;
77
use std::io::{Read, Write};
8+
use std::path::PathBuf;
89

910
fn main() {
1011
// Collect command-line arguments.
11-
let argv: Vec<String> = args().collect();
12+
let mut args = args();
1213
// Expect exactly one argument, with the name of the SVD file.
1314
// (Arg #0 is this program's name, Arg #1 is the actual argument)
14-
if argv.len() != 2 {
15+
let svd_fn = if let (Some(_), Some(arg1), None) = (args.next(), args.next(), args.next()) {
16+
PathBuf::from(arg1)
17+
} else {
1518
println!("Usage: (svd2json) file.svd");
1619
return;
17-
}
18-
let svd_fn: String = argv[1].clone();
20+
};
1921

2022
// Open the XML-formatted SVD file and read it into a String.
21-
let svd_xml = &mut String::new();
23+
let mut svd_xml = String::new();
2224
File::open(&svd_fn)
2325
.expect("Failed to open SVD input file")
24-
.read_to_string(svd_xml)
26+
.read_to_string(&mut svd_xml)
2527
.expect("Failed to read SVD input file to a String");
2628

2729
// 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");
30+
let device = svd::parse(&mut svd_xml).expect("Failed to parse the SVD file into Rust structs");
2931

3032
// Convert the parsed data into JSON format.
31-
let v: Value = json::to_value(device).expect("Failed to parse Rust structs into JSON format");
33+
let v: Value =
34+
json::to_value(device).expect("Failed to serialize Rust structs into JSON format");
3235

3336
// Write the JSON-formatted device description to a file.
34-
let json_fn: String = svd_fn + ".json";
37+
let mut json_fn = svd_fn.to_path_buf();
38+
json_fn.set_extension("json");
3539
File::create(json_fn)
3640
.expect("Failed to open JSON output file")
3741
.write_all(json::to_string_pretty(&v).unwrap().as_bytes())

svd-parser/examples/svd2yaml.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 std::path::PathBuf;
8+
use yaml::Value;
9+
10+
fn main() {
11+
// Collect command-line arguments.
12+
let mut args = args();
13+
// Expect exactly one argument, with the name of the SVD file.
14+
// (Arg #0 is this program's name, Arg #1 is the actual argument)
15+
let svd_fn = if let (Some(_), Some(arg1), None) = (args.next(), args.next(), args.next()) {
16+
PathBuf::from(arg1)
17+
} else {
18+
println!("Usage: (svd2yaml) file.svd");
19+
return;
20+
};
21+
22+
// Open the XML-formatted SVD file and read it into a String.
23+
let mut svd_xml = String::new();
24+
File::open(&svd_fn)
25+
.expect("Failed to open SVD input file")
26+
.read_to_string(&mut svd_xml)
27+
.expect("Failed to read SVD input file to a String");
28+
29+
// Use the 'svd_parser' crate to parse the file.
30+
let device = svd::parse(&mut svd_xml).expect("Failed to parse the SVD file into Rust structs");
31+
32+
// Convert the parsed data into YAML format.
33+
let v: Value =
34+
yaml::to_value(device).expect("Failed to serialize Rust structs into YAML format");
35+
36+
// Write the YAML-formatted device description to a file.
37+
let mut yaml_fn = svd_fn.clone();
38+
yaml_fn.set_extension("yaml");
39+
File::create(&yaml_fn)
40+
.expect("Failed to open YAML output file")
41+
.write_all(yaml::to_string(&v).unwrap().as_bytes())
42+
.expect("Failed to write to YAML output file");
43+
}

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 `names` function for arrays, `base_addresses` for `Peripheral`,
1213
`address_offsets` for `Register` and `Cluster`, `bit_offsets` for `Field` arrays

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)