Skip to content

Commit

Permalink
non-compiling WIP for File trait
Browse files Browse the repository at this point in the history
  • Loading branch information
taminob committed Jan 24, 2024
1 parent c163b2a commit 68fe930
Show file tree
Hide file tree
Showing 16 changed files with 502 additions and 251 deletions.
4 changes: 2 additions & 2 deletions src/fs/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use log::*;
use crate::fs::dir::{DotFilter, Files};
use crate::fs::File;

pub trait Directory: Sized {
pub trait Directory<F: File>: Sized {
/// Create a new Dir object filled with all the files in the directory
/// pointed to by the given path. Fails if the directory can’t be read, or
/// isn’t actually a directory, or if there’s an IO error that occurs at
Expand All @@ -29,7 +29,7 @@ pub trait Directory: Sized {
git_ignoring: bool,
deref_links: bool,
total_size: bool,
) -> Files<'dir, 'ig>;
) -> Files<'dir, 'ig, F>;

/// Whether this directory contains a file with the given path.
fn contains(&self, path: &Path) -> bool;
Expand Down
22 changes: 12 additions & 10 deletions src/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::slice::Iter as SliceIter;

use log::*;

use crate::fs::File;
use crate::fs::{File, FsFile};

/// A **Dir** provides a cached list of the file paths in a directory that’s
/// being listed.
Expand Down Expand Up @@ -45,14 +45,14 @@ impl Dir {

/// Produce an iterator of IO results of trying to read all the files in
/// this directory.
pub fn files<'dir, 'ig>(
pub fn files<'dir, 'ig, F: File>(
&'dir self,
dots: DotFilter,
git: Option<&'ig GitCache>,
git_ignoring: bool,
deref_links: bool,
total_size: bool,
) -> Files<'dir, 'ig> {
) -> Files<'dir, 'ig, F> {
Files {
inner: self.contents.iter(),
dir: self,
Expand All @@ -78,7 +78,7 @@ impl Dir {

/// Iterator over reading the contents of a directory as `File` objects.
#[allow(clippy::struct_excessive_bools)]
pub struct Files<'dir, 'ig> {
pub struct Files<'dir, 'ig, F: File> {
/// The internal iterator over the paths that have been read already.
inner: SliceIter<'dir, PathBuf>,

Expand All @@ -101,9 +101,11 @@ pub struct Files<'dir, 'ig> {

/// Whether to calculate the directory size recursively
total_size: bool,

dummy: std::marker::PhantomData<F>, // TODO
}

impl<'dir, 'ig> Files<'dir, 'ig> {
impl<'dir, 'ig, T: File> Files<'dir, 'ig, T> {
fn parent(&self) -> PathBuf {
// We can’t use `Path#parent` here because all it does is remove the
// last path component, which is no good for us if the path is
Expand All @@ -115,7 +117,7 @@ impl<'dir, 'ig> Files<'dir, 'ig> {

/// Go through the directory until we encounter a file we can list (which
/// varies depending on the dotfile visibility flag)
fn next_visible_file(&mut self) -> Option<Result<File<'dir>, (PathBuf, io::Error)>> {
fn next_visible_file<F: File>(&mut self) -> Option<Result<F, (PathBuf, io::Error)>> {
loop {
if let Some(path) = self.inner.next() {
let filename = File::filename(path);
Expand Down Expand Up @@ -174,23 +176,23 @@ enum DotsNext {
Files,
}

impl<'dir, 'ig> Iterator for Files<'dir, 'ig> {
type Item = Result<File<'dir>, (PathBuf, io::Error)>;
impl<'dir, 'ig, F: File> Iterator for Files<'dir, 'ig, F> {
type Item = Result<F, (PathBuf, io::Error)>;

fn next(&mut self) -> Option<Self::Item> {
match self.dots {
DotsNext::Dot => {
self.dots = DotsNext::DotDot;
Some(
File::new_aa_current(self.dir, self.total_size)
FsFile::new_aa_current(self.dir, self.total_size)
.map_err(|e| (Path::new(".").to_path_buf(), e)),
)
}

DotsNext::DotDot => {
self.dots = DotsNext::Files;
Some(
File::new_aa_parent(self.parent(), self.dir, self.total_size)
FsFile::new_aa_parent(self.parent(), self.dir, self.total_size)
.map_err(|e| (self.parent(), e)),
)
}
Expand Down
Loading

0 comments on commit 68fe930

Please sign in to comment.