1
1
#![ recursion_limit = "128" ]
2
2
3
3
use log:: { error, info} ;
4
- use std:: path:: PathBuf ;
5
- use svd_parser:: svd;
6
-
7
- mod generate;
8
- mod util;
4
+ use std:: path:: { Path , PathBuf } ;
9
5
10
6
use std:: fs:: File ;
11
7
use std:: io:: Write ;
@@ -14,7 +10,10 @@ use std::process;
14
10
use anyhow:: { Context , Result } ;
15
11
use clap:: { App , Arg } ;
16
12
17
- use crate :: util:: { build_rs, Config , Target } ;
13
+ use svd2rust:: {
14
+ generate, load_from,
15
+ util:: { build_rs, Config , SourceType , Target } ,
16
+ } ;
18
17
19
18
fn run ( ) -> Result < ( ) > {
20
19
use clap_conf:: prelude:: * ;
@@ -84,6 +83,11 @@ fn run() -> Result<()> {
84
83
. short ( "s" )
85
84
. help ( "Make advanced checks due to parsing SVD" ) ,
86
85
)
86
+ . arg (
87
+ Arg :: with_name ( "source_type" )
88
+ . long ( "source_type" )
89
+ . help ( "Specify file/stream format" ) ,
90
+ )
87
91
. arg (
88
92
Arg :: with_name ( "log_level" )
89
93
. long ( "log" )
@@ -101,19 +105,19 @@ fn run() -> Result<()> {
101
105
) )
102
106
. get_matches ( ) ;
103
107
104
- let xml = & mut String :: new ( ) ;
108
+ let input = & mut String :: new ( ) ;
105
109
match matches. value_of ( "input" ) {
106
110
Some ( file) => {
107
111
File :: open ( file)
108
112
. context ( "Cannot open the SVD file" ) ?
109
- . read_to_string ( xml )
113
+ . read_to_string ( input )
110
114
. context ( "Cannot read the SVD file" ) ?;
111
115
}
112
116
None => {
113
117
let stdin = std:: io:: stdin ( ) ;
114
118
stdin
115
119
. lock ( )
116
- . read_to_string ( xml )
120
+ . read_to_string ( input )
117
121
. context ( "Cannot read from stdin" ) ?;
118
122
}
119
123
}
@@ -146,6 +150,18 @@ fn run() -> Result<()> {
146
150
cfg. bool_flag ( "ignore_groups" , Filter :: Arg ) || cfg. bool_flag ( "ignore_groups" , Filter :: Conf ) ;
147
151
let strict = cfg. bool_flag ( "strict" , Filter :: Arg ) || cfg. bool_flag ( "strict" , Filter :: Conf ) ;
148
152
153
+ let mut source_type = cfg
154
+ . grab ( )
155
+ . arg ( "source_type" )
156
+ . conf ( "source_type" )
157
+ . done ( )
158
+ . and_then ( |s| SourceType :: from_extension ( & s) )
159
+ . unwrap_or_default ( ) ;
160
+
161
+ if let Some ( file) = matches. value_of ( "input" ) {
162
+ source_type = SourceType :: from_path ( Path :: new ( file) )
163
+ }
164
+
149
165
let config = Config {
150
166
target,
151
167
nightly,
@@ -155,18 +171,11 @@ fn run() -> Result<()> {
155
171
ignore_groups,
156
172
strict,
157
173
output_dir : path. clone ( ) ,
158
- } ;
159
-
160
- let mut parser_config = svd_parser:: Config :: default ( ) ;
161
- parser_config. validate_level = if strict {
162
- svd:: ValidateLevel :: Strict
163
- } else {
164
- svd:: ValidateLevel :: Weak
174
+ source_type,
165
175
} ;
166
176
167
177
info ! ( "Parsing device from SVD file" ) ;
168
- let device = svd_parser:: parse_with_config ( xml, & parser_config)
169
- . with_context ( || "Error parsing SVD file" . to_string ( ) ) ?;
178
+ let device = load_from ( input, & config) ?;
170
179
171
180
let mut device_x = String :: new ( ) ;
172
181
info ! ( "Rendering device" ) ;
0 commit comments