diff --git a/crates/ruff_db/src/system.rs b/crates/ruff_db/src/system.rs index 784b6b8f3c99c7..31b8191acbf1ca 100644 --- a/crates/ruff_db/src/system.rs +++ b/crates/ruff_db/src/system.rs @@ -104,14 +104,14 @@ impl FileType { } } -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug)] pub struct DirEntry { path: SystemPathBuf, - file_type: FileType, + file_type: Result, } impl DirEntry { - pub fn new(path: SystemPathBuf, file_type: FileType) -> Self { + pub fn new(path: SystemPathBuf, file_type: Result) -> Self { Self { path, file_type } } @@ -119,7 +119,13 @@ impl DirEntry { &self.path } - pub fn file_type(&self) -> FileType { - self.file_type + pub fn file_type(&self) -> &Result { + &self.file_type + } +} + +impl PartialEq for DirEntry { + fn eq(&self, other: &Self) -> bool { + self.path == other.path } } diff --git a/crates/ruff_db/src/system/memory_fs.rs b/crates/ruff_db/src/system/memory_fs.rs index 418a4c49bfb915..691ca9221f4a94 100644 --- a/crates/ruff_db/src/system/memory_fs.rs +++ b/crates/ruff_db/src/system/memory_fs.rs @@ -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)))); } } } @@ -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) } diff --git a/crates/ruff_db/src/system/os.rs b/crates/ruff_db/src/system/os.rs index e7adedcba0176d..d274038b7cb7fa 100644 --- a/crates/ruff_db/src/system/os.rs +++ b/crates/ruff_db/src/system/os.rs @@ -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)), )) } } @@ -93,14 +93,13 @@ impl From for FileType { } } -impl TryFrom for DirEntry { - type Error = std::io::Error; - - fn try_from(value: camino::Utf8DirEntry) -> Result { - Ok(Self { - path: SystemPathBuf::from_utf8_path_buf(value.path().to_path_buf()), - file_type: FileType::from(value.file_type()?), - }) +impl From 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, + } } } @@ -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) }