@@ -43,6 +43,24 @@ impl FromStr for OutputFormat {
4343 }
4444}
4545
46+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
47+ #[ non_exhaustive]
48+ pub enum ConfigFormat {
49+ Yaml ,
50+ Json ,
51+ }
52+
53+ impl FromStr for ConfigFormat {
54+ type Err = anyhow:: Error ;
55+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
56+ match s {
57+ "yml" | "yaml" | "YAML" => Ok ( Self :: Yaml ) ,
58+ "json" | "JSON" => Ok ( Self :: Json ) ,
59+ _ => Err ( anyhow ! ( "Unknown config file format" ) ) ,
60+ }
61+ }
62+ }
63+
4664pub fn convert (
4765 in_path : & Path ,
4866 out_path : & Path ,
@@ -51,6 +69,7 @@ pub fn convert(
5169 expand : bool ,
5270 expand_properties : bool ,
5371 ignore_enums : bool ,
72+ format_config : Option < & Path > ,
5473) -> Result < ( ) > {
5574 let input_format = match input_format {
5675 None => match in_path. extension ( ) . and_then ( |e| e. to_str ( ) ) {
@@ -87,8 +106,31 @@ pub fn convert(
87106 device
88107 } ;
89108
109+ let config = if let Some ( format_config) = format_config {
110+ let config_format = match format_config. extension ( ) . and_then ( |e| e. to_str ( ) ) {
111+ Some ( s) => ConfigFormat :: from_str ( s) ?,
112+ _ => return Err ( anyhow ! ( "Unknown output file format" ) ) ,
113+ } ;
114+ let mut config = String :: new ( ) ;
115+ File :: open ( format_config) ?. read_to_string ( & mut config) ?;
116+
117+ let config_map: std:: collections:: HashMap < String , String > = match config_format {
118+ ConfigFormat :: Yaml => serde_yaml:: from_str ( & config) ?,
119+ ConfigFormat :: Json => serde_json:: from_str ( & config) ?,
120+ } ;
121+
122+ let mut config = svd_encoder:: Config :: default ( ) ;
123+ config_map
124+ . iter ( )
125+ . for_each ( |( name, value) | config. update ( name, value) ) ;
126+
127+ config
128+ } else {
129+ svd_encoder:: Config :: default ( )
130+ } ;
131+
90132 let output = match output_format {
91- OutputFormat :: Xml => svd_encoder:: encode ( & device) ?,
133+ OutputFormat :: Xml => svd_encoder:: encode_with_config ( & device, & config ) ?,
92134 OutputFormat :: Yaml => serde_yaml:: to_string ( & device) ?,
93135 OutputFormat :: Json => serde_json:: to_string_pretty ( & device) ?,
94136 } ;
0 commit comments