Skip to content

Commit

Permalink
red-knot: Add directory support to MemoryFileSystem (#11825)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Jun 13, 2024
1 parent d4dd96d commit 22b6488
Show file tree
Hide file tree
Showing 5 changed files with 463 additions and 93 deletions.
20 changes: 18 additions & 2 deletions crates/ruff_db/src/file_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ pub trait FileSystem {

/// Returns `true` if `path` exists.
fn exists(&self, path: &FileSystemPath) -> bool;

/// Returns `true` if `path` exists and is a directory.
fn is_directory(&self, path: &FileSystemPath) -> bool {
self.metadata(path)
.map_or(false, |metadata| metadata.file_type.is_directory())
}

/// Returns `true` if `path` exists and is a file.
fn is_file(&self, path: &FileSystemPath) -> bool {
self.metadata(path)
.map_or(false, |metadata| metadata.file_type.is_file())
}
}

// TODO support untitled files for the LSP use case. Wrap a `str` and `String`
Expand All @@ -37,7 +49,7 @@ pub trait FileSystem {
///
/// The path is guaranteed to be valid UTF-8.
#[repr(transparent)]
#[derive(Eq, PartialEq, Hash)]
#[derive(Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct FileSystemPath(Utf8Path);

impl FileSystemPath {
Expand Down Expand Up @@ -95,7 +107,7 @@ impl FileSystemPath {
///
/// The path is guaranteed to be valid UTF-8.
#[repr(transparent)]
#[derive(Eq, PartialEq, Clone, Hash)]
#[derive(Eq, PartialEq, Clone, Hash, PartialOrd, Ord)]
pub struct FileSystemPathBuf(Utf8PathBuf);

impl Default for FileSystemPathBuf {
Expand All @@ -109,6 +121,10 @@ impl FileSystemPathBuf {
Self(Utf8PathBuf::new())
}

pub fn from_utf8_path_buf(path: Utf8PathBuf) -> Self {
Self(path)
}

#[inline]
pub fn as_path(&self) -> &FileSystemPath {
FileSystemPath::new(&self.0)
Expand Down
Loading

0 comments on commit 22b6488

Please sign in to comment.