Skip to content

Commit

Permalink
Merge pull request #31 from tomoikey/feat/refactoring
Browse files Browse the repository at this point in the history
remove lifetime parameter
  • Loading branch information
tomoikey authored Nov 9, 2024
2 parents cd2a9a5 + 1f7378b commit 19f3cc2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 52 deletions.
20 changes: 9 additions & 11 deletions src/rule/collection/exists.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::{HashMap, HashSet, VecDeque};

use crate::rule::composer::Not;
use crate::rule::{ForAllRule, Rule};
use crate::rule::{ForAllRule, Iterable, Rule};
use crate::Refined;

/// A type that holds a value satisfying the `ExistsRule`
pub type Exists<RULE, ITERABLE> = Refined<ExistsRule<RULE, ITERABLE, <RULE as Rule>::Item>>;
pub type Exists<RULE, ITERABLE> = Refined<ExistsRule<RULE, ITERABLE>>;

/// A type that holds a Vec value satisfying the `ExistsRule`
pub type ExistsVec<RULE> = Refined<ExistsVecRule<RULE>>;
Expand All @@ -23,25 +23,23 @@ pub type ExistsHashMap<K, RULE> = Refined<ExistsHashMapRule<K, RULE>>;
pub type ExistsString<RULE> = Refined<ExistsStringRule<RULE>>;

/// Rule where at least one data in the collection satisfies the condition
pub type ExistsRule<RULE, ITERABLE, ITEM> = Not<ForAllRule<Not<RULE>, ITERABLE, ITEM>>;
pub type ExistsRule<RULE, ITERABLE> =
Not<ForAllRule<Not<RULE>, ITERABLE, <ITERABLE as Iterable>::Item>>;

/// Rule where at least one data in the `Vec` satisfies the condition
pub type ExistsVecRule<RULE> = ExistsRule<RULE, Vec<<RULE as Rule>::Item>, <RULE as Rule>::Item>;
pub type ExistsVecRule<RULE> = ExistsRule<RULE, Vec<<RULE as Rule>::Item>>;

/// Rule where at least one data in the `VecDeque` satisfies the condition
pub type ExistsVecDequeRule<RULE> =
ExistsRule<RULE, VecDeque<<RULE as Rule>::Item>, <RULE as Rule>::Item>;
pub type ExistsVecDequeRule<RULE> = ExistsRule<RULE, VecDeque<<RULE as Rule>::Item>>;

/// Rule where at least one data in the `HashSet` satisfies the condition
pub type ExistsHashSetRule<RULE> =
ExistsRule<RULE, HashSet<<RULE as Rule>::Item>, <RULE as Rule>::Item>;
pub type ExistsHashSetRule<RULE> = ExistsRule<RULE, HashSet<<RULE as Rule>::Item>>;

/// Rule where at least one data in the `HashMap` satisfies the condition
pub type ExistsHashMapRule<K, RULE> =
ExistsRule<RULE, HashMap<K, <RULE as Rule>::Item>, <RULE as Rule>::Item>;
pub type ExistsHashMapRule<K, RULE> = ExistsRule<RULE, HashMap<K, <RULE as Rule>::Item>>;

/// Rule where at least one data in the `String` satisfies the condition
pub type ExistsStringRule<RULE> = ExistsRule<RULE, String, char>;
pub type ExistsStringRule<RULE> = ExistsRule<RULE, String>;

