@@ -4,14 +4,16 @@ use svd_parser::svd::{Device, Peripheral, PeripheralInfo};
44use yaml_rust:: { yaml:: Hash , Yaml } ;
55
66use std:: collections:: HashSet ;
7- use std:: { fs:: File , io:: Read , path:: Path } ;
7+ use std:: str:: FromStr ;
8+ use std:: { fs, path:: Path } ;
89
910use super :: iterators:: { MatchIter , Matched } ;
1011use super :: peripheral:: { PeripheralExt , RegisterBlockExt } ;
1112use super :: yaml_ext:: { AsType , GetVal } ;
1213use super :: { abspath, adding_pos, matchname, Config , PatchResult , Spec , VAL_LVL } ;
1314use super :: { make_address_block, make_address_blocks, make_cpu, make_interrupt, make_peripheral} ;
1415use super :: { make_dim_element, modify_dim_element, modify_register_properties} ;
16+ use crate :: common:: input_format:: InputFormat ;
1517
1618pub type PerMatchIterMut < ' a , ' b > = MatchIter < ' b , std:: slice:: IterMut < ' a , Peripheral > > ;
1719
@@ -195,11 +197,24 @@ impl DeviceExt for Device {
195197 . collect :: < Vec < _ > > ( ) ;
196198 let mut new = match pcopysrc. as_slice ( ) {
197199 [ ppath, pcopyname] => {
198- let f = File :: open ( abspath ( path, Path :: new ( ppath) ) ?) ?;
199- let mut contents = String :: new ( ) ;
200- ( & f) . read_to_string ( & mut contents) . unwrap ( ) ;
201- let filedev = svd_parser:: parse ( & contents)
202- . with_context ( || format ! ( "Parsing file {contents}" ) ) ?;
200+ let ppath = Path :: new ( ppath) ;
201+ let input_format = ppath
202+ . extension ( )
203+ . map ( |ext_os| ext_os. to_str ( ) . expect ( "ppath is str" ) )
204+ . and_then ( |ext| InputFormat :: from_str ( ext) . ok ( ) )
205+ . unwrap_or ( InputFormat :: Xml ) ;
206+ let filepath = abspath ( path, ppath) ?;
207+ let contents = fs:: read_to_string ( filepath) ?;
208+ let filedev = match input_format {
209+ InputFormat :: Xml => svd_parser:: parse ( & contents)
210+ . with_context ( || format ! ( "Parsing svd file {contents}" ) ) ?,
211+ #[ cfg( feature = "yaml" ) ]
212+ InputFormat :: Yaml => serde_yaml:: from_str ( & contents)
213+ . with_context ( || format ! ( "Parsing yaml file {contents}" ) ) ?,
214+ #[ cfg( feature = "json" ) ]
215+ InputFormat :: Json => serde_json:: from_str ( & contents)
216+ . with_context ( || format ! ( "Parsing json file {contents}" ) ) ?,
217+ } ;
203218 filedev
204219 . get_peripheral ( pcopyname)
205220 . ok_or_else ( || {
0 commit comments