@@ -4,7 +4,7 @@ use once_cell::sync::Lazy;
4
4
use regex:: Regex ;
5
5
use std:: borrow:: Cow ;
6
6
use std:: fs:: File ;
7
- use std:: io:: { Read , Write } ;
7
+ use std:: io:: { Cursor , Read } ;
8
8
use std:: path:: { Path , PathBuf } ;
9
9
use svd_parser:: expand:: { BlockPath , FieldPath , RegisterPath } ;
10
10
use svd_parser:: svd:: {
@@ -21,6 +21,8 @@ use yaml_rust::{yaml::Hash, Yaml, YamlLoader};
21
21
22
22
use hashlink:: linked_hash_map;
23
23
24
+ pub use svd_encoder:: Config as EncoderConfig ;
25
+
24
26
use anyhow:: { anyhow, Context , Result } ;
25
27
pub type PatchResult = anyhow:: Result < ( ) > ;
26
28
@@ -70,7 +72,7 @@ impl Default for Config {
70
72
}
71
73
}
72
74
73
- fn load_patch ( yaml_file : & Path ) -> Result < Yaml > {
75
+ pub fn load_patch ( yaml_file : & Path ) -> Result < Yaml > {
74
76
// Load the specified YAML root file
75
77
let f = File :: open ( yaml_file) ?;
76
78
let mut contents = String :: new ( ) ;
@@ -91,14 +93,14 @@ pub fn process_file(
91
93
format_config : Option < & Path > ,
92
94
config : & Config ,
93
95
) -> Result < ( ) > {
94
- let mut doc = load_patch ( yaml_file) ?;
95
- let root = doc. hash_mut ( ) ?;
96
+ let doc = load_patch ( yaml_file) ?;
96
97
97
98
// Load the specified SVD file
98
99
let svdpath = abspath (
99
100
yaml_file,
100
101
Path :: new (
101
- root. get_str ( "_svd" ) ?
102
+ doc. hash ( ) ?
103
+ . get_str ( "_svd" ) ?
102
104
. ok_or_else ( || anyhow ! ( "You must have an svd key in the root YAML file" ) ) ?,
103
105
) ,
104
106
) ?;
@@ -109,36 +111,45 @@ pub fn process_file(
109
111
pth. set_extension ( "svd.patched" ) ;
110
112
pth
111
113
} ;
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 > {
113
129
let mut contents = String :: new ( ) ;
114
- ( & f ) . read_to_string ( & mut contents) ?;
130
+ svd . read_to_string ( & mut contents) ?;
115
131
let mut parser_config = svd_parser:: Config :: default ( ) ;
116
132
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) ?;
118
134
119
135
// 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 ;
122
138
let mut out_str = String :: new ( ) ;
123
139
let mut emitter = yaml_rust:: YamlEmitter :: new ( & mut out_str) ;
124
- emitter. dump ( & doc ) . unwrap ( ) ;
140
+ emitter. dump ( patch ) . unwrap ( ) ;
125
141
if config. show_patch_on_error {
126
142
format ! ( "Processing device `{name}`. Patches looks like:\n {out_str}" )
127
143
} else {
128
144
format ! ( "Processing device `{name}`" )
129
145
}
130
146
} ) ?;
131
147
132
- svd . validate_all ( config. post_validate ) ?;
148
+ dev . validate_all ( config. post_validate ) ?;
133
149
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
+ ) )
142
153
}
143
154
144
155
/// Gets the absolute path of relpath from the point of view of frompath.
0 commit comments