From 80df9aec1db449e6cc0f4513103aa442b8d43de3 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 5 Jul 2024 17:53:09 -0400 Subject: refactor: move `FileCollector` to deno_config (#24433) --- ext/fs/interface.rs | 71 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 13 deletions(-) (limited to 'ext/fs/interface.rs') diff --git a/ext/fs/interface.rs b/ext/fs/interface.rs index f639a700b..8f791f4c2 100644 --- a/ext/fs/interface.rs +++ b/ext/fs/interface.rs @@ -343,26 +343,71 @@ impl<'a> DenoConfigFsAdapter<'a> { } impl<'a> deno_config::fs::DenoConfigFs for DenoConfigFsAdapter<'a> { - fn read_to_string(&self, path: &Path) -> Result { - use deno_io::fs::FsError; - use std::io::ErrorKind; + fn read_to_string_lossy( + &self, + path: &Path, + ) -> Result { self .0 .read_text_file_lossy_sync(path, None) - .map_err(|err| match err { - FsError::Io(io) => io, - FsError::FileBusy => std::io::Error::new(ErrorKind::Other, "file busy"), - FsError::NotSupported => { - std::io::Error::new(ErrorKind::Other, "not supported") - } - FsError::PermissionDenied(name) => std::io::Error::new( - ErrorKind::PermissionDenied, - format!("requires {}", name), - ), + .map_err(map_deno_fs_to_config_err) + } + + fn stat_sync( + &self, + path: &Path, + ) -> Result { + self + .0 + .stat_sync(path) + .map(|stat| deno_config::fs::FsMetadata { + is_file: stat.is_file, + is_directory: stat.is_directory, + is_symlink: stat.is_symlink, + }) + .map_err(map_deno_fs_to_config_err) + } + + fn read_dir( + &self, + path: &Path, + ) -> Result, std::io::Error> { + self + .0 + .read_dir_sync(path) + .map_err(map_deno_fs_to_config_err) + .map(|entries| { + entries + .into_iter() + .map(|e| deno_config::fs::FsDirEntry { + path: path.join(e.name), + metadata: deno_config::fs::FsMetadata { + is_file: e.is_file, + is_directory: e.is_directory, + is_symlink: e.is_symlink, + }, + }) + .collect() }) } } +fn map_deno_fs_to_config_err(fs_err: deno_io::fs::FsError) -> std::io::Error { + use deno_io::fs::FsError; + use std::io::ErrorKind; + match fs_err { + FsError::Io(io) => io, + FsError::FileBusy => std::io::Error::new(ErrorKind::Other, "file busy"), + FsError::NotSupported => { + std::io::Error::new(ErrorKind::Other, "not supported") + } + FsError::PermissionDenied(name) => std::io::Error::new( + ErrorKind::PermissionDenied, + format!("requires {}", name), + ), + } +} + // Like String::from_utf8_lossy but operates on owned values #[inline(always)] fn string_from_utf8_lossy(buf: Vec) -> String { -- cgit v1.2.3