From e2a46e4281920867ba37c67fddbaaa2820f067d2 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 18 Aug 2026 07:29:18 +0900 Subject: refactor(zip): return a ZipEntryStat struct from stat_index The stat array was modelled as IndexMap, forcing callers through untyped lookups with an unwrap_or(0) fallback PHP has no counterpart for. Only `size` and `comp_size` are kept: statIndex has a single call site in Composer and it reads nothing else. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe-php-shim/src/zip.rs | 39 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) (limited to 'crates/shirabe-php-shim') diff --git a/crates/shirabe-php-shim/src/zip.rs b/crates/shirabe-php-shim/src/zip.rs index 4fab9aad..fe566ae4 100644 --- a/crates/shirabe-php-shim/src/zip.rs +++ b/crates/shirabe-php-shim/src/zip.rs @@ -1,7 +1,5 @@ use crate::ErrorException; -use crate::PhpMixed; use crate::{StreamBacking, StreamState}; -use indexmap::IndexMap; use zip::write::SimpleFileOptions; /// Test-only behaviour mirroring PHPUnit's `getMockBuilder('ZipArchive')->getMock()`, where @@ -32,6 +30,14 @@ enum ZipState { }, } +/// One entry of an archive, as returned by [`ZipArchive::stat_index`]. Only fields that Composer +/// accesses are ported. +#[derive(Debug, Clone)] +pub struct ZipEntryStat { + pub size: i64, + pub comp_size: i64, +} + #[derive(Debug)] pub struct ZipArchive { pub num_files: i64, @@ -113,35 +119,16 @@ impl ZipArchive { self.num_files } - pub fn stat_index(&self, index: i64) -> Option> { + pub fn stat_index(&self, index: i64) -> Option { let mut state = self.state.borrow_mut(); let ZipState::Reader(archive) = &mut *state else { return None; }; let file = archive.by_index(index as usize).ok()?; - let mut stat = IndexMap::new(); - stat.insert( - "name".to_string(), - PhpMixed::String(file.name().to_string()), - ); - stat.insert("index".to_string(), PhpMixed::Int(index)); - stat.insert("crc".to_string(), PhpMixed::Int(file.crc32() as i64)); - stat.insert("size".to_string(), PhpMixed::Int(file.size() as i64)); - // PHP exposes the last-modified time as a Unix timestamp. The `zip` crate - // only surfaces a 2-second-precision MS-DOS datetime; no consumer reads - // this field, so it is reported as 0 rather than reconstructing it. - stat.insert("mtime".to_string(), PhpMixed::Int(0)); - stat.insert( - "comp_size".to_string(), - PhpMixed::Int(file.compressed_size() as i64), - ); - let comp_method = match file.compression() { - zip::CompressionMethod::Stored => 0, - zip::CompressionMethod::Deflated => 8, - _ => -1, - }; - stat.insert("comp_method".to_string(), PhpMixed::Int(comp_method)); - Some(stat) + Some(ZipEntryStat { + size: file.size() as i64, + comp_size: file.compressed_size() as i64, + }) } pub fn extract_to(&self, path: impl AsRef) -> Result { -- cgit v1.3.1-4-g156e