1
- use anyhow:: { Context , Result } ;
2
1
use regex:: Regex ;
2
+ use rust_project_goals:: spanned:: { Error , Spanned } ;
3
+ use rust_project_goals:: { spanned:: Context as _, spanned:: Result } ;
3
4
use std:: fs:: { self , File } ;
4
5
use std:: io:: Write ;
5
6
use std:: path:: { Path , PathBuf } ;
@@ -288,8 +289,9 @@ pub fn create_cfp(timeframe: &str, force: bool, dry_run: bool) -> Result<()> {
288
289
println ! ( "Would create/overwrite directory: {}" , dir_path. display( ) ) ;
289
290
}
290
291
} 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
+ } ) ?;
293
295
println ! ( "Created directory: {}" , dir_path. display( ) ) ;
294
296
} else {
295
297
println ! ( "Would create directory: {}" , dir_path. display( ) ) ;
@@ -342,7 +344,7 @@ pub fn create_cfp(timeframe: &str, force: bool, dry_run: bool) -> Result<()> {
342
344
fn validate_timeframe ( timeframe : & str ) -> Result < ( ) > {
343
345
let re = Regex :: new ( r"^\d{4}[hH][12]$" ) . unwrap ( ) ;
344
346
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" ) ) ;
346
348
}
347
349
Ok ( ( ) )
348
350
}
@@ -356,8 +358,7 @@ fn copy_and_process_template(
356
358
dry_run : bool ,
357
359
) -> Result < ( ) > {
358
360
// 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 ( ) ?;
361
362
362
363
// Use the pure function to process the content
363
364
let processed_content = text_processing:: process_template_content (
@@ -369,9 +370,9 @@ fn copy_and_process_template(
369
370
// Write to destination file
370
371
if !dry_run {
371
372
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" ) ?
373
374
. 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" ) ?;
375
376
376
377
println ! ( "Created file: {}" , dest_path. display( ) ) ;
377
378
} else {
@@ -383,8 +384,7 @@ fn copy_and_process_template(
383
384
/// Updates the SUMMARY.md file to include the new timeframe section
384
385
fn update_summary_md ( timeframe : & str , lowercase_timeframe : & str , dry_run : bool ) -> Result < ( ) > {
385
386
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" ) ?;
388
388
389
389
// Use the pure function to process the content
390
390
let new_content =
@@ -408,8 +408,7 @@ fn update_summary_md(timeframe: &str, lowercase_timeframe: &str, dry_run: bool)
408
408
409
409
// Write the updated content back to SUMMARY.md
410
410
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" ) ?;
413
412
414
413
println ! ( "Updated SUMMARY.md with {} section" , timeframe) ;
415
414
} else {
@@ -422,8 +421,7 @@ fn update_summary_md(timeframe: &str, lowercase_timeframe: &str, dry_run: bool)
422
421
/// Updates the src/README.md with information about the new timeframe
423
422
fn update_main_readme ( timeframe : & str , lowercase_timeframe : & str , dry_run : bool ) -> Result < ( ) > {
424
423
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" ) ?;
427
425
428
426
// Use the pure function to process the content
429
427
let new_content =
@@ -495,8 +493,7 @@ fn update_main_readme(timeframe: &str, lowercase_timeframe: &str, dry_run: bool)
495
493
496
494
// Write the updated content back to README.md
497
495
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" ) ?;
500
497
}
501
498
502
499
Ok ( ( ) )
0 commit comments