Skip to content

Commit

Permalink
impl: reverse test
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Sep 18, 2024
1 parent a0b7dbd commit 150602f
Show file tree
Hide file tree
Showing 3 changed files with 284 additions and 31 deletions.
128 changes: 122 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,22 @@ fn example_14() -> anyhow::Result<()> {

```rust
fn example_15() -> anyhow::Result<()> {
let table = vec![
(vec!["hey".to_string(), "hello".to_string(), "world".to_string()], true),
(vec!["hey".to_string(), "hello".to_string(), "".to_string()], false),
(vec!["hey".to_string(), "".to_string(), "world".to_string()], false),
(vec!["hey".to_string(), "".to_string(), "".to_string()], false),
(vec!["".to_string(), "hello".to_string(), "world".to_string()], true),
(vec!["".to_string(), "hello".to_string(), "".to_string()], false),
(vec!["".to_string(), "".to_string(), "world".to_string()], false),
(vec!["".to_string(), "".to_string(), "".to_string()], false),
];

for (value, ok) in table {
let tail = TailVec::<NonEmptyStringRule>::new(value.clone());
assert_eq!(tail.is_ok(), ok);
}

Ok(())
}
```
Expand All @@ -407,6 +423,22 @@ fn example_15() -> anyhow::Result<()> {

```rust
fn example_16() -> anyhow::Result<()> {
let table = vec![
(vec!["hey".to_string(), "hello".to_string(), "world".to_string()], true),
(vec!["hey".to_string(), "hello".to_string(), "".to_string()], true),
(vec!["hey".to_string(), "".to_string(), "world".to_string()], false),
(vec!["hey".to_string(), "".to_string(), "".to_string()], false),
(vec!["".to_string(), "hello".to_string(), "world".to_string()], false),
(vec!["".to_string(), "hello".to_string(), "".to_string()], false),
(vec!["".to_string(), "".to_string(), "world".to_string()], false),
(vec!["".to_string(), "".to_string(), "".to_string()], false),
];

for (value, ok) in table {
let init = InitVec::<NonEmptyStringRule>::new(value.clone());
assert_eq!(init.is_ok(), ok);
}

Ok(())
}
```
Expand All @@ -417,10 +449,94 @@ fn example_16() -> anyhow::Result<()> {

```rust
fn example_17() -> anyhow::Result<()> {
let table = vec![
(vec!["good morning".to_string(), "hello".to_string()], true),
(vec!["good morning".to_string(), "".to_string()], false),
(vec!["".to_string(), "hello".to_string()], true),
(vec!["".to_string(), "".to_string()], false),
];

for (value, expected) in table {
let refined = Index1Vec::<NonEmptyStringRule>::new(value.clone());
assert_eq!(refined.is_ok(), expected);
}

Ok(())
}
```

# `Reverse`

`Reverse` is a rule that applies a specific rule to all elements in the Iterator in reverse order.
`refined_type` crate has `Index0` to `Index10` by default.

```rust
fn example_18() -> Result<(), Error<Vec<i32>>> {
let table = vec![
(vec!["good morning".to_string(), "hello".to_string()], true),
(vec!["good morning".to_string(), "".to_string()], false),
(vec!["".to_string(), "hello".to_string()], true),
(vec!["".to_string(), "".to_string()], false),
];

for (value, expected) in table {
let refined = Reverse::<Index0VecRule<NonEmptyStringRule>, _>::new(value.clone());
assert_eq!(refined.is_ok(), expected);
}

Ok(())
}
```

if you need more, you can define it like this.

```rust
define_index_refined!(11, 12, 13);
define_index_rule!(11, 12, 13);
```

# `Skip`

`Skip` is a rule that applies a specific rule to the elements of the Iterator while skipping the elements according
to `SkipOption`.

```rust
fn example_19() -> Result<(), Error<Vec<i32>>> {
let table = vec![
(vec!["hey".to_string(), "hello".to_string(), "world".to_string()], true),
(vec!["hey".to_string(), "hello".to_string(), "".to_string()], false),
(vec!["hey".to_string(), "".to_string(), "world".to_string()], false),
(vec!["hey".to_string(), "".to_string(), "".to_string()], false),
(vec!["".to_string(), "hello".to_string(), "world".to_string()], true),
(vec!["".to_string(), "hello".to_string(), "".to_string()], false),
(vec!["".to_string(), "".to_string(), "world".to_string()], false),
(vec!["".to_string(), "".to_string(), "".to_string()], false),
];

for (value, ok) in table {
let init = SkipVec::<NonEmptyStringRule, SkipFirst<_>>::new(value.clone());
assert_eq!(init.is_ok(), ok);
}

Ok(())
}
```

if you need more skip option, you can define it like this.

```rust
pub struct NoSkip<T> {
_phantom_data: std::marker::PhantomData<T>,
}

impl<ITEM> SkipOption for NoSkip<ITEM> {
type Item = ITEM;
fn should_skip(_: usize, _: &Self::Item) -> bool {
false
}
}
```

---

## `into_iter()` and `iter()`
Expand All @@ -432,7 +548,7 @@ Feel free to explore the capabilities of the Iterator you’ve been given!
### `into_iter()`

```rust
fn example_18() -> anyhow::Result<()> {
fn example_20() -> anyhow::Result<()> {
let ne_vec = NonEmptyVec::new(vec![1, 2, 3])?;
let ne_vec: NonEmptyVec<i32> = ne_vec.into_iter().map(|n| n * 2).map(|n| n * 3).collect();
assert_eq!(ne_vec.into_value(), vec![6, 12, 18]);
Expand All @@ -443,7 +559,7 @@ fn example_18() -> anyhow::Result<()> {
### `iter()`

```rust
fn example_19() -> anyhow::Result<()> {
fn example_21() -> anyhow::Result<()> {
let ne_vec = NonEmptyVec::new(vec![1, 2, 3])?;
let ne_vec: NonEmptyVec<i32> = ne_vec.iter().map(|n| n * 2).map(|n| n * 3).collect();
assert_eq!(ne_vec.into_value(), vec![6, 12, 18]);
Expand All @@ -454,7 +570,7 @@ fn example_19() -> anyhow::Result<()> {
### `NonEmptyVec` to `NonEmptyVecDeque` using `collect()`

```rust
fn example_20() -> anyhow::Result<()> {
fn example_22() -> anyhow::Result<()> {
let ne_vec = NonEmptyVec::new(vec![1, 2, 3])?;
let ne_vec_deque: NonEmptyVecDeque<i32> = ne_vec.into_iter().collect();
assert_eq!(ne_vec_deque.into_value(), vec![1, 2, 3]);
Expand All @@ -469,7 +585,7 @@ You can impose constraints on objects that have a length, such as `String` or `V
### String

```rust
fn example_21() -> Result<(), Error> {
fn example_23() -> Result<(), Error> {
length_greater_than!(5);
length_equal!(5, 10);
length_less_than!(10);
Expand Down Expand Up @@ -504,7 +620,7 @@ fn example_21() -> Result<(), Error> {

```rust
#[test]
fn example_22() -> anyhow::Result<()> {
fn example_24() -> anyhow::Result<()> {
length_greater_than!(5);
length_equal!(5, 10);
length_less_than!(10);
Expand Down Expand Up @@ -561,7 +677,7 @@ by `refined_type`, you can easily do so using `LengthDefinition`.

```rust
#[test]
fn example_23() -> anyhow::Result<()> {
fn example_25() -> anyhow::Result<()> {
length_equal!(5);

#[derive(Debug, PartialEq)]
Expand Down
45 changes: 32 additions & 13 deletions src/rule/collection/reverse.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
mod collection;
mod string;

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

/// A type that holds a value satisfying the `ReverseRule`
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>>;

/// Rule where the data in the collection satisfies the condition after reversing
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>;
#[cfg(test)]
mod tests {
use crate::result::Error;
use crate::rule::{Index0VecRule, NonEmptyStringRule, Reverse};

#[test]
fn test_reverse_valid() -> Result<(), Error<Vec<String>>> {
let table = vec![
vec!["hey".to_string(), "hello".to_string()],
vec!["hello".to_string()],
];

for input in table {
let refined = Reverse::<Index0VecRule<NonEmptyStringRule>, _>::new(input.clone())?;
assert_eq!(refined.into_value(), input);
}

Ok(())
}

#[test]
fn test_reverse_invalid() {
let table = vec![vec!["".to_string()], vec![]];

for input in table {
let refined = Reverse::<Index0VecRule<NonEmptyStringRule>, _>::new(input.clone());
assert!(refined.is_err());
}
}
}
Loading

0 comments on commit 150602f

Please sign in to comment.