1- use anyhow:: { Context , Result } ;
21use regex:: Regex ;
2+ use rust_project_goals:: spanned:: { Error , Spanned } ;
3+ use rust_project_goals:: { spanned:: Context as _, spanned:: Result } ;
34use std:: fs:: { self , File } ;
45use std:: io:: Write ;
56use std:: path:: { Path , PathBuf } ;
@@ -288,8 +289,9 @@ pub fn create_cfp(timeframe: &str, force: bool, dry_run: bool) -> Result<()> {
288289 println ! ( "Would create/overwrite directory: {}" , dir_path. display( ) ) ;
289290 }
290291 } else if !dry_run {
291- fs:: create_dir_all ( & dir_path)
292- . with_context ( || format ! ( "Failed to create directory {}" , dir_path. display( ) ) ) ?;
292+ fs:: create_dir_all ( & dir_path) . with_context ( || {
293+ Spanned :: here ( format ! ( "Failed to create directory {}" , dir_path. display( ) ) )
294+ } ) ?;
293295 println ! ( "Created directory: {}" , dir_path. display( ) ) ;
294296 } else {
295297 println ! ( "Would create directory: {}" , dir_path. display( ) ) ;
@@ -342,7 +344,7 @@ pub fn create_cfp(timeframe: &str, force: bool, dry_run: bool) -> Result<()> {
342344fn validate_timeframe ( timeframe : & str ) -> Result < ( ) > {
343345 let re = Regex :: new ( r"^\d{4}[hH][12]$" ) . unwrap ( ) ;
344346 if !re. is_match ( timeframe) {
345- anyhow :: bail! ( "Invalid timeframe format. Expected format: YYYYhN or YYYYHN (e.g., 2025h1, 2025H1, 2025h2, or 2025H2)" ) ;
347+ return Err ( Error :: str ( "Invalid timeframe format. Expected format: YYYYhN or YYYYHN (e.g., 2025h1, 2025H1, 2025h2, or 2025H2" ) ) ;
346348 }
347349 Ok ( ( ) )
348350}
@@ -356,8 +358,7 @@ fn copy_and_process_template(
356358 dry_run : bool ,
357359) -> Result < ( ) > {
358360 // Read the template file
359- let template_content = fs:: read_to_string ( template_path)
360- . with_context ( || format ! ( "Failed to read template file: {}" , template_path) ) ?;
361+ let template_content = Spanned :: read_str_from_file ( template_path) . transpose ( ) ?;
361362
362363 // Use the pure function to process the content
363364 let processed_content = text_processing:: process_template_content (
@@ -369,9 +370,9 @@ fn copy_and_process_template(
369370 // Write to destination file
370371 if !dry_run {
371372 File :: create ( dest_path)
372- . with_context ( || format ! ( "Failed to create file: {}" , dest_path . display ( ) ) ) ?
373+ . with_path_context ( dest_path , "Failed to create file" ) ?
373374 . write_all ( processed_content. as_bytes ( ) )
374- . with_context ( || format ! ( "Failed to write to file: {}" , dest_path . display ( ) ) ) ?;
375+ . with_path_context ( dest_path , "Failed to write to file" ) ?;
375376
376377 println ! ( "Created file: {}" , dest_path. display( ) ) ;
377378 } else {
@@ -383,8 +384,7 @@ fn copy_and_process_template(
383384/// Updates the SUMMARY.md file to include the new timeframe section
384385fn update_summary_md ( timeframe : & str , lowercase_timeframe : & str , dry_run : bool ) -> Result < ( ) > {
385386 let summary_path = "src/SUMMARY.md" ;
386- let content =
387- fs:: read_to_string ( summary_path) . with_context ( || format ! ( "Failed to read SUMMARY.md" ) ) ?;
387+ let content = fs:: read_to_string ( summary_path) . with_str_context ( "Failed to read SUMMARY.md" ) ?;
388388
389389 // Use the pure function to process the content
390390 let new_content =
@@ -408,8 +408,7 @@ fn update_summary_md(timeframe: &str, lowercase_timeframe: &str, dry_run: bool)
408408
409409 // Write the updated content back to SUMMARY.md
410410 if !dry_run {
411- fs:: write ( summary_path, new_content)
412- . with_context ( || format ! ( "Failed to write to SUMMARY.md" ) ) ?;
411+ fs:: write ( summary_path, new_content) . with_str_context ( "Failed to write to SUMMARY.md" ) ?;
413412
414413 println ! ( "Updated SUMMARY.md with {} section" , timeframe) ;
415414 } else {
@@ -422,8 +421,7 @@ fn update_summary_md(timeframe: &str, lowercase_timeframe: &str, dry_run: bool)
422421/// Updates the src/README.md with information about the new timeframe
423422fn update_main_readme ( timeframe : & str , lowercase_timeframe : & str , dry_run : bool ) -> Result < ( ) > {
424423 let readme_path = "src/README.md" ;
425- let content =
426- fs:: read_to_string ( readme_path) . with_context ( || format ! ( "Failed to read README.md" ) ) ?;
424+ let content = fs:: read_to_string ( readme_path) . with_str_context ( "Failed to read README.md" ) ?;
427425
428426 // Use the pure function to process the content
429427 let new_content =
@@ -495,8 +493,7 @@ fn update_main_readme(timeframe: &str, lowercase_timeframe: &str, dry_run: bool)
495493
496494 // Write the updated content back to README.md
497495 if !dry_run {
498- fs:: write ( readme_path, new_content)
499- . with_context ( || format ! ( "Failed to write to src/README.md" ) ) ?;
496+ fs:: write ( readme_path, new_content) . with_str_context ( "Failed to write to src/README.md" ) ?;
500497 }
501498
502499 Ok ( ( ) )
0 commit comments