Skip to content

Commit d440730

Browse files
committed
svd2json/yaml fixes
1 parent edcb403 commit d440730

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

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: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,39 @@ use svd_parser as svd;
44
use std::env::args;
55
use std::fs::File;
66
use std::io::{Read, Write};
7+
use std::path::PathBuf;
78
use yaml::Value;
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: (svd2yaml) 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 YAML format.
31-
let v: Value = yaml::to_value(device).expect("Failed to parse Rust structs into YAML format");
33+
let v: Value =
34+
yaml::to_value(device).expect("Failed to serialize Rust structs into YAML format");
3235

3336
// Write the YAML-formatted device description to a file.
34-
let yaml_fn: String = svd_fn + ".yaml";
35-
File::create(yaml_fn)
37+
let mut yaml_fn = svd_fn.clone();
38+
yaml_fn.set_extension("yaml");
39+
File::create(&yaml_fn)
3640
.expect("Failed to open YAML output file")
3741
.write_all(yaml::to_string(&v).unwrap().as_bytes())
3842
.expect("Failed to write to YAML output file");

0 commit comments

Comments
 (0)