From a6c47ee74023f6ef683988cabc8caa95406e3c99 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 5 May 2023 12:44:24 -0400 Subject: refactor(ext/node): combine `deno_node::Fs` with `deno_fs::FileSystem` (#18991) --- ext/fs/interface.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'ext/fs/interface.rs') diff --git a/ext/fs/interface.rs b/ext/fs/interface.rs index 1847b5982..474089153 100644 --- a/ext/fs/interface.rs +++ b/ext/fs/interface.rs @@ -73,7 +73,7 @@ pub struct FsDirEntry { } #[async_trait::async_trait(?Send)] -pub trait FileSystem: Send + Sync { +pub trait FileSystem: std::fmt::Debug + Send + Sync { fn cwd(&self) -> FsResult; fn tmp_dir(&self) -> FsResult; fn chdir(&self, path: &Path) -> FsResult<()>; @@ -225,4 +225,26 @@ pub trait FileSystem: Send + Sync { let buf = file.read_all_async().await?; Ok(buf) } + + fn is_file(&self, path: &Path) -> bool { + self.stat_sync(path).map(|m| m.is_file).unwrap_or(false) + } + + fn is_dir(&self, path: &Path) -> bool { + self + .stat_sync(path) + .map(|m| m.is_directory) + .unwrap_or(false) + } + + fn exists(&self, path: &Path) -> bool { + self.stat_sync(path).is_ok() + } + + fn read_to_string(&self, path: &Path) -> FsResult { + let buf = self.read_file_sync(path)?; + String::from_utf8(buf).map_err(|err| { + std::io::Error::new(std::io::ErrorKind::InvalidData, err).into() + }) + } } -- cgit v1.2.3