summaryrefslogtreecommitdiff
path: root/ext/node/lib.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-04-06 15:08:14 +0200
committerGitHub <noreply@github.com>2023-04-06 15:08:14 +0200
commit339165bd9565806374fa842dfc217dcc5ebabac5 (patch)
tree800fc65dacb90735bcb204133a13d450e46e9624 /ext/node/lib.rs
parent9626c48a018dc505b6c7b9c0c6632f8c45724ebe (diff)
refactor(ext/node): add more methods to 'NodeFs' trait (#18604)
Added more methods to `ext/node/clippy.toml` that are not allowed to be used in the crate. Prerequisite for https://github.com/denoland/deno/pull/18544
Diffstat (limited to 'ext/node/lib.rs')
-rw-r--r--ext/node/lib.rs24
1 files changed, 22 insertions, 2 deletions
diff --git a/ext/node/lib.rs b/ext/node/lib.rs
index 12d7b0b1e..04fd07cab 100644
--- a/ext/node/lib.rs
+++ b/ext/node/lib.rs
@@ -52,8 +52,11 @@ pub trait NodePermissions {
pub trait NodeFs {
fn current_dir() -> io::Result<PathBuf>;
- fn metadata<P: AsRef<Path>>(path: P) -> io::Result<std::fs::Metadata>;
+ fn is_file<P: AsRef<Path>>(path: P) -> bool;
+ fn is_dir<P: AsRef<Path>>(path: P) -> bool;
+ fn exists<P: AsRef<Path>>(path: P) -> bool;
fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String>;
+ fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf>;
}
pub struct RealFs;
@@ -63,15 +66,32 @@ impl NodeFs for RealFs {
std::env::current_dir()
}
- fn metadata<P: AsRef<Path>>(path: P) -> io::Result<std::fs::Metadata> {
+ fn exists<P: AsRef<Path>>(path: P) -> bool {
+ #[allow(clippy::disallowed_methods)]
+ std::fs::metadata(path).is_ok()
+ }
+
+ fn is_file<P: AsRef<Path>>(path: P) -> bool {
#[allow(clippy::disallowed_methods)]
std::fs::metadata(path)
+ .map(|m| m.is_file())
+ .unwrap_or(false)
+ }
+
+ fn is_dir<P: AsRef<Path>>(path: P) -> bool {
+ #[allow(clippy::disallowed_methods)]
+ std::fs::metadata(path).map(|m| m.is_dir()).unwrap_or(false)
}
fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
#[allow(clippy::disallowed_methods)]
std::fs::read_to_string(path)
}
+
+ fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
+ #[allow(clippy::disallowed_methods)]
+ std::path::Path::canonicalize(path.as_ref())
+ }
}
pub trait RequireNpmResolver {