From 51deadd983a7058348554672286736d67d999a67 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 12 Dec 2022 19:36:45 +0300 Subject: refactor(tvix/eval): use `EvalIO::path_exists` for the builtin Change-Id: I49822ce30137777865e7370ee86666636e277b35 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7573 Tested-by: BuildkiteCI Reviewed-by: grfn --- tvix/eval/src/builtins/impure.rs | 3 ++- tvix/eval/src/io.rs | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/builtins/impure.rs b/tvix/eval/src/builtins/impure.rs index c2ba2f2a07f7..d91f703a6ab7 100644 --- a/tvix/eval/src/builtins/impure.rs +++ b/tvix/eval/src/builtins/impure.rs @@ -28,7 +28,8 @@ mod impure_builtins { #[builtin("pathExists")] fn builtin_path_exists(vm: &mut VM, s: Value) -> Result { - Ok(coerce_value_to_path(&s, vm)?.exists().into()) + let path = coerce_value_to_path(&s, vm)?; + vm.io().path_exists(path).map(Value::Bool) } #[builtin("readDir")] diff --git a/tvix/eval/src/io.rs b/tvix/eval/src/io.rs index fd0680f3c2a5..627eb848dcba 100644 --- a/tvix/eval/src/io.rs +++ b/tvix/eval/src/io.rs @@ -21,6 +21,9 @@ use crate::errors::ErrorKind; /// Defines how filesystem interaction occurs inside of tvix-eval. pub trait EvalIO { + /// Verify whether the file at the specified path exists. + fn path_exists(&self, path: PathBuf) -> Result; + /// Read the file at the specified path to a string. fn read_to_string(&self, path: PathBuf) -> Result; } @@ -32,8 +35,14 @@ pub struct StdIO; #[cfg(feature = "impure")] impl EvalIO for StdIO { + fn path_exists(&self, path: PathBuf) -> Result { + path.try_exists().map_err(|e| ErrorKind::IO { + path: Some(path), + error: std::rc::Rc::new(e), + }) + } + fn read_to_string(&self, path: PathBuf) -> Result { - let path: PathBuf = path.into(); std::fs::read_to_string(&path).map_err(|e| ErrorKind::IO { path: Some(path), error: std::rc::Rc::new(e), @@ -46,6 +55,12 @@ impl EvalIO for StdIO { pub struct DummyIO; impl EvalIO for DummyIO { + fn path_exists(&self, _: PathBuf) -> Result { + Err(ErrorKind::NotImplemented( + "I/O methods are not implemented in DummyIO", + )) + } + fn read_to_string(&self, _: PathBuf) -> Result { Err(ErrorKind::NotImplemented( "I/O methods are not implemented in DummyIO", -- cgit 1.4.1