11#![ recursion_limit = "128" ]
22
33use 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 } ;
95
106use std:: fs:: File ;
117use std:: io:: Write ;
@@ -14,7 +10,10 @@ use std::process;
1410use anyhow:: { Context , Result } ;
1511use clap:: { App , Arg } ;
1612
17- use crate :: util:: { build_rs, Config , Target } ;
13+ use svd2rust:: {
14+ generate, load_from,
15+ util:: { build_rs, Config , SourceType , Target } ,
16+ } ;
1817
1918fn run ( ) -> Result < ( ) > {
2019 use clap_conf:: prelude:: * ;
@@ -84,6 +83,11 @@ fn run() -> Result<()> {
8483 . short ( "s" )
8584 . help ( "Make advanced checks due to parsing SVD" ) ,
8685 )
86+ . arg (
87+ Arg :: with_name ( "source_type" )
88+ . long ( "source_type" )
89+ . help ( "Specify file/stream format" ) ,
90+ )
8791 . arg (
8892 Arg :: with_name ( "log_level" )
8993 . long ( "log" )
@@ -101,19 +105,19 @@ fn run() -> Result<()> {
101105 ) )
102106 . get_matches ( ) ;
103107
104- let xml = & mut String :: new ( ) ;
108+ let input = & mut String :: new ( ) ;
105109 match matches. value_of ( "input" ) {
106110 Some ( file) => {
107111 File :: open ( file)
108112 . context ( "Cannot open the SVD file" ) ?
109- . read_to_string ( xml )
113+ . read_to_string ( input )
110114 . context ( "Cannot read the SVD file" ) ?;
111115 }
112116 None => {
113117 let stdin = std:: io:: stdin ( ) ;
114118 stdin
115119 . lock ( )
116- . read_to_string ( xml )
120+ . read_to_string ( input )
117121 . context ( "Cannot read from stdin" ) ?;
118122 }
119123 }
@@ -146,6 +150,18 @@ fn run() -> Result<()> {
146150 cfg. bool_flag ( "ignore_groups" , Filter :: Arg ) || cfg. bool_flag ( "ignore_groups" , Filter :: Conf ) ;
147151 let strict = cfg. bool_flag ( "strict" , Filter :: Arg ) || cfg. bool_flag ( "strict" , Filter :: Conf ) ;
148152
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+
149165 let config = Config {
150166 target,
151167 nightly,
@@ -155,18 +171,11 @@ fn run() -> Result<()> {
155171 ignore_groups,
156172 strict,
157173 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,
165175 } ;
166176
167177 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) ?;
170179
171180 let mut device_x = String :: new ( ) ;
172181 info ! ( "Rendering device" ) ;
0 commit comments