@@ -77,15 +77,21 @@ impl Tag {
7777/// Receives the AutoRest configuration file and parses it to its various configurations (by tags/API versions),
7878/// according to its extension.
7979/// e.g. for "path/to/config.md", it will get parsed as CommonMark [Literate Tag](http://azure.github.io/autorest/user/literate-file-formats/configuration.html).
80- pub fn parse_configurations_from_autorest_config_file ( config_file : & Utf8Path ) -> Result < Configuration > {
81- let extension = config_file
82- . extension ( )
83- . ok_or_else ( || Error :: with_message ( ErrorKind :: Parse , || format ! ( "expected md extension {config_file}" ) ) ) ?;
80+ pub fn parse_configurations_from_autorest_config_file (
81+ config_file : & Utf8Path ,
82+ ) -> Result < Configuration > {
83+ let extension = config_file. extension ( ) . ok_or_else ( || {
84+ Error :: with_message ( ErrorKind :: Parse , || {
85+ format ! ( "expected md extension {config_file}" )
86+ } )
87+ } ) ?;
8488 match extension. to_lowercase ( ) . as_str ( ) {
8589 "md" => {
8690 use literate_config:: * ;
87- let cmark_content =
88- std:: fs:: read_to_string ( config_file) . with_context ( ErrorKind :: Io , || format ! ( "reading the md file {config_file}" ) ) ?;
91+ let cmark_content = std:: fs:: read_to_string ( config_file)
92+ . with_context ( ErrorKind :: Io , || {
93+ format ! ( "reading the md file {config_file}" )
94+ } ) ?;
8995 Ok ( parse_configuration ( & cmark_content) ?)
9096 }
9197 _ => Err ( Error :: with_message ( ErrorKind :: Io , || {
@@ -118,12 +124,13 @@ mod literate_config {
118124 let root = parse_document ( & arena, cmark_content, & ComrakOptions :: default ( ) ) ;
119125
120126 // Get the AST node corresponding with "## Configuration".
121- let configuration_heading_node = get_configuration_section_heading_node ( root) . ok_or_else ( || {
122- Error :: message (
123- ErrorKind :: Parse ,
124- "no `## Configuration` heading in the AutoRest literate configuration file" ,
125- )
126- } ) ?;
127+ let configuration_heading_node =
128+ get_configuration_section_heading_node ( root) . ok_or_else ( || {
129+ Error :: message (
130+ ErrorKind :: Parse ,
131+ "no `## Configuration` heading in the AutoRest literate configuration file" ,
132+ )
133+ } ) ?;
127134
128135 let mut tags = Vec :: new ( ) ;
129136 let mut basic_info = BasicInformation :: default ( ) ;
@@ -133,15 +140,27 @@ mod literate_config {
133140 let mut current_node = configuration_heading_node. next_sibling ( ) ;
134141 while let Some ( node) = current_node {
135142 if is_basic_information ( node) {
136- let yaml = extract_yaml ( node) ?
137- . ok_or_else ( || Error :: message ( ErrorKind :: Parse , "expected configuration tag to contain a YAML code block" ) ) ?;
138- basic_info = serde_yaml:: from_str ( & yaml) . context ( ErrorKind :: DataConversion , "reading basic information block yaml" ) ?;
143+ let yaml = extract_yaml ( node) ?. ok_or_else ( || {
144+ Error :: message (
145+ ErrorKind :: Parse ,
146+ "expected configuration tag to contain a YAML code block" ,
147+ )
148+ } ) ?;
149+ basic_info = serde_yaml:: from_str ( & yaml) . context (
150+ ErrorKind :: DataConversion ,
151+ "reading basic information block yaml" ,
152+ ) ?;
139153 } else if let Some ( tag_name) = get_tag_name ( node) {
140154 // Extract the configuration from the first node inside the tag heading ("Tag: ..."),
141155 // by looking at the first YAML code block.
142- let yaml = extract_yaml ( node) ?
143- . ok_or_else ( || Error :: message ( ErrorKind :: Parse , "Expected configuration tag to contain a YAML code block." ) ) ?;
144- let mut tag: Tag = serde_yaml:: from_str ( & yaml) . context ( ErrorKind :: Parse , "reading configuration block yaml" ) ?;
156+ let yaml = extract_yaml ( node) ?. ok_or_else ( || {
157+ Error :: message (
158+ ErrorKind :: Parse ,
159+ "Expected configuration tag to contain a YAML code block." ,
160+ )
161+ } ) ?;
162+ let mut tag: Tag = serde_yaml:: from_str ( & yaml)
163+ . context ( ErrorKind :: Parse , "reading configuration block yaml" ) ?;
145164 for input_file in tag. input_files . iter_mut ( ) {
146165 * input_file = input_file. replace ( '\\' , "/" ) ;
147166 }
@@ -167,7 +186,9 @@ mod literate_config {
167186 // from https://github.com/kivikakk/comrak/blob/main/examples/headers.rs
168187 fn collect_text < ' a > ( node : & ' a AstNode < ' a > , output : & mut String ) {
169188 match node. data . borrow ( ) . value {
170- NodeValue :: Text ( ref literal) | NodeValue :: Code ( NodeCode { ref literal, .. } ) => output. push_str ( literal) ,
189+ NodeValue :: Text ( ref literal) | NodeValue :: Code ( NodeCode { ref literal, .. } ) => {
190+ output. push_str ( literal)
191+ }
171192 NodeValue :: LineBreak | NodeValue :: SoftBreak => output. push ( ' ' ) ,
172193 _ => {
173194 for n in node. children ( ) {
@@ -179,7 +200,9 @@ mod literate_config {
179200
180201 /// Returns the first "## Configuration" AST Node.
181202 /// There should only be one per Literate Configuration file.
182- fn get_configuration_section_heading_node < ' a > ( root : & ' a AstNode < ' a > ) -> Option < & ' a AstNode < ' a > > {
203+ fn get_configuration_section_heading_node < ' a > (
204+ root : & ' a AstNode < ' a > ,
205+ ) -> Option < & ' a AstNode < ' a > > {
183206 root. children ( ) . find ( |node| {
184207 if is_header_at_level ( node, 2 ) {
185208 let mut text = String :: new ( ) ;
@@ -220,19 +243,33 @@ mod literate_config {
220243 fn extract_yaml < ' a > ( configuration_tag_heading_node : & ' a AstNode < ' a > ) -> Result < Option < String > > {
221244 let mut current_node = configuration_tag_heading_node
222245 . next_sibling ( )
223- . ok_or_else ( || Error :: message ( ErrorKind :: Parse , "markdown ended unexpectedly after configuration tag heading" ) ) ?;
246+ . ok_or_else ( || {
247+ Error :: message (
248+ ErrorKind :: Parse ,
249+ "markdown ended unexpectedly after configuration tag heading" ,
250+ )
251+ } ) ?;
224252 loop {
225- if let NodeValue :: CodeBlock ( NodeCodeBlock { info, literal, fenced, .. } ) = & current_node. data . borrow ( ) . value {
253+ if let NodeValue :: CodeBlock ( NodeCodeBlock {
254+ info,
255+ literal,
256+ fenced,
257+ ..
258+ } ) = & current_node. data . borrow ( ) . value
259+ {
226260 if !fenced {
227261 continue ;
228262 }
229263 if info. trim_start ( ) . to_lowercase ( ) . starts_with ( "yaml" ) {
230264 return Ok ( Some ( literal. to_owned ( ) ) ) ;
231265 }
232266 }
233- current_node = current_node
234- . next_sibling ( )
235- . ok_or_else ( || Error :: message ( ErrorKind :: Parse , "markdown ended unexpectedly after configuration tag heading" ) ) ?;
267+ current_node = current_node. next_sibling ( ) . ok_or_else ( || {
268+ Error :: message (
269+ ErrorKind :: Parse ,
270+ "markdown ended unexpectedly after configuration tag heading" ,
271+ )
272+ } ) ?;
236273 }
237274 }
238275}
@@ -277,7 +314,9 @@ mod tests {
277314 fn test_get_input_file_api_version ( ) {
278315 assert_eq ! (
279316 Some ( "2019-05-05-preview" . to_owned( ) ) ,
280- get_input_file_api_version( "Microsoft.AlertsManagement/preview/2019-05-05-preview/ActionRules.json" )
317+ get_input_file_api_version(
318+ "Microsoft.AlertsManagement/preview/2019-05-05-preview/ActionRules.json"
319+ )
281320 ) ;
282321 }
283322
@@ -317,8 +356,14 @@ input-file:
317356 assert_eq ! ( 1 , tags. len( ) ) ;
318357 assert_eq ! ( "package-2019-06" , tags[ 0 ] . tag) ;
319358 assert_eq ! ( 5 , tags[ 0 ] . input_files. len( ) ) ;
320- assert_eq ! ( "Microsoft.Storage/stable/2019-06-01/storage.json" , tags[ 0 ] . input_files[ 0 ] ) ;
321- assert_eq ! ( "Microsoft.Storage/stable/2019-06-01/blob.json" , tags[ 0 ] . input_files[ 1 ] ) ;
359+ assert_eq ! (
360+ "Microsoft.Storage/stable/2019-06-01/storage.json" ,
361+ tags[ 0 ] . input_files[ 0 ]
362+ ) ;
363+ assert_eq ! (
364+ "Microsoft.Storage/stable/2019-06-01/blob.json" ,
365+ tags[ 0 ] . input_files[ 1 ]
366+ ) ;
322367 Ok ( ( ) )
323368 }
324369
@@ -354,12 +399,21 @@ input-file:
354399 assert_eq ! ( 2 , tags. len( ) ) ;
355400 assert_eq ! ( "package-2019-06" , tags[ 0 ] . tag) ;
356401 assert_eq ! ( 5 , tags[ 0 ] . input_files. len( ) ) ;
357- assert_eq ! ( "Microsoft.Storage/stable/2019-06-01/storage.json" , tags[ 0 ] . input_files[ 0 ] ) ;
358- assert_eq ! ( "Microsoft.Storage/stable/2019-06-01/blob.json" , tags[ 0 ] . input_files[ 1 ] ) ;
402+ assert_eq ! (
403+ "Microsoft.Storage/stable/2019-06-01/storage.json" ,
404+ tags[ 0 ] . input_files[ 0 ]
405+ ) ;
406+ assert_eq ! (
407+ "Microsoft.Storage/stable/2019-06-01/blob.json" ,
408+ tags[ 0 ] . input_files[ 1 ]
409+ ) ;
359410
360411 assert_eq ! ( "package-2015-05-preview" , tags[ 1 ] . tag) ;
361412 assert_eq ! ( 1 , tags[ 1 ] . input_files. len( ) ) ;
362- assert_eq ! ( "Microsoft.Storage/preview/2015-05-01-preview/storage.json" , tags[ 1 ] . input_files[ 0 ] ) ;
413+ assert_eq ! (
414+ "Microsoft.Storage/preview/2015-05-01-preview/storage.json" ,
415+ tags[ 1 ] . input_files[ 0 ]
416+ ) ;
363417 Ok ( ( ) )
364418 }
365419
0 commit comments