@@ -139,24 +139,25 @@ fn expand_cluster_array(
139139 path : & str ,
140140 index : & Index ,
141141) -> Result < ( ) > {
142- let cpath = if let Some ( dpath ) = c . derived_from . as_ref ( ) {
143- let cpath = dpath . to_string ( ) ;
144- if let Some ( d ) = index
142+ let cpath;
143+ if let Some ( dpath ) = c . derived_from . as_ref ( ) {
144+ let d = index
145145 . clusters
146146 . get ( dpath)
147147 . or_else ( || index. clusters . get ( & format ! ( "{}.{}" , path, dpath) ) )
148- {
149- if d. derived_from . is_some ( ) {
150- return Err ( anyhow ! ( "Multiple derive for {} is not supported" , dpath) ) ;
151- }
152- c = c. derive_from ( d) ;
153- c. derived_from = None ;
148+ . ok_or_else ( || anyhow ! ( "Cluster {} not found" , dpath) ) ?;
149+ cpath = if c. children . is_empty ( ) {
150+ dpath. to_string ( )
154151 } else {
155- return Err ( anyhow ! ( "Cluster {} not found" , dpath) ) ;
152+ format ! ( "{}.{}" , path, c. name)
153+ } ;
154+ if d. derived_from . is_some ( ) {
155+ return Err ( anyhow ! ( "Multiple derive for {} is not supported" , dpath) ) ;
156156 }
157- cpath
157+ c = c. derive_from ( d) ;
158+ c. derived_from = None ;
158159 } else {
159- format ! ( "{}.{}" , path, c. name)
160+ cpath = format ! ( "{}.{}" , path, c. name) ;
160161 } ;
161162
162163 for rc in take ( & mut c. children ) {
@@ -201,24 +202,25 @@ fn expand_register_array(
201202 path : & str ,
202203 index : & Index ,
203204) -> Result < ( ) > {
204- let rpath = if let Some ( dpath ) = r . derived_from . as_ref ( ) {
205- let rpath = dpath . to_string ( ) ;
206- if let Some ( d ) = index
205+ let rpath;
206+ if let Some ( dpath ) = r . derived_from . as_ref ( ) {
207+ let d = index
207208 . registers
208209 . get ( dpath)
209210 . or_else ( || index. registers . get ( & format ! ( "{}.{}" , path, dpath) ) )
210- {
211- if d. derived_from . is_some ( ) {
212- return Err ( anyhow ! ( "multiple derive for {} is not supported" , dpath) ) ;
213- }
214- r = r. derive_from ( d) ;
215- r. derived_from = None ;
211+ . ok_or_else ( || anyhow ! ( "register {} not found" , dpath) ) ?;
212+ rpath = if r. fields . is_none ( ) {
213+ dpath. to_string ( )
216214 } else {
217- return Err ( anyhow ! ( "register {} not found" , dpath) ) ;
215+ format ! ( "{}.{}" , path, r. name)
216+ } ;
217+ if d. derived_from . is_some ( ) {
218+ return Err ( anyhow ! ( "multiple derive for {} is not supported" , dpath) ) ;
218219 }
219- rpath
220+ r = r. derive_from ( d) ;
221+ r. derived_from = None ;
220222 } else {
221- format ! ( "{}.{}" , path, r. name)
223+ rpath = format ! ( "{}.{}" , path, r. name) ;
222224 } ;
223225
224226 if let Some ( field) = r. fields . as_mut ( ) {
@@ -255,24 +257,25 @@ fn expand_field(
255257 rpath : & str ,
256258 index : & Index ,
257259) -> Result < ( ) > {
258- let fpath = if let Some ( dpath ) = f . derived_from . as_ref ( ) {
259- let fpath = dpath . to_string ( ) ;
260- if let Some ( d ) = index
260+ let fpath;
261+ if let Some ( dpath ) = f . derived_from . as_ref ( ) {
262+ let d = index
261263 . fields
262264 . get ( dpath)
263265 . or_else ( || index. fields . get ( & format ! ( "{}.{}" , rpath, dpath) ) )
264- {
265- if d. derived_from . is_some ( ) {
266- return Err ( anyhow ! ( "multiple derive for {} is not supported" , dpath) ) ;
267- }
268- f = f. derive_from ( d) ;
269- f. derived_from = None ;
266+ . ok_or_else ( || anyhow ! ( "field {} not found" , dpath) ) ?;
267+ fpath = if f. enumerated_values . is_empty ( ) {
268+ dpath. to_string ( )
270269 } else {
271- return Err ( anyhow ! ( "field {} not found" , dpath) ) ;
270+ format ! ( "{}.{}" , rpath, f. name)
271+ } ;
272+ if d. derived_from . is_some ( ) {
273+ return Err ( anyhow ! ( "multiple derive for {} is not supported" , dpath) ) ;
272274 }
273- fpath
275+ f = f. derive_from ( d) ;
276+ f. derived_from = None ;
274277 } else {
275- format ! ( "{}.{}" , rpath, f. name)
278+ fpath = format ! ( "{}.{}" , rpath, f. name) ;
276279 } ;
277280
278281 for ev in & mut f. enumerated_values {
@@ -361,15 +364,21 @@ pub fn expand(indevice: &Device) -> Result<Device> {
361364
362365 let peripherals = take ( & mut device. peripherals ) ;
363366 for mut p in peripherals {
364- let mut path = p . name . to_string ( ) ;
367+ let path;
365368 if let Some ( dpath) = p. derived_from . as_ref ( ) {
366- path = dpath. into ( ) ;
367369 if let Some ( d) = index. get_base_peripheral ( dpath) {
370+ path = if p. registers . is_none ( ) {
371+ dpath. to_string ( )
372+ } else {
373+ p. name . to_string ( )
374+ } ;
368375 p = p. derive_from ( d) ;
369376 p. derived_from = None ;
370377 } else {
371378 return Err ( anyhow ! ( "peripheral {} not found" , dpath) ) ;
372379 }
380+ } else {
381+ path = p. name . to_string ( ) ;
373382 }
374383 if let Some ( regs) = p. registers . as_mut ( ) {
375384 for rc in take ( regs) {
0 commit comments