red-knot: Add directory support to MemoryFileSystem (#11825)

This commit is contained in:
Micha Reiser 2024-06-13 08:48:28 +01:00 committed by GitHub
parent d4dd96d1f4
commit 22b6488550
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 463 additions and 93 deletions

View file

@ -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`
@ -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 {
@ -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 {
@ -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)