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`.
This commit is contained in:
Luca Casonato 2023-04-17 16:10:59 +02:00 committed by GitHub
parent bad4b7554b
commit bcbdaac7e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 104 additions and 72 deletions

View file

@ -5,32 +5,8 @@ use std::path::Path;
use std::path::PathBuf;
use std::rc::Rc;
use deno_core::error::not_supported;
use deno_core::error::resource_unavailable;
use deno_core::error::AnyError;
use deno_core::Resource;
use serde::Deserialize;
use serde::Serialize;
use tokio::task::JoinError;
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>;
}
#[derive(Deserialize, Default, Debug, Clone, Copy)]
#[serde(rename_all = "camelCase")]
@ -74,21 +50,6 @@ impl OpenOptions {
mode,
}
}
pub(crate) fn check<P: FsPermissions>(
&self,
permissions: &mut P,
path: &Path,
api_name: &str,
) -> Result<(), AnyError> {
if self.read {
permissions.check_read(path, api_name)?;
}
if self.write || self.append {
permissions.check_write(path, api_name)?;
}
Ok(())
}
}
pub struct FsStat {
@ -141,28 +102,6 @@ impl From<io::Error> for FsError {
}
}
impl From<JoinError> for FsError {
fn from(err: JoinError) -> Self {
if err.is_cancelled() {
todo!("async tasks must not be cancelled")
}
if err.is_panic() {
std::panic::resume_unwind(err.into_panic()); // resume the panic on the main thread
}
unreachable!()
}
}
impl From<FsError> for AnyError {
fn from(err: FsError) -> Self {
match err {
FsError::Io(err) => AnyError::from(err),
FsError::FileBusy => resource_unavailable(),
FsError::NotSupported => not_supported(),
}
}
}
pub type FsResult<T> = Result<T, FsError>;
#[async_trait::async_trait(?Send)]
@ -214,7 +153,7 @@ pub trait File {
#[async_trait::async_trait(?Send)]
pub trait FileSystem: Clone {
type File: File + Resource;
type File: File;
fn cwd(&self) -> FsResult<PathBuf>;
fn tmp_dir(&self) -> FsResult<PathBuf>;