Skip to content

Commit

Permalink
Introduce a more sophisticated DepthBehavior type.
Browse files Browse the repository at this point in the history
This change replaces the simple `usize` of the `WalkBehavior::depth`
field with a much more rich `DepthBehavior` type. `DepthBehavior`
supports both minimum and maximum depth bounds and is well-typed to
provide better APIs and conversions.

Perhaps more importantly, this change removes much of the awareness of
invariant prefixes and adjusted roots in `Glob`s from the base walk
implementation over paths. Root and relative paths are now more
consistent, referring to the walked `Path` or, similarly, the path given
to `Glob::walk` functions. The only exception are `Glob`s that have a
root, for which the root is formed from the invariant prefix (because a
rooted prefix escapes the path given to `Glob::walk` functions).

Depth too is now more consistently applied from this root. Prefixes in
`Glob`s are no longer exposed in walk APIs (`GlobWalker` has been
removed from public APIs). To allow for more control over depth
behaviors, `Glob` can expose depth variance, which can then be used to
construct a `DepthBehavior`. This is not implemented by this change.
  • Loading branch information
olson-sean-k committed Mar 15, 2024
1 parent 93c5926 commit 905778b
Show file tree
Hide file tree
Showing 5 changed files with 553 additions and 321 deletions.
27 changes: 20 additions & 7 deletions src/lib.rs
Expand Up @@ -66,6 +66,7 @@ pub mod prelude {
use miette::Diagnostic;
use regex::Regex;
use std::borrow::{Borrow, Cow};
use std::cmp::Ordering;
use std::convert::Infallible;
use std::ffi::OsStr;
use std::fmt::{self, Debug, Display, Formatter};
Expand Down Expand Up @@ -601,13 +602,6 @@ pub struct Glob<'t> {
}

impl<'t> Glob<'t> {
fn compile<T>(tree: impl Borrow<T>) -> Result<Regex, CompileError>
where
T: ConcatenationTree<'t>,
{
encode::compile(tree)
}

// TODO: Document pattern syntax in the crate documentation and refer to it here.
/// Constructs a [`Glob`] from a glob expression.
///
Expand Down Expand Up @@ -791,6 +785,13 @@ impl<'t> Glob<'t> {
pub fn is_empty(&self) -> bool {
self.tree.as_ref().as_token().is_empty()
}

fn compile<T>(tree: impl Borrow<T>) -> Result<Regex, CompileError>
where
T: ConcatenationTree<'t>,
{
encode::compile(tree)
}
}

impl Display for Glob<'_> {
Expand Down Expand Up @@ -1063,6 +1064,18 @@ fn parse_and_check(
Ok(checked)
}

fn minmax<T>(lhs: T, rhs: T) -> [T; 2]
where
T: Ord,
{
use Ordering::{Equal, Greater, Less};

match lhs.cmp(&rhs) {
Equal | Less => [lhs, rhs],
Greater => [rhs, lhs],
}
}

#[cfg(test)]
pub mod harness {
use expect_macro::expect;
Expand Down
3 changes: 0 additions & 3 deletions src/token/variance/bound.rs
Expand Up @@ -211,9 +211,6 @@ impl From<VariantRange> for NaturalRange {
}
}

// NOTE: Given the naturals X and Y where X < Y, this defines an unconventional meaning for the
// range [Y,X] and repetitions like `<_:10,1>`: the bounds are reordered, so `<_:10,1>` and
// `<_:1,10>` are the same.
impl<T> From<(usize, T)> for NaturalRange
where
T: Into<Option<usize>>,
Expand Down
15 changes: 1 addition & 14 deletions src/token/variance/invariant/natural.rs
@@ -1,4 +1,3 @@
use std::cmp::Ordering;
use std::num::NonZeroUsize;

use crate::token::variance::bound::{
Expand Down Expand Up @@ -80,7 +79,7 @@ macro_rules! impl_invariant_natural {
}

fn bound(lhs: Self, rhs: Self) -> Boundedness<Self::Bound> {
let (lower, upper) = self::minmax(lhs, rhs);
let [lower, upper] = crate::minmax(lhs, rhs);
BoundedVariantRange::try_from_lower_and_upper(lower.0, upper.0)
.map_or(Unbounded, Bounded)
}
Expand Down Expand Up @@ -144,15 +143,3 @@ impl GlobVariance<Depth> {
!self.has_upper_bound()
}
}

fn minmax<T>(lhs: T, rhs: T) -> (T, T)
where
T: Ord,
{
use Ordering::{Equal, Greater, Less};

match lhs.cmp(&rhs) {
Equal | Less => (lhs, rhs),
Greater => (rhs, lhs),
}
}

0 comments on commit 905778b

Please sign in to comment.