feat(cli): move summary display predicates to Rust

Port the single-file and multiple-file summary decisions through the existing FIO context ABI, including the display-level lookup and the original multiple-file assertion invariant. Keep the C wrappers and call sites unchanged.

Test Plan: cargo test --manifest-path rust/cli/Cargo.toml --no-default-features --features cli,compression,decompression,benchmark (108 passed); CLI all-target clippy; make -B -C programs -j2 zstd zstd-small zstd-frugal; make -C tests -j2 test-cli-tests (41 passed).
This commit is contained in:
2026-07-18 04:43:53 +02:00
parent d9fe5d25ab
commit 49f03de3e2
2 changed files with 98 additions and 4 deletions
+5 -4
View File
@@ -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);
}
+93
View File
@@ -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() };