From 339165bd9565806374fa842dfc217dcc5ebabac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 6 Apr 2023 15:08:14 +0200 Subject: 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 --- ext/node/lib.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'ext/node/lib.rs') 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; - fn metadata>(path: P) -> io::Result; + fn is_file>(path: P) -> bool; + fn is_dir>(path: P) -> bool; + fn exists>(path: P) -> bool; fn read_to_string>(path: P) -> io::Result; + fn canonicalize>(path: P) -> io::Result; } pub struct RealFs; @@ -63,15 +66,32 @@ impl NodeFs for RealFs { std::env::current_dir() } - fn metadata>(path: P) -> io::Result { + fn exists>(path: P) -> bool { + #[allow(clippy::disallowed_methods)] + std::fs::metadata(path).is_ok() + } + + fn is_file>(path: P) -> bool { #[allow(clippy::disallowed_methods)] std::fs::metadata(path) + .map(|m| m.is_file()) + .unwrap_or(false) + } + + fn is_dir>(path: P) -> bool { + #[allow(clippy::disallowed_methods)] + std::fs::metadata(path).map(|m| m.is_dir()).unwrap_or(false) } fn read_to_string>(path: P) -> io::Result { #[allow(clippy::disallowed_methods)] std::fs::read_to_string(path) } + + fn canonicalize>(path: P) -> io::Result { + #[allow(clippy::disallowed_methods)] + std::path::Path::canonicalize(path.as_ref()) + } } pub trait RequireNpmResolver { -- cgit v1.2.3