66
77use std:: { path:: PathBuf , sync:: Arc } ;
88
9- use anyhow:: anyhow;
9+ use anyhow:: { Context , anyhow} ;
1010use cargo_metadata:: Metadata ;
1111use colored:: Colorize ;
1212use cursive:: Cursive ;
@@ -19,7 +19,7 @@ use jkconfig::{
1919use object:: { Architecture , Object } ;
2020use tokio:: fs;
2121
22- use crate :: build:: config:: BuildConfig ;
22+ use crate :: { build:: config:: BuildConfig , utils :: PathResultExt } ;
2323
2424/// Configuration for output directories.
2525///
@@ -148,30 +148,28 @@ impl AppContext {
148148 let res = cargo_metadata:: MetadataCommand :: new ( )
149149 . current_dir ( & self . paths . manifest )
150150 . no_deps ( )
151- . exec ( ) ?;
151+ . exec ( )
152+ . with_context ( || {
153+ format ! (
154+ "failed to load cargo metadata from {}" ,
155+ self . paths. manifest. display( )
156+ )
157+ } ) ?;
152158 Ok ( res)
153159 }
154160
155161 /// Sets the ELF file path and detects its architecture.
156162 ///
157163 /// This also reads the ELF file to detect the target CPU architecture.
158- pub async fn set_elf_path ( & mut self , path : PathBuf ) {
164+ pub async fn set_elf_path ( & mut self , path : PathBuf ) -> anyhow :: Result < ( ) > {
159165 self . paths . artifacts . elf = Some ( path. clone ( ) ) ;
160- let binary_data = match fs:: read ( path) . await {
161- Ok ( data) => data,
162- Err ( e) => {
163- println ! ( "Failed to read ELF file: {e}" ) ;
164- return ;
165- }
166- } ;
167- let file = match object:: File :: parse ( binary_data. as_slice ( ) ) {
168- Ok ( f) => f,
169- Err ( e) => {
170- println ! ( "Failed to parse ELF file: {e}" ) ;
171- return ;
172- }
173- } ;
174- self . arch = Some ( file. architecture ( ) )
166+ let binary_data = fs:: read ( & path)
167+ . await
168+ . with_path ( "failed to read ELF file" , & path) ?;
169+ let file = object:: File :: parse ( binary_data. as_slice ( ) )
170+ . with_context ( || format ! ( "failed to parse ELF file: {}" , path. display( ) ) ) ?;
171+ self . arch = Some ( file. architecture ( ) ) ;
172+ Ok ( ( ) )
175173 }
176174
177175 /// Strips debug symbols from the ELF file.
@@ -191,13 +189,15 @@ impl AppContext {
191189 . artifacts
192190 . elf
193191 . as_ref ( )
194- . ok_or ( anyhow ! ( "elf not exist" ) ) ?
195- . canonicalize ( ) ?;
192+ . ok_or ( anyhow ! ( "elf not exist" ) ) ?;
193+ let elf_path = elf_path
194+ . canonicalize ( )
195+ . with_path ( "failed to canonicalize file" , elf_path) ?;
196196
197197 let stripped_elf_path = elf_path. with_file_name (
198198 elf_path
199199 . file_stem ( )
200- . ok_or ( anyhow ! ( "Invalid file path" ) ) ?
200+ . ok_or_else ( || anyhow ! ( "invalid ELF file path: {}" , elf_path . display ( ) ) ) ?
201201 . to_string_lossy ( )
202202 . to_string ( )
203203 + ".elf" ,
@@ -251,12 +251,14 @@ impl AppContext {
251251 . artifacts
252252 . elf
253253 . as_ref ( )
254- . ok_or ( anyhow ! ( "elf not exist" ) ) ?
255- . canonicalize ( ) ?;
254+ . ok_or ( anyhow ! ( "elf not exist" ) ) ?;
255+ let elf_path = elf_path
256+ . canonicalize ( )
257+ . with_path ( "failed to canonicalize file" , elf_path) ?;
256258
257259 let bin_name = elf_path
258260 . file_stem ( )
259- . ok_or ( anyhow ! ( "Invalid file path" ) ) ?
261+ . ok_or_else ( || anyhow ! ( "invalid ELF file path: {}" , elf_path . display ( ) ) ) ?
260262 . to_string_lossy ( )
261263 . to_string ( )
262264 + ".bin" ;
@@ -268,7 +270,7 @@ impl AppContext {
268270 } ;
269271
270272 if let Some ( parent) = bin_path. parent ( ) {
271- std:: fs:: create_dir_all ( parent) ?;
273+ std:: fs:: create_dir_all ( parent) . with_path ( "failed to create directory" , parent ) ?;
272274 }
273275
274276 println ! (
@@ -326,11 +328,12 @@ impl AppContext {
326328 self . build_config_path = Some ( config_path. clone ( ) ) ;
327329
328330 let Some ( c) : Option < BuildConfig > = jkconfig:: run (
329- config_path,
331+ config_path. clone ( ) ,
330332 menu,
331333 & [ self . ui_hock_feature_select ( ) , self . ui_hock_pacage_select ( ) ] ,
332334 )
333- . await ?
335+ . await
336+ . with_context ( || format ! ( "failed to load build config: {}" , config_path. display( ) ) ) ?
334337 else {
335338 anyhow:: bail!( "No build configuration obtained" ) ;
336339 } ;
0 commit comments