From 92d2199afcecd0056e82d2559700769715401ef0 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Thu, 6 Aug 2026 03:46:41 +0900 Subject: refactor(php-shim): take filesystem paths as impl AsRef The shim's filesystem entry points took `&str` even though each one resolves to a local path through `std::fs` or a syscall, so callers holding a `PathBuf` had to stringify it at the call site. They now take `impl AsRef`, the form `file_exists`, `is_dir`, `unlink` and the rest of the already-converted set use. `Phar`, `PharData` and `ZipArchive` keep their archive path as a `PathBuf`. `PharData::compress` names the compressed sibling by appending the suffix to the file name rather than formatting the path into a `String`. Arguments PHP resolves through a stream wrapper (`fopen`, `file_put_contents`, `include`) still take `&str`, as do the byte-string operations (`dirname`, `basename`, `pathinfo`) and archive-internal entry names, which are `/`-joined logical names rather than OS paths. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe-php-shim/src/zip.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'crates/shirabe-php-shim/src/zip.rs') diff --git a/crates/shirabe-php-shim/src/zip.rs b/crates/shirabe-php-shim/src/zip.rs index 83dd70ce..56f02db7 100644 --- a/crates/shirabe-php-shim/src/zip.rs +++ b/crates/shirabe-php-shim/src/zip.rs @@ -27,7 +27,7 @@ enum ZipState { Writer { writer: zip::ZipWriter, /// The destination path, retained so `close` can confirm the file exists. - path: String, + path: std::path::PathBuf, status: String, }, } @@ -65,10 +65,11 @@ impl ZipArchive { } } - pub fn open(&mut self, filename: &str, flags: i64) -> Result<(), i64> { + pub fn open(&mut self, filename: impl AsRef, flags: i64) -> Result<(), i64> { if let Some(mock) = &self.mock { return mock.open; } + let filename = filename.as_ref(); if flags & Self::CREATE != 0 { let file = match std::fs::File::create(filename) { Ok(f) => f, @@ -76,7 +77,7 @@ impl ZipArchive { }; *self.state.borrow_mut() = ZipState::Writer { writer: zip::ZipWriter::new(file), - path: filename.to_string(), + path: filename.to_path_buf(), status: String::new(), }; self.num_files = 0; @@ -143,7 +144,7 @@ impl ZipArchive { Some(stat) } - pub fn extract_to(&self, path: &str) -> Result { + pub fn extract_to(&self, path: impl AsRef) -> Result { if let Some(mock) = &self.mock { return mock.extract_to.clone().map_err(|message| ErrorException { message, @@ -157,7 +158,7 @@ impl ZipArchive { let ZipState::Reader(archive) = &mut *state else { return Ok(false); }; - Ok(archive.extract(path).is_ok()) + Ok(archive.extract(path.as_ref()).is_ok()) } pub fn locate_name(&self, name: &str) -> Option { @@ -230,8 +231,8 @@ impl ZipArchive { .is_ok() } - pub fn add_file(&self, filepath: &str, local_name: &str) -> bool { - let contents = match std::fs::read(filepath) { + pub fn add_file(&self, filepath: impl AsRef, local_name: &str) -> bool { + let contents = match std::fs::read(filepath.as_ref()) { Ok(c) => c, Err(_) => return false, }; -- cgit v1.3.1