diff options
author | Luca Casonato <hello@lcas.dev> | 2023-04-17 16:10:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-17 16:10:59 +0200 |
commit | bcbdaac7e6399870b5b6fcdf765013c1682fe80c (patch) | |
tree | 990e98d3787775a6349e444dc68eac352d69bd66 /ext/fs/lib.rs | |
parent | bad4b7554bd499975170f7d4e1a30540783aea69 (diff) |
chore(ext/fs): decouple fs trait from deno_core (#18732)
This commit removes the dependencies on `deno_core` for the Fs trait.
This allows to move the trait into a different crate that does not
depend on core in the limit.
This adds a new `bounds` field to `deno_core::extension!` that expands
to `where` clauses on the generated code. This allows to add bounds to
the extension parameters, such as `Fs::File: Resource`.
Diffstat (limited to 'ext/fs/lib.rs')
-rw-r--r-- | ext/fs/lib.rs | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index c1418815c..464d84ade 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -9,7 +9,6 @@ pub use crate::interface::FileSystem; pub use crate::interface::FsDirEntry; pub use crate::interface::FsError; pub use crate::interface::FsFileType; -pub use crate::interface::FsPermissions; pub use crate::interface::FsResult; pub use crate::interface::FsStat; pub use crate::interface::OpenOptions; @@ -17,11 +16,48 @@ use crate::ops::*; pub use crate::std_fs::StdFs; +use deno_core::error::AnyError; use deno_core::OpState; +use deno_core::Resource; use std::cell::RefCell; use std::convert::From; +use std::path::Path; use std::rc::Rc; +pub trait FsPermissions { + fn check_read(&mut self, p: &Path, api_name: &str) -> Result<(), AnyError>; + fn check_read_all(&mut self, api_name: &str) -> Result<(), AnyError>; + fn check_read_blind( + &mut self, + p: &Path, + display: &str, + api_name: &str, + ) -> Result<(), AnyError>; + fn check_write(&mut self, p: &Path, api_name: &str) -> Result<(), AnyError>; + fn check_write_all(&mut self, api_name: &str) -> Result<(), AnyError>; + fn check_write_blind( + &mut self, + p: &Path, + display: &str, + api_name: &str, + ) -> Result<(), AnyError>; + + fn check( + &mut self, + open_options: &OpenOptions, + path: &Path, + api_name: &str, + ) -> Result<(), AnyError> { + if open_options.read { + self.check_read(path, api_name)?; + } + if open_options.write || open_options.append { + self.check_write(path, api_name)?; + } + Ok(()) + } +} + struct UnstableChecker { pub unstable: bool, } @@ -52,6 +88,7 @@ pub(crate) fn check_unstable2(state: &Rc<RefCell<OpState>>, api_name: &str) { deno_core::extension!(deno_fs, deps = [ deno_web ], parameters = [Fs: FileSystem, P: FsPermissions], + bounds = [Fs::File: Resource], ops = [ op_cwd<Fs, P>, op_umask<Fs>, |