#[cfg(test)]
mod tests {
Expand Down
18 changes: 9 additions & 9 deletions src/rule/collection/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ use crate::Refined;
use std::collections::VecDeque;

/// A type that holds a value satisfying the `InitRule`
pub type Init<'a, RULE, ITERABLE> = Refined<InitRule<'a, RULE, ITERABLE>>;
pub type Init<'a, RULE, ITERABLE> = Refined<InitRule<RULE, ITERABLE>>;

/// A type that holds a Vec value satisfying the `InitRule`
pub type InitVec<'a, RULE> = Refined<InitVecRule<'a, RULE>>;
pub type InitVec<RULE> = Refined<InitVecRule<RULE>>;

/// A type that holds a VecDeque value satisfying the `InitRule`
pub type InitVecDeque<'a, RULE> = Refined<InitVecDequeRule<'a, RULE>>;
pub type InitVecDeque<RULE> = Refined<InitVecDequeRule<RULE>>;

/// A type that holds a String value satisfying the `InitRule`
pub type InitString<'a, RULE> = Refined<InitStringRule<'a, RULE>>;
pub type InitString<'a, RULE> = Refined<InitStringRule<RULE>>;

/// Rule that applies to the initialization of a collection
pub type InitRule<'a, RULE, ITERABLE> =
ReverseRule<'a, SkipRule<RULE, ITERABLE, SkipFirst<<ITERABLE as Iterable<'a>>::Item>>>;
pub type InitRule<RULE, ITERABLE> =
ReverseRule<SkipRule<RULE, ITERABLE, SkipFirst<<ITERABLE as Iterable>::Item>>>;

/// Rule that applies to the initialization of a `Vec`
pub type InitVecRule<'a, RULE> = InitRule<'a, RULE, Vec<<RULE as Rule>::Item>>;
pub type InitVecRule<RULE> = InitRule<RULE, Vec<<RULE as Rule>::Item>>;

/// Rule that applies to the initialization of a `VecDeque`
pub type InitVecDequeRule<'a, RULE> = InitRule<'a, RULE, VecDeque<<RULE as Rule>::Item>>;
pub type InitVecDequeRule<RULE> = InitRule<RULE, VecDeque<<RULE as Rule>::Item>>;

/// Rule that applies to the initialization of a `String`
pub type InitStringRule<'a, RULE> = InitRule<'a, RULE, String>;
pub type InitStringRule<RULE> = InitRule<RULE, String>;

#[cfg(test)]
mod tests {
Expand Down
42 changes: 25 additions & 17 deletions src/rule/collection/iterable.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use std::collections::VecDeque;

pub trait Iterable<'a> {
type Item: 'a;
pub trait Iterable {
type Item;

fn into_iterator(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a>;
fn into_iterator<'a>(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a>
where
Self: 'a;
fn length(&self) -> usize;
}

impl<'a, T> Iterable<'a> for Vec<T>
where
T: 'a,
{
impl<T> Iterable for Vec<T> {
type Item = T;

fn into_iterator(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a> {
fn into_iterator<'a>(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a>
where
Self: 'a,
{
Box::new(self.into_iter())
}

Expand All @@ -22,13 +24,13 @@ where
}
}

impl<'a, T> Iterable<'a> for VecDeque<T>
where
T: 'a,
{
impl<T> Iterable for VecDeque<T> {
type Item = T;

fn into_iterator(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a> {
fn into_iterator<'a>(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a>
where
Self: 'a,
{
Box::new(self.into_iter())
}

Expand All @@ -37,10 +39,13 @@ where
}
}

impl<'a> Iterable<'a> for String {
impl Iterable for String {
type Item = char;

fn into_iterator(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a> {
fn into_iterator<'a>(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a>
where
Self: 'a,
{
Box::new(self.chars().collect::<Vec<_>>().into_iter())
}

Expand All @@ -49,10 +54,13 @@ impl<'a> Iterable<'a> for String {
}
}

impl<'a> Iterable<'a> for &'a str {
impl<'a> Iterable for &'a str {
type Item = char;

fn into_iterator(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a> {
fn into_iterator<'b>(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'b>
where
Self: 'b,
{
Box::new(self.chars())
}

Expand Down
16 changes: 8 additions & 8 deletions src/rule/collection/last.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ use crate::rule::{IndexRule, ReverseRule, Rule};
use crate::Refined;

/// A type that holds a value satisfying the `LastRule`
pub type Last<'a, RULE, ITERABLE> = Refined<LastRule<'a, RULE, ITERABLE>>;
pub type Last<RULE, ITERABLE> = Refined<LastRule<RULE, ITERABLE>>;

/// A type that holds a Vec value satisfying the `LastRule`
pub type LastVec<'a, RULE> = Refined<LastVecRule<'a, RULE>>;
pub type LastVec<RULE> = Refined<LastVecRule<RULE>>;

/// A type that holds a VecDeque value satisfying the `LastRule`
pub type LastVecDeque<'a, RULE> = Refined<LastVecDequeRule<'a, RULE>>;
pub type LastVecDeque<RULE> = Refined<LastVecDequeRule<RULE>>;

/// A type that holds a String value satisfying the `LastRule`
pub type LastString<'a, RULE> = Refined<LastStringRule<'a, RULE>>;
pub type LastString<RULE> = Refined<LastStringRule<RULE>>;

/// Rule where the last element satisfies the condition
pub type LastRule<'a, RULE, ITERABLE> = ReverseRule<'a, IndexRule<0, RULE, ITERABLE>>;
pub type LastRule<RULE, ITERABLE> = ReverseRule<IndexRule<0, RULE, ITERABLE>>;

/// Rule where the last element in the `Vec` satisfies the condition
pub type LastVecRule<'a, RULE> = LastRule<'a, RULE, Vec<<RULE as Rule>::Item>>;
pub type LastVecRule<RULE> = LastRule<RULE, Vec<<RULE as Rule>::Item>>;

/// Rule where the last element in the `VecDeque` satisfies the condition
pub type LastVecDequeRule<'a, RULE> = LastRule<'a, RULE, VecDeque<<RULE as Rule>::Item>>;
pub type LastVecDequeRule<RULE> = LastRule<RULE, VecDeque<<RULE as Rule>::Item>>;

/// Rule where the last element in the `String` satisfies the condition
pub type LastStringRule<'a, RULE> = LastRule<'a, RULE, String>;
pub type LastStringRule<RULE> = LastRule<RULE, String>;

#[cfg(test)]
mod tests {
Expand Down
10 changes: 5 additions & 5 deletions src/rule/collection/reverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ use crate::Refined;
use std::marker::PhantomData;

/// A type that holds a value satisfying the `ReverseRule`
pub type Reverse<'a, RULE> = Refined<ReverseRule<'a, RULE>>;
pub type Reverse<'a, RULE> = Refined<ReverseRule<RULE>>;

/// Rule where the data in the collection satisfies the condition after reversing
pub struct ReverseRule<'a, RULE>
pub struct ReverseRule<RULE>
where
RULE: Rule,
{
_phantom_data: PhantomData<&'a RULE>,
_phantom_data: PhantomData<RULE>,
}

impl<'a, RULE, ITERABLE> Rule for ReverseRule<'a, RULE>
impl<RULE, ITERABLE> Rule for ReverseRule<RULE>
where
RULE: Rule<Item = ITERABLE>,
ITERABLE: Iterable<'a> + FromIterator<ITERABLE::Item>,
ITERABLE: Iterable + FromIterator<ITERABLE::Item>,
{
type Item = RULE::Item;

Expand Down
4 changes: 2 additions & 2 deletions src/rule/collection/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ where
_phantom_data: PhantomData<(RULE, ITERABLE, OPTION)>,
}

impl<'a, RULE, ITERABLE, OPTION> Rule for SkipRule<RULE, ITERABLE, OPTION>
impl<RULE, ITERABLE, OPTION> Rule for SkipRule<RULE, ITERABLE, OPTION>
where
RULE: Rule,
ITERABLE: Iterable<'a, Item = RULE::Item> + FromIterator<RULE::Item>,
ITERABLE: Iterable<Item = RULE::Item> + FromIterator<RULE::Item>,
OPTION: SkipOption<Item = RULE::Item>,
{
type Item = ITERABLE;
Expand Down

0 comments on commit 19f3cc2

Please sign in to comment.