Skip to content

Commit

Permalink
[wip] Refactor depth behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
olson-sean-k committed Mar 6, 2024
1 parent 93c5926 commit e43d693
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::token::walk::{BranchFold, Fold, FoldMap, Starting, TokenEntry};
use crate::{StrExt as _, PATHS_ARE_CASE_INSENSITIVE};

pub use crate::token::parse::{parse, ParseError, ROOT_SEPARATOR_EXPRESSION};
pub use crate::token::variance::bound::NaturalRange;
pub use crate::token::variance::bound::{NaturalRange, VariantRange};

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Lint

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Lint

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, nightly)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, nightly)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 1.75.0)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 1.75.0)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, stable)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, stable)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, beta)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, beta)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (macOS-latest, 1.75.0)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (macOS-latest, 1.75.0)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (macOS-latest, nightly)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (macOS-latest, stable)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (macOS-latest, stable)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (macOS-latest, nightly)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, 1.75.0)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, 1.75.0)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, beta)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, beta)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, nightly)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, nightly)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, stable)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, stable)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (macOS-latest, beta)

unused import: `VariantRange`

Check warning on line 24 in src/token/mod.rs

View workflow job for this annotation

GitHub Actions / Test (macOS-latest, beta)

unused import: `VariantRange`
pub use crate::token::variance::invariant::{Breadth, Depth, GlobVariance, Invariant, Size, Text};
pub use crate::token::variance::Variance;

Expand Down
46 changes: 25 additions & 21 deletions src/walk/glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,44 +193,48 @@ impl<'t> Glob<'t> {
Some(prefix.into())
}
};
// Establish the root directory and any prefix in that root path that is not a part of the
// glob expression. The directory tree is traversed from `root`, which may include an
// invariant prefix from the glob. The `prefix` is an integer that specifies how many
// components from the end of the root path must be popped to get the portion of the root
// path that is not present in the glob. The prefix may be empty or may be the entirety of
// `root` depending on `directory` and the glob.
// Establish the root path and any pivot in that root path from the given directory and any
// invariant prefix in the glob. The file system is traversed from this root path. The
// pivot partitions the root path into the given directory and any invariant prefix by
// specifying how many components from the end of the root path must be popped to restore
// the given directory. The popped components form the invariant prefix of the glob. Either
// partition of the root path may be empty depending on the given directory and the glob
// pattern. In this way, any invariant prefix of the glob becomes a postfix in the root
// path.
//
// Note that a rooted glob, like in `Path::join`, replaces `directory` when establishing
// the root path. In this case, there is no prefix, as the entire root path is present in
// the glob expression.
let (root, prefix) = match prefix {
// Note that a rooted glob, like in `Path::join`, replaces the given directory when
// establishing the root path. In this case, there is no invariant prefix (the pivot is
// zero), as the entire root path is present in the glob expression and the given directory
// is completely discarded.
let (root, pivot) = match prefix {
Some(prefix) => directory.join_and_get_depth(prefix),
_ => (directory, 0),
};
Anchor { root, prefix }
Anchor { root, pivot }
}
}

/// Root path and prefix of a `Glob` when walking a particular path.
/// Root path and pivot of a `Glob` when walking a particular directory.
#[derive(Clone, Debug)]
struct Anchor {
/// The root (starting) directory of the walk.
/// The root (starting) path of the walk.
root: PathBuf,
// TODO: Is there a better name for this? This is a prefix w.r.t. a glob but is a suffix w.r.t.
// the root directory. This can be a bit confusing since either perspective is reasonable
// (and in some contexts one may be more intuitive than the other).
/// The number of components from the end of `root` that are present in the `Glob`'s
/// expression.
prefix: usize,
/// The number of components from the end of `root` that are present in any invariant prefix of
/// the glob expression.
///
/// The pivot partitions the root path into a target directory and any invariant prefix in the
/// `Glob` (this prefix becomes a postfix in the root path or, when rooted, replaces any target
/// directory).
pivot: usize,
}

impl Anchor {
pub fn root_prefix_paths(&self) -> (&Path, &Path) {
self.root.split_at_depth(self.prefix)
self.root.split_at_depth(self.pivot)
}

pub fn walk_with_behavior(self, behavior: impl Into<WalkBehavior>) -> WalkTree {
WalkTree::with_prefix_and_behavior(self.root, self.prefix, behavior)
WalkTree::with_pivot_and_behavior(self.root, self.pivot, behavior)
}
}

Expand Down
Loading

0 comments on commit e43d693

Please sign in to comment.