@@ -4,35 +4,39 @@ use svd_parser as svd;
4
4
use std:: env:: args;
5
5
use std:: fs:: File ;
6
6
use std:: io:: { Read , Write } ;
7
+ use std:: path:: PathBuf ;
7
8
use yaml:: Value ;
8
9
9
10
fn main ( ) {
10
11
// Collect command-line arguments.
11
- let argv : Vec < String > = args ( ) . collect ( ) ;
12
+ let mut args = args ( ) ;
12
13
// Expect exactly one argument, with the name of the SVD file.
13
14
// (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 {
15
18
println ! ( "Usage: (svd2yaml) file.svd" ) ;
16
19
return ;
17
- }
18
- let svd_fn: String = argv[ 1 ] . clone ( ) ;
20
+ } ;
19
21
20
22
// 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 ( ) ;
22
24
File :: open ( & svd_fn)
23
25
. expect ( "Failed to open SVD input file" )
24
- . read_to_string ( svd_xml)
26
+ . read_to_string ( & mut svd_xml)
25
27
. expect ( "Failed to read SVD input file to a String" ) ;
26
28
27
29
// 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" ) ;
29
31
30
32
// 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" ) ;
32
35
33
36
// 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)
36
40
. expect ( "Failed to open YAML output file" )
37
41
. write_all ( yaml:: to_string ( & v) . unwrap ( ) . as_bytes ( ) )
38
42
. expect ( "Failed to write to YAML output file" ) ;
0 commit comments