@@ -41,20 +41,40 @@ fn main() {
41
41
}
42
42
}
43
43
Some ( pattern) => {
44
- if let Some ( peripheral) = d. peripherals
45
- . iter ( )
46
- . find ( |x| x. name . to_ascii_lowercase ( ) == pattern)
47
- . or ( d. peripherals
48
- . iter ( )
49
- . find ( |x| x. name . to_ascii_lowercase ( ) . contains ( & pattern) ) ) {
50
- println ! ( "{}" ,
51
- svd2rust:: gen_peripheral( peripheral, & d. defaults)
52
- . iter( )
53
- . map( |i| i. to_string( ) )
54
- . collect:: <Vec <_>>( )
55
- . join( "\n \n " ) ) ;
56
-
44
+ if let Some ( peripheral) = find_peripheral ( & d, |n| n == pattern)
45
+ . or ( find_peripheral ( & d, |n| n. contains ( pattern) ) ) {
46
+ if let Some ( base_peripheral) = peripheral. derived_from . as_ref ( )
47
+ . and_then ( |bn| find_peripheral ( & d, |n| n == bn. to_ascii_lowercase ( ) ) ) {
48
+ let merged_peripheral = merge ( peripheral, base_peripheral) ;
49
+ println ! ( "{}" , gen_peripheral_desc( & merged_peripheral, & d. defaults) ) ;
50
+ } else {
51
+ println ! ( "{}" , gen_peripheral_desc( peripheral, & d. defaults) ) ;
52
+ }
57
53
}
58
54
}
59
55
}
60
56
}
57
+
58
+ fn find_peripheral < F : Fn ( & str ) -> bool > ( device : & svd:: Device , matcher : F ) -> Option < & svd:: Peripheral > {
59
+ device. peripherals . iter ( ) . find ( |x| matcher ( & x. name . to_ascii_lowercase ( ) ) )
60
+ }
61
+
62
+ fn gen_peripheral_desc ( p : & svd:: Peripheral , def : & svd:: Defaults ) -> String {
63
+ svd2rust:: gen_peripheral ( p, & def)
64
+ . iter ( )
65
+ . map ( |i| i. to_string ( ) )
66
+ . collect :: < Vec < _ > > ( )
67
+ . join ( "\n \n " )
68
+ }
69
+
70
+ fn merge ( p : & svd:: Peripheral , bp : & svd:: Peripheral ) -> svd:: Peripheral {
71
+ svd:: Peripheral {
72
+ name : p. name . clone ( ) ,
73
+ base_address : p. base_address ,
74
+ derived_from : None ,
75
+ group_name : p. group_name . clone ( ) . or ( bp. group_name . clone ( ) ) ,
76
+ description : p. description . clone ( ) . or ( bp. description . clone ( ) ) ,
77
+ interrupt : p. interrupt . clone ( ) . or ( bp. interrupt . clone ( ) ) ,
78
+ registers : p. registers . clone ( ) . or ( bp. registers . clone ( ) ) ,
79
+ }
80
+ }
0 commit comments