Skip to content

Commit

Permalink
Improvements to DirEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Jul 12, 2024
1 parent 5d2038e commit 860c4f8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
16 changes: 11 additions & 5 deletions crates/ruff_db/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,28 @@ impl FileType {
}
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug)]
pub struct DirEntry {
path: SystemPathBuf,
file_type: FileType,
file_type: Result<FileType>,
}

impl DirEntry {
pub fn new(path: SystemPathBuf, file_type: FileType) -> Self {
pub fn new(path: SystemPathBuf, file_type: Result<FileType>) -> Self {
Self { path, file_type }
}

pub fn path(&self) -> &SystemPath {
&self.path
}

pub fn file_type(&self) -> FileType {
self.file_type
pub fn file_type(&self) -> &Result<FileType> {
&self.file_type
}
}

impl PartialEq for DirEntry {
fn eq(&self, other: &Self) -> bool {
self.path == other.path
}
}
8 changes: 4 additions & 4 deletions crates/ruff_db/src/system/memory_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl<'a> Iterator for DirectoryIterator<'a> {
} else {
FileType::File
};
break Some(Ok(DirEntry::new(path, file_type)));
break Some(Ok(DirEntry::new(path, Ok(file_type))));
}
}
}
Expand Down Expand Up @@ -675,9 +675,9 @@ mod tests {
.map(Result::unwrap)
.collect();
let expected_contents = vec![
DirEntry::new(SystemPathBuf::from("/a/bar.py"), FileType::File),
DirEntry::new(SystemPathBuf::from("/a/baz.pyi"), FileType::File),
DirEntry::new(SystemPathBuf::from("/a/foo"), FileType::Directory),
DirEntry::new(SystemPathBuf::from("/a/bar.py"), Ok(FileType::File)),
DirEntry::new(SystemPathBuf::from("/a/baz.pyi"), Ok(FileType::File)),
DirEntry::new(SystemPathBuf::from("/a/foo"), Ok(FileType::Directory)),
];
assert_eq!(contents, expected_contents)
}
Expand Down
25 changes: 12 additions & 13 deletions crates/ruff_db/src/system/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl System for OsSystem {
Ok(Box::new(
path.as_camino_path()
.read_dir_utf8()?
.map(|res| res.and_then(DirEntry::try_from)),
.map(|res| res.map(DirEntry::from)),
))
}
}
Expand All @@ -93,14 +93,13 @@ impl From<std::fs::FileType> for FileType {
}
}

impl TryFrom<camino::Utf8DirEntry> for DirEntry {
type Error = std::io::Error;

fn try_from(value: camino::Utf8DirEntry) -> Result<Self> {
Ok(Self {
path: SystemPathBuf::from_utf8_path_buf(value.path().to_path_buf()),
file_type: FileType::from(value.file_type()?),
})
impl From<camino::Utf8DirEntry> for DirEntry {
fn from(value: camino::Utf8DirEntry) -> Self {
let file_type = value.file_type().map(FileType::from);
Self {
path: SystemPathBuf::from_utf8_path_buf(value.into_path()),
file_type,
}
}
}

Expand Down Expand Up @@ -128,12 +127,12 @@ mod tests {
.unwrap()
.map(Result::unwrap)
.collect();
sorted_contents.sort();
sorted_contents.sort_by(|a, b| a.path.cmp(&b.path));

let expected_contents = vec![
DirEntry::new(tempdir_path.join("a/bar.py"), FileType::File),
DirEntry::new(tempdir_path.join("a/baz.pyi"), FileType::File),
DirEntry::new(tempdir_path.join("a/foo"), FileType::Directory),
DirEntry::new(tempdir_path.join("a/bar.py"), Ok(FileType::File)),
DirEntry::new(tempdir_path.join("a/baz.pyi"), Ok(FileType::File)),
DirEntry::new(tempdir_path.join("a/foo"), Ok(FileType::Directory)),
];
assert_eq!(sorted_contents, expected_contents)
}
Expand Down

0 comments on commit 860c4f8

Please sign in to comment.