-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
81 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
)) | ||
} | ||
} | ||
} | ||
} |