@@ -47,7 +47,8 @@ fn main() -> Result<()> {
4747 serde_json:: from_str ( & apif) . with_context ( || "parse transformed.json" . to_string ( ) ) ?;
4848
4949 let buf = std:: fs:: read ( descriptor_file) . with_context ( || "read protos.fds" . to_string ( ) ) ?;
50- let fds = FileDescriptorSet :: decode ( & * buf) . unwrap ( ) ; // pulls in proto::Message
50+ let fds = FileDescriptorSet :: decode ( & * buf)
51+ . with_context ( || "failed to decode FileDescriptorSet from protobuf descriptor file" ) ?; // pulls in proto::Message
5152
5253 // Map of module path to the names of child modules.
5354 let mut module_tree: HashMap < String , BTreeSet < String > > = HashMap :: new ( ) ;
@@ -61,10 +62,11 @@ fn main() -> Result<()> {
6162 if package_name. contains ( ".schema" ) {
6263 continue ;
6364 }
65+ let pkg_path = tmp_dir. join ( format ! ( "{}.rs" , package_name) ) ;
6466 let mut pkg_rs = OpenOptions :: new ( )
6567 . append ( true )
66- . open ( tmp_dir . join ( format ! ( "{}.rs" , package_name ) ) )
67- . unwrap ( ) ;
68+ . open ( & pkg_path )
69+ . with_context ( || format ! ( "failed to open or create package file: {}" , pkg_path . display ( ) ) ) ? ;
6870
6971 for msg in f. message_type {
7072 let message_name = match msg. name {
@@ -73,13 +75,12 @@ fn main() -> Result<()> {
7375 } ;
7476 let path = format ! ( "{}.{}" , package_name, message_name) ;
7577 if let Some ( resource) = resources. get ( & path) {
76- append_trait_impl ( & mut pkg_rs, message_name, resource) ;
78+ append_trait_impl ( & mut pkg_rs, message_name, resource) ? ;
7779 }
7880 }
7981
8082 let mut parts = package_name. split ( '.' ) . collect :: < Vec < _ > > ( ) ;
81- while !parts. is_empty ( ) {
82- let module = parts. pop ( ) . unwrap ( ) ;
83+ while let Some ( module) = parts. pop ( ) {
8384 let parent = parts. join ( "/" ) ;
8485 module_tree
8586 . entry ( parent)
@@ -96,31 +97,44 @@ fn main() -> Result<()> {
9697 } else {
9798 format ! ( "{}/mod.rs" , k)
9899 } ) ;
99- std:: fs:: create_dir_all ( dst. parent ( ) . unwrap ( ) ) ?;
100+ let parent_dir = dst. parent ( )
101+ . with_context ( || format ! ( "Failed to get parent directory for path: {}" , dst. display( ) ) ) ?;
102+ std:: fs:: create_dir_all ( parent_dir)
103+ . with_context ( || format ! ( "Failed to create directory structure: {}" , parent_dir. display( ) ) ) ?;
104+
100105 let lines = mods
101106 . into_iter ( )
102107 . map ( |m| format ! ( "pub mod {};" , m) )
103108 . collect :: < Vec < _ > > ( ) ;
104- std:: fs:: write ( dst, lines. join ( "\n " ) + "\n " ) ?;
109+ std:: fs:: write ( & dst, lines. join ( "\n " ) + "\n " )
110+ . with_context ( || format ! ( "Failed to write module file: {}" , dst. display( ) ) ) ?;
105111 }
106112
107113 for pkg in pkgs {
108114 let src = tmp_dir. join ( format ! ( "{}.rs" , & pkg) ) ;
109115 let dst = target_dir. join ( format ! ( "{}/mod.rs" , pkg. replace( '.' , "/" ) ) ) ;
110- std:: fs:: create_dir_all ( dst. parent ( ) . unwrap ( ) ) ?;
111- std:: fs:: rename ( src, dst) ?;
116+
117+ let parent_dir = dst. parent ( )
118+ . with_context ( || format ! ( "Failed to get parent directory for path: {}" , dst. display( ) ) ) ?;
119+ std:: fs:: create_dir_all ( parent_dir)
120+ . with_context ( || format ! ( "Failed to create directory structure: {}" , parent_dir. display( ) ) ) ?;
121+
122+ std:: fs:: rename ( & src, & dst)
123+ . with_context ( || format ! ( "Failed to rename generated package file from {} to {}" , src. display( ) , dst. display( ) ) ) ?;
112124 }
113125
126+ let lib_path = target_dir. join ( "lib.rs" ) ;
114127 let mut lib_rs = OpenOptions :: new ( )
115128 . append ( true )
116- . open ( target_dir. join ( "lib.rs" ) )
117- . unwrap ( ) ;
118- append_trait_def ( & mut lib_rs) ;
129+ . open ( & lib_path)
130+ . with_context ( || format ! ( "Failed to open generated lib.rs file at: {}" , lib_path. display( ) ) ) ?;
131+
132+ append_trait_def ( & mut lib_rs) ?;
119133
120134 Ok ( ( ) )
121135}
122136
123- fn append_trait_def ( lib_rs : & mut File ) {
137+ fn append_trait_def ( lib_rs : & mut File ) -> Result < ( ) > {
124138 let tokens = quote ! {
125139 /// The scope of a [`Resource`].
126140 pub trait ResourceScope { }
@@ -194,11 +208,13 @@ fn append_trait_def(lib_rs: &mut File) {
194208 fn conditions_mut( & mut self ) -> Option <& mut Vec <Self :: Condition >>;
195209 }
196210 } ;
197- writeln ! ( lib_rs) . unwrap ( ) ;
198- writeln ! ( lib_rs, "{}" , & tokens) . unwrap ( ) ;
211+ writeln ! ( lib_rs) . with_context ( || "Failed to write newline to lib.rs" ) ?;
212+ writeln ! ( lib_rs, "{}" , & tokens) . with_context ( || "Failed to write trait definitions to lib.rs" ) ?;
213+
214+ Ok ( ( ) )
199215}
200216
201- fn append_trait_impl ( pkg_rs : & mut File , message_name : & str , resource : & Resource ) {
217+ fn append_trait_impl ( pkg_rs : & mut File , message_name : & str , resource : & Resource ) -> Result < ( ) > {
202218 // Convert to match prost
203219 let type_name = format_ident ! ( "{}" , message_name) ;
204220
@@ -300,6 +316,8 @@ fn append_trait_impl(pkg_rs: &mut File, message_name: &str, resource: &Resource)
300316 tokens
301317 } ;
302318
303- writeln ! ( pkg_rs) . unwrap ( ) ;
304- writeln ! ( pkg_rs, "{}" , & tokens) . unwrap ( ) ;
319+ writeln ! ( pkg_rs) . with_context ( || "Failed to write newline to package file" ) ?;
320+ writeln ! ( pkg_rs, "{}" , & tokens) . with_context ( || "Failed to write trait implementations to package file" ) ?;
321+
322+ Ok ( ( ) )
305323}
0 commit comments