diff --git a/programs/fileio.c b/programs/fileio.c index eb012a29d..fa6812a45 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -283,16 +283,17 @@ typedef char FIO_rust_ctx_size[ (sizeof(FIO_ctx_t) == offsetof(FIO_ctx_t, totalBytesOutput) + sizeof(size_t)) ? 1 : -1]; +int FIO_rust_shouldDisplayFileSummary(const FIO_ctx_t* fCtx); +int FIO_rust_shouldDisplayMultipleFileSummary(const FIO_ctx_t* fCtx); + static int FIO_shouldDisplayFileSummary(FIO_ctx_t const* fCtx) { - return fCtx->nbFilesTotal <= 1 || g_display_prefs.displayLevel >= 3; + return FIO_rust_shouldDisplayFileSummary(fCtx); } static int FIO_shouldDisplayMultipleFileSummary(FIO_ctx_t const* fCtx) { - int const shouldDisplay = (fCtx->nbFilesProcessed >= 1 && fCtx->nbFilesTotal > 1); - assert(shouldDisplay || FIO_shouldDisplayFileSummary(fCtx) || fCtx->nbFilesProcessed == 0); - return shouldDisplay; + return FIO_rust_shouldDisplayMultipleFileSummary(fCtx); } diff --git a/rust/src/fileio_prefs.rs b/rust/src/fileio_prefs.rs index 898b06466..0e5b1b4a4 100644 --- a/rust/src/fileio_prefs.rs +++ b/rust/src/fileio_prefs.rs @@ -497,6 +497,29 @@ pub unsafe extern "C" fn FIO_setHasStdinInput(ctx: *mut FIO_ctx_t, value: c_int) unsafe { ptr::addr_of_mut!((*ctx).hasStdinInput).write(c_int::from(value != 0)) }; } +#[inline] +fn should_display_file_summary(ctx: &FIO_ctx_t) -> bool { + ctx.nbFilesTotal <= 1 || unsafe { (*display_prefs()).displayLevel >= 3 } +} + +/// Return whether the single-file summary should be displayed. +#[no_mangle] +pub unsafe extern "C" fn FIO_rust_shouldDisplayFileSummary(ctx: *const FIO_ctx_t) -> c_int { + let ctx = unsafe { &*ctx }; + c_int::from(should_display_file_summary(ctx)) +} + +/// Return whether the multiple-file summary should be displayed. +/// +/// The assertion mirrors the original C helper's invariant exactly. +#[no_mangle] +pub unsafe extern "C" fn FIO_rust_shouldDisplayMultipleFileSummary(ctx: *const FIO_ctx_t) -> c_int { + let ctx = unsafe { &*ctx }; + let should_display = ctx.nbFilesProcessed >= 1 && ctx.nbFilesTotal > 1; + assert!(should_display || should_display_file_summary(ctx) || ctx.nbFilesProcessed == 0); + c_int::from(should_display) +} + #[no_mangle] pub unsafe extern "C" fn FIO_determineHasStdinInput( ctx: *mut FIO_ctx_t, @@ -1349,6 +1372,76 @@ mod tests { unsafe { FIO_freeContext(ctx) }; } + fn summary_context(nb_files_total: c_int, nb_files_processed: c_int) -> FIO_ctx_t { + FIO_ctx_t { + nbFilesTotal: nb_files_total, + hasStdinInput: 0, + hasStdoutOutput: 0, + currFileIdx: 0, + nbFilesProcessed: nb_files_processed, + totalBytesInput: 0, + totalBytesOutput: 0, + } + } + + #[test] + fn file_summary_display_matches_file_count_and_display_level() { + let mut ctx = summary_context(1, 0); + + for display_level in [0, 2] { + FIO_setNotificationLevel(display_level); + assert_eq!(unsafe { FIO_rust_shouldDisplayFileSummary(&ctx) }, 1); + } + + ctx.nbFilesTotal = 2; + for display_level in [0, 2] { + FIO_setNotificationLevel(display_level); + assert_eq!(unsafe { FIO_rust_shouldDisplayFileSummary(&ctx) }, 0); + } + + FIO_setNotificationLevel(3); + assert_eq!(unsafe { FIO_rust_shouldDisplayFileSummary(&ctx) }, 1); + + FIO_setNotificationLevel(2); + } + + #[test] + fn multiple_file_summary_display_matches_processed_count_and_file_count() { + let mut ctx = summary_context(2, 0); + + for display_level in [0, 2, 3] { + FIO_setNotificationLevel(display_level); + assert_eq!( + unsafe { FIO_rust_shouldDisplayMultipleFileSummary(&ctx) }, + 0 + ); + } + + ctx.nbFilesProcessed = 1; + for display_level in [0, 2, 3] { + FIO_setNotificationLevel(display_level); + assert_eq!( + unsafe { FIO_rust_shouldDisplayMultipleFileSummary(&ctx) }, + 1 + ); + } + + ctx.nbFilesProcessed = 2; + FIO_setNotificationLevel(0); + assert_eq!( + unsafe { FIO_rust_shouldDisplayMultipleFileSummary(&ctx) }, + 1 + ); + + ctx.nbFilesTotal = 1; + assert_eq!( + unsafe { FIO_rust_shouldDisplayMultipleFileSummary(&ctx) }, + 0 + ); + + FIO_setNotificationLevel(2); + } + #[test] fn preference_setters_update_the_c_layout() { let prefs = unsafe { FIO_createPreferences() };