11// cargo run --example gen_mgmt --release
22// https://github.com/Azure/azure-rest-api-specs/blob/master/specification/compute/resource-manager
3- use autorust_codegen:: { self , cargo_toml, get_mgmt_readmes, lib_rs, path, Config , PropertyName , SpecReadme } ;
4- use std:: { collections:: HashSet , fs, path:: PathBuf } ;
3+ use autorust_codegen:: {
4+ self , cargo_toml, get_mgmt_readmes, io, lib_rs,
5+ readme_md:: { self , ReadmeMd } ,
6+ Config , Error , PropertyName , Result , SpecReadme ,
7+ } ;
8+ use camino:: Utf8PathBuf ;
9+ use std:: { collections:: HashSet , fs} ;
510
611const OUTPUT_FOLDER : & str = "../mgmt" ;
712
@@ -293,24 +298,6 @@ const BOX_PROPERTIES: &[(&str, &str, &str)] = &[
293298 ( "../../../azure-rest-api-specs/specification/keyvault/resource-manager/Microsoft.KeyVault/stable/2021-10-01/managedHsm.json" , "Error" , "innererror" ) ,
294299] ;
295300
296- pub type Result < T , E = Error > = std:: result:: Result < T , E > ;
297-
298- #[ derive( Debug , thiserror:: Error ) ]
299- pub enum Error {
300- #[ error( transparent) ]
301- CodegenError ( #[ from] autorust_codegen:: Error ) ,
302- #[ error( "file name was not utf-8" ) ]
303- FileNameNotUtf8Error { } ,
304- #[ error( "IoError" ) ]
305- IoError { source : std:: io:: Error } ,
306- #[ error( "PathError" ) ]
307- PathError { source : path:: Error } ,
308- #[ error( "CargoTomlError" ) ]
309- CargoTomlError { source : cargo_toml:: Error } ,
310- #[ error( "LibRsError" ) ]
311- LibRsError { source : lib_rs:: Error } ,
312- }
313-
314301fn main ( ) -> Result < ( ) > {
315302 for ( i, spec) in get_mgmt_readmes ( ) ?. iter ( ) . enumerate ( ) {
316303 if !ONLY_SERVICES . is_empty ( ) {
@@ -329,11 +316,11 @@ fn main() -> Result<()> {
329316fn gen_crate ( spec : & SpecReadme ) -> Result < ( ) > {
330317 let service_name = & spec. service_name ( ) ;
331318 let crate_name = & format ! ( "azure_mgmt_{}" , service_name) ;
332- let output_folder = & path :: join ( OUTPUT_FOLDER , service_name) . map_err ( |source| Error :: PathError { source } ) ?;
319+ let output_folder = & io :: join ( OUTPUT_FOLDER , service_name) ?;
333320
334- let src_folder = path :: join ( output_folder, "src" ) . map_err ( |source| Error :: PathError { source } ) ?;
321+ let src_folder = io :: join ( output_folder, "src" ) ?;
335322 if src_folder. exists ( ) {
336- fs:: remove_dir_all ( & src_folder) . map_err ( |source| Error :: IoError { source } ) ?;
323+ fs:: remove_dir_all ( & src_folder) ?;
337324 }
338325
339326 let mut tags = Vec :: new ( ) ;
@@ -342,7 +329,7 @@ fn gen_crate(spec: &SpecReadme) -> Result<()> {
342329 let mut box_properties = HashSet :: new ( ) ;
343330 for ( file_path, schema_name, property_name) in BOX_PROPERTIES {
344331 box_properties. insert ( PropertyName {
345- file_path : PathBuf :: from ( file_path) ,
332+ file_path : Utf8PathBuf :: from ( file_path) ,
346333 schema_name : schema_name. to_string ( ) ,
347334 property_name : property_name. to_string ( ) ,
348335 } ) ;
@@ -351,7 +338,7 @@ fn gen_crate(spec: &SpecReadme) -> Result<()> {
351338 let mut optional_properties = HashSet :: new ( ) ;
352339 for ( file_path, schema_name, property_name) in OPTIONAL_PROPERTIES {
353340 optional_properties. insert ( PropertyName {
354- file_path : PathBuf :: from ( file_path) ,
341+ file_path : Utf8PathBuf :: from ( file_path) ,
355342 schema_name : schema_name. to_string ( ) ,
356343 property_name : property_name. to_string ( ) ,
357344 } ) ;
@@ -365,12 +352,12 @@ fn gen_crate(spec: &SpecReadme) -> Result<()> {
365352 continue ;
366353 }
367354 println ! ( " {}" , tag_name) ;
368- let mod_output_folder = path :: join ( & src_folder, & tag. rust_mod_name ( ) ) . map_err ( |source| Error :: PathError { source } ) ?;
355+ let mod_output_folder = io :: join ( & src_folder, & tag. rust_mod_name ( ) ) ?;
369356 tags. push ( tag) ;
370357 let input_files: Result < Vec < _ > > = tag
371358 . input_files ( )
372359 . iter ( )
373- . map ( |input_file| path :: join ( spec. readme ( ) , input_file) . map_err ( |source| Error :: PathError { source } ) )
360+ . map ( |input_file| io :: join ( spec. readme ( ) , input_file) . map_err ( Error :: from ) )
374361 . collect ( ) ;
375362 let input_files = input_files?;
376363 autorust_codegen:: run ( Config {
@@ -385,19 +372,14 @@ fn gen_crate(spec: &SpecReadme) -> Result<()> {
385372 if tags. is_empty ( ) {
386373 return Ok ( ( ) ) ;
387374 }
388- cargo_toml:: create (
375+ cargo_toml:: create ( crate_name, & tags, config. tag ( ) , & io:: join ( output_folder, "Cargo.toml" ) ?) ?;
376+ lib_rs:: create ( & tags, & io:: join ( src_folder, "lib.rs" ) ?, false ) ?;
377+
378+ let readme = ReadmeMd {
389379 crate_name,
390- & tags,
391- config. tag ( ) ,
392- & path:: join ( output_folder, "Cargo.toml" ) . map_err ( |source| Error :: PathError { source } ) ?,
393- )
394- . map_err ( |source| Error :: CargoTomlError { source } ) ?;
395- lib_rs:: create (
396- & tags,
397- & path:: join ( src_folder, "lib.rs" ) . map_err ( |source| Error :: PathError { source } ) ?,
398- false ,
399- )
400- . map_err ( |source| Error :: LibRsError { source } ) ?;
380+ readme_url : readme_md:: url ( spec. readme ( ) . as_str ( ) ) ,
381+ } ;
382+ readme. create ( & io:: join ( output_folder, "README.md" ) ?) ?;
401383
402384 Ok ( ( ) )
403385}
0 commit comments