Skip to content

Commit

Permalink
fix reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Sep 17, 2024
1 parent 44eb1b6 commit 65991f8
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 119 deletions.
2 changes: 2 additions & 0 deletions src/rule/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ mod head;
mod index;
mod init;
mod last;
mod reverse;

pub use exists::*;
pub use for_all::*;
pub use head::*;
pub use index::*;
pub use init::*;
pub use last::*;
pub use reverse::*;
10 changes: 2 additions & 8 deletions src/rule/collection/last.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
mod collection;
mod string;

use std::collections::VecDeque;
use std::marker::PhantomData;

use crate::rule::Rule;
use crate::rule::{Index0Rule, ReverseRule, Rule};
use crate::Refined;

/// A type that holds a value satisfying the `LastRule`
Expand All @@ -20,9 +16,7 @@ pub type LastVecDeque<RULE> = Refined<LastVecDequeRule<RULE>>;
pub type LastString<RULE> = Refined<LastStringRule<RULE>>;

/// Rule where the last element satisfies the condition
pub struct LastRule<RULE, ITERABLE> {
_phantom_data: PhantomData<(RULE, ITERABLE)>,
}
pub type LastRule<RULE, ITERABLE> = ReverseRule<Index0Rule<RULE, ITERABLE>, ITERABLE>;

/// Rule where the last element in the `Vec` satisfies the condition
pub type LastVecRule<RULE> = LastRule<RULE, Vec<<RULE as Rule>::Item>>;
Expand Down
64 changes: 0 additions & 64 deletions src/rule/collection/last/collection.rs

This file was deleted.

47 changes: 0 additions & 47 deletions src/rule/collection/last/string.rs

This file was deleted.

25 changes: 25 additions & 0 deletions src/rule/collection/reverse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
mod collection;
mod string;

use crate::rule::Rule;
use crate::Refined;
use std::collections::VecDeque;
use std::marker::PhantomData;

pub type Reverse<RULE, ITERABLE> = Refined<ReverseRule<RULE, ITERABLE>>;

pub type ReverseVec<RULE> = Refined<ReverseVecRule<RULE>>;

pub type ReverseVecDeque<RULE> = Refined<ReverseVecDequeRule<RULE>>;

pub type ReverseString<RULE> = Refined<ReverseStringRule<RULE>>;

pub struct ReverseRule<RULE, ITERABLE> {
_phantom_data: PhantomData<(RULE, ITERABLE)>,
}

pub type ReverseVecRule<RULE> = ReverseRule<RULE, Vec<<RULE as Rule>::Item>>;

pub type ReverseVecDequeRule<RULE> = ReverseRule<RULE, VecDeque<<RULE as Rule>::Item>>;

pub type ReverseStringRule<RULE> = ReverseRule<RULE, String>;
31 changes: 31 additions & 0 deletions src/rule/collection/reverse/collection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::collections::VecDeque;

use crate::result::Error;
use crate::rule::collection::reverse::ReverseRule;
use crate::rule::Rule;

macro_rules! impl_reverse {
($($t:ty),*) => {
$(
impl<RULE, ITEM> Rule for ReverseRule<RULE, $t>
where
RULE: Rule<Item = $t>,
{
type Item = $t;

fn validate(target: Self::Item) -> Result<Self::Item, Error<Self::Item>> {
match RULE::validate(target.into_iter().rev().collect()) {
Ok(value) => Ok(value.into_iter().rev().collect()),
Err(e) => {
let message = e.to_string();
Err(Error::new(e.into_value().into_iter().rev().collect(), message))
},
}
}
}
)*
};
() => {};
}

impl_reverse![Vec<ITEM>, VecDeque<ITEM>];
21 changes: 21 additions & 0 deletions src/rule/collection/reverse/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::rule::{ReverseRule, Rule};

impl<RULE> Rule for ReverseRule<RULE, String>
where
RULE: Rule<Item = String>,
{
type Item = String;

fn validate(target: Self::Item) -> Result<Self::Item, crate::result::Error<Self::Item>> {
match RULE::validate(target.chars().rev().collect()) {
Ok(value) => Ok(value.chars().rev().collect()),
Err(e) => {
let message = e.to_string();
Err(crate::result::Error::new(
e.into_value().chars().rev().collect(),
message,
))
}
}
}
}

0 comments on commit 65991f8

Please sign in to comment.