Skip to content

Commit

Permalink
Simplify isomers.
Browse files Browse the repository at this point in the history
  • Loading branch information
olson-sean-k committed Oct 31, 2023
1 parent 6cef258 commit f998ec6
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 67 deletions.
91 changes: 64 additions & 27 deletions src/walk/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ mod state {
{
State::new(f(self.into_inner()))
}

pub fn get(&self) -> &T {
&self.inner
}
}

impl<K, T> State<K, Option<T>> {
Expand All @@ -49,7 +53,7 @@ mod state {

impl<K, T> AsRef<T> for State<K, T> {
fn as_ref(&self) -> &T {
&self.inner
self.get()
}
}

Expand All @@ -74,11 +78,11 @@ use state::*;
pub type Filtrate<T> = State<FiltrateKind, T>;
pub type Residue<T> = State<ResidueKind, T>;

pub type IsomerSubstituent<'a, F, R> = <F as Isomer<R>>::Substituent<'a>;
pub type SeparationFiltrate<S> = <S as Feed>::Filtrate;
pub type SeparationResidue<S> = <S as Feed>::Residue;
pub type SeparationSubstituent<'a, S> =
IsomerSubstituent<'a, SeparationFiltrate<S>, SeparationResidue<S>>;
//pub type IsomerSubstituent<'a, F, R> = <F as Isomer<R>>::Substituent<'a>;
//pub type SeparationFiltrate<S> = <S as Feed>::Filtrate;
//pub type SeparationResidue<S> = <S as Feed>::Residue;
//pub type SeparationSubstituent<'a, S> =
// IsomerSubstituent<'a, SeparationFiltrate<S>, SeparationResidue<S>>;

impl<T> Filtrate<T> {
pub fn filter(self) -> Residue<T> {
Expand Down Expand Up @@ -142,18 +146,26 @@ impl<T, U> Feed for (T, U) {
type Residue = U;
}

pub trait Isomer<T>: Sized {
pub trait Isomeric: Feed {
type Substituent<'a>
where
Self: 'a;

fn substituent(&self) -> Self::Substituent<'_>;
fn substituent(separation: &Separation<Self>) -> Self::Substituent<'_>;
}

//pub trait Isomer<T>: Sized {
// type Substituent<'a>
// where
// Self: 'a;
//
// fn substituent(&self) -> Self::Substituent<'_>;
//}

#[derive(Clone, Copy, Debug)]
pub enum Separation<S>
where
S: Feed,
S: Feed + ?Sized,
{
Filtrate(Filtrate<S::Filtrate>),
Residue(Residue<S::Residue>),
Expand Down Expand Up @@ -204,19 +216,26 @@ where
}
}

pub fn substituent(&self) -> <S::Filtrate as Isomer<S::Residue>>::Substituent<'_>
pub fn substituent(&self) -> S::Substituent<'_>
where
S::Filtrate: Isomer<S::Residue>,
S::Residue: for<'a> Isomer<
S::Filtrate,
Substituent<'a> = <S::Filtrate as Isomer<S::Residue>>::Substituent<'a>,
>,
S: Isomeric,
{
match self {
Separation::Filtrate(ref filtrate) => filtrate.as_ref().substituent(),
Separation::Residue(ref residue) => residue.as_ref().substituent(),
}
S::substituent(self)
}

//pub fn substituent(&self) -> <S::Filtrate as Isomer<S::Residue>>::Substituent<'_>
//where
// S::Filtrate: Isomer<S::Residue>,
// S::Residue: for<'a> Isomer<
// S::Filtrate,
// Substituent<'a> = <S::Filtrate as Isomer<S::Residue>>::Substituent<'a>,
// >,
//{
// match self {
// Separation::Filtrate(ref filtrate) => filtrate.as_ref().substituent(),
// Separation::Residue(ref residue) => residue.as_ref().substituent(),
// }
//}
}

// TODO: Base this `impl` on concrete types (i.e., `(T, TreeResidue<R>)`) instead of trait bounds
Expand Down Expand Up @@ -264,22 +283,40 @@ where
) -> Self
where
U: 'static + From<S::Filtrate>,
S::Filtrate: Isomer<TreeResidue<U>>,
TreeResidue<U>: for<'a> Isomer<
S::Filtrate,
Substituent<'a> = <S::Filtrate as Isomer<TreeResidue<U>>>::Substituent<'a>,
>,
S: Isomeric,
I: SkipTree,
F: FnOnce(
<S::Filtrate as Isomer<TreeResidue<U>>>::Substituent<'_>,
) -> Option<TreeResidue<()>>,
F: FnOnce(S::Substituent<'_>) -> Option<TreeResidue<()>>,
{
match f(self.substituent()) {
Some(TreeResidue::Tree(())) => self.filter_map_tree(cancellation, From::from),
Some(TreeResidue::Node(())) => self.filter_map_node(From::from),
_ => self,
}
}

//pub fn filter_tree_by_substituent<I, F>(
// self,
// cancellation: WalkCancellation<'_, I>,
// f: F,
//) -> Self
//where
// U: 'static + From<S::Filtrate>,
// S::Filtrate: Isomer<TreeResidue<U>>,
// TreeResidue<U>: for<'a> Isomer<
// S::Filtrate,
// Substituent<'a> = <S::Filtrate as Isomer<TreeResidue<U>>>::Substituent<'a>,
// >,
// I: SkipTree,
// F: FnOnce(
// <S::Filtrate as Isomer<TreeResidue<U>>>::Substituent<'_>,
// ) -> Option<TreeResidue<()>>,
//{
// match f(self.substituent()) {
// Some(TreeResidue::Tree(())) => self.filter_map_tree(cancellation, From::from),
// Some(TreeResidue::Node(())) => self.filter_map_node(From::from),
// _ => self,
// }
//}
}

impl<T, R> Separation<(Option<T>, R)> {
Expand Down
96 changes: 56 additions & 40 deletions src/walk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::capture::MatchedText;
use crate::encode::CompileError;
use crate::token::{self, Token, TokenTree};
use crate::walk::filter::{
FilterMapTree, Isomer, SeparatingFilter, SeparatingFilterInput, Separation, SkipTree,
FilterMapTree, Isomeric, SeparatingFilter, SeparatingFilterInput, Separation, SkipTree,
TreeIterator,
};
use crate::{BuildError, CandidatePath, Combine, Glob};
Expand All @@ -30,6 +30,22 @@ pub type FileFiltrate<T> = Result<T, WalkError>;
pub type FileResidue = TreeResidue<WalkEntry>;
pub type FileSeparation<T> = (FileFiltrate<T>, FileResidue);

impl<T> Isomeric for (T, FileResidue)
where
T: AsRef<WalkEntry>,
{
type Substituent<'a> = &'a WalkEntry
where
Self: 'a;

fn substituent(separation: &Separation<Self>) -> Self::Substituent<'_> {
match separation {
Separation::Filtrate(ref filtrate) => filtrate.get().as_ref(),
Separation::Residue(ref residue) => residue.get().as_ref(),
}
}
}

/// Describes errors that occur when matching a [`Glob`] against a directory tree.
///
/// `WalkError` implements conversion into [`io::Error`].
Expand Down Expand Up @@ -287,45 +303,45 @@ struct Anchor {
prefix: PathBuf,
}

impl Isomer<FileResidue> for GlobEntry {
type Substituent<'a> = &'a WalkEntry
where
Self: 'a;

fn substituent(&self) -> Self::Substituent<'_> {
self.as_ref()
}
}

impl Isomer<GlobEntry> for FileResidue {
type Substituent<'a> = &'a WalkEntry
where
Self: 'a;

fn substituent(&self) -> Self::Substituent<'_> {
self.as_ref()
}
}

impl Isomer<FileResidue> for WalkEntry {
type Substituent<'a> = &'a WalkEntry
where
Self: 'a;

fn substituent(&self) -> Self::Substituent<'_> {
self
}
}

impl Isomer<WalkEntry> for FileResidue {
type Substituent<'a> = &'a WalkEntry
where
Self: 'a;

fn substituent(&self) -> Self::Substituent<'_> {
self.as_ref()
}
}
//impl Isomer<FileResidue> for GlobEntry {
// type Substituent<'a> = &'a WalkEntry
// where
// Self: 'a;
//
// fn substituent(&self) -> Self::Substituent<'_> {
// self.as_ref()
// }
//}
//
//impl Isomer<GlobEntry> for FileResidue {
// type Substituent<'a> = &'a WalkEntry
// where
// Self: 'a;
//
// fn substituent(&self) -> Self::Substituent<'_> {
// self.as_ref()
// }
//}
//
//impl Isomer<FileResidue> for WalkEntry {
// type Substituent<'a> = &'a WalkEntry
// where
// Self: 'a;
//
// fn substituent(&self) -> Self::Substituent<'_> {
// self
// }
//}
//
//impl Isomer<WalkEntry> for FileResidue {
// type Substituent<'a> = &'a WalkEntry
// where
// Self: 'a;
//
// fn substituent(&self) -> Self::Substituent<'_> {
// self.as_ref()
// }
//}

#[derive(Clone, Debug)]
pub struct WalkEntry {
Expand Down

0 comments on commit f998ec6

Please sign in to comment.