@@ -4,7 +4,7 @@ use once_cell::sync::Lazy;
44use regex:: Regex ;
55use std:: borrow:: Cow ;
66use std:: fs:: File ;
7- use std:: io:: { Read , Write } ;
7+ use std:: io:: { Cursor , Read } ;
88use std:: path:: { Path , PathBuf } ;
99use svd_parser:: expand:: { BlockPath , FieldPath , RegisterPath } ;
1010use svd_parser:: svd:: {
@@ -21,6 +21,8 @@ use yaml_rust::{yaml::Hash, Yaml, YamlLoader};
2121
2222use hashlink:: linked_hash_map;
2323
24+ pub use svd_encoder:: Config as EncoderConfig ;
25+
2426use anyhow:: { anyhow, Context , Result } ;
2527pub type PatchResult = anyhow:: Result < ( ) > ;
2628
@@ -70,7 +72,7 @@ impl Default for Config {
7072 }
7173}
7274
73- fn load_patch ( yaml_file : & Path ) -> Result < Yaml > {
75+ pub fn load_patch ( yaml_file : & Path ) -> Result < Yaml > {
7476 // Load the specified YAML root file
7577 let f = File :: open ( yaml_file) ?;
7678 let mut contents = String :: new ( ) ;
@@ -91,14 +93,14 @@ pub fn process_file(
9193 format_config : Option < & Path > ,
9294 config : & Config ,
9395) -> Result < ( ) > {
94- let mut doc = load_patch ( yaml_file) ?;
95- let root = doc. hash_mut ( ) ?;
96+ let doc = load_patch ( yaml_file) ?;
9697
9798 // Load the specified SVD file
9899 let svdpath = abspath (
99100 yaml_file,
100101 Path :: new (
101- root. get_str ( "_svd" ) ?
102+ doc. hash ( ) ?
103+ . get_str ( "_svd" ) ?
102104 . ok_or_else ( || anyhow ! ( "You must have an svd key in the root YAML file" ) ) ?,
103105 ) ,
104106 ) ?;
@@ -109,36 +111,45 @@ pub fn process_file(
109111 pth. set_extension ( "svd.patched" ) ;
110112 pth
111113 } ;
112- let f = File :: open ( svdpath) ?;
114+
115+ let encoder_config = get_encoder_config ( format_config) ?;
116+
117+ let mut svd_out = process_reader ( File :: open ( svdpath) ?, & doc, & encoder_config, config) ?;
118+ std:: io:: copy ( & mut svd_out, & mut File :: create ( svdpath_out) ?) ?;
119+
120+ Ok ( ( ) )
121+ }
122+
123+ pub fn process_reader < R : Read > (
124+ mut svd : R ,
125+ patch : & Yaml ,
126+ format_config : & EncoderConfig ,
127+ config : & Config ,
128+ ) -> Result < impl Read > {
113129 let mut contents = String :: new ( ) ;
114- ( & f ) . read_to_string ( & mut contents) ?;
130+ svd . read_to_string ( & mut contents) ?;
115131 let mut parser_config = svd_parser:: Config :: default ( ) ;
116132 parser_config. validate_level = ValidateLevel :: Disabled ;
117- let mut svd = svd_parser:: parse_with_config ( & contents, & parser_config) ?;
133+ let mut dev = svd_parser:: parse_with_config ( & contents, & parser_config) ?;
118134
119135 // Process device
120- svd . process ( root , config) . with_context ( || {
121- let name = & svd . name ;
136+ dev . process ( patch . hash ( ) ? , config) . with_context ( || {
137+ let name = & dev . name ;
122138 let mut out_str = String :: new ( ) ;
123139 let mut emitter = yaml_rust:: YamlEmitter :: new ( & mut out_str) ;
124- emitter. dump ( & doc ) . unwrap ( ) ;
140+ emitter. dump ( patch ) . unwrap ( ) ;
125141 if config. show_patch_on_error {
126142 format ! ( "Processing device `{name}`. Patches looks like:\n {out_str}" )
127143 } else {
128144 format ! ( "Processing device `{name}`" )
129145 }
130146 } ) ?;
131147
132- svd . validate_all ( config. post_validate ) ?;
148+ dev . validate_all ( config. post_validate ) ?;
133149
134- // SVD should now be updated, write it out
135- let config = get_encoder_config ( format_config) ?;
136- let svd_out = svd_encoder:: encode_with_config ( & svd, & config) ?;
137-
138- let mut f = File :: create ( svdpath_out) ?;
139- f. write_all ( svd_out. as_bytes ( ) ) ?;
140-
141- Ok ( ( ) )
150+ Ok ( Cursor :: new (
151+ svd_encoder:: encode_with_config ( & dev, format_config) ?. into_bytes ( ) ,
152+ ) )
142153}
143154
144155/// Gets the absolute path of relpath from the point of view of frompath.
0 commit comments