Skip to content

Commit

Permalink
Merge pull request #34 from tomoikey/v0.5.19
Browse files Browse the repository at this point in the history
V0.5.19
  • Loading branch information
tomoikey authored Nov 10, 2024
2 parents ef1fe16 + a6bb3ed commit 93bd8d2
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repository = "https://github.com/tomoikey/refined_type"
readme = "README.md"
categories = ["accessibility", "development-tools", "rust-patterns"]
license = "MIT"
version = "0.5.18"
version = "0.5.19"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
45 changes: 45 additions & 0 deletions examples/1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use refined_type::rule::composer::{If, IfElse};
use refined_type::rule::{
EmailStringRule, EvenRuleU8, ExistsVecRule, ForAllVecRule, GreaterRuleU8, HeadVecRule,
NonEmptyString, NonEmptyStringRule, NonEmptyVecRule,
};
use refined_type::{And, Refined};
use serde::Deserialize;
use std::fmt::Display;

impl Display for Data {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"name: {}, age: {}, friends: {:?}",
self.name, self.age, self.friends
)
}
}

#[allow(clippy::type_complexity)]
#[derive(Debug, Deserialize)]
pub struct Data {
name: NonEmptyString,
age: Refined<If<GreaterRuleU8<10>, EvenRuleU8>>,
friends: Refined<
IfElse<
And![ForAllVecRule<NonEmptyStringRule>, NonEmptyVecRule<String>],
HeadVecRule<EmailStringRule>,
ExistsVecRule<EmailStringRule>,
>,
>,
}

fn main() {
let data = r#"
{
"name": "John Doe",
"age": 20,
"friends": ["alice@example.com", "Bob"]
}
"#;

let data: Data = serde_json::from_str(data).unwrap();
println!("{}", data); // name: John Doe, age: 20, friends: Refined { value: ["alice@example.com", "Bob"] }
}
1 change: 1 addition & 0 deletions src/rule/collection/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::VecDeque;
pub type Index<const INDEX: usize, RULE, ITERABLE> = Refined<IndexRule<INDEX, RULE, ITERABLE>>;
pub type IndexVec<const INDEX: usize, RULE> = Refined<IndexRuleVec<INDEX, RULE>>;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IndexRule<const INDEX: usize, RULE, ITERABLE>
where
RULE: Rule,
Expand Down
1 change: 1 addition & 0 deletions src/rule/collection/reverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::marker::PhantomData;
pub type Reverse<RULE> = Refined<ReverseRule<RULE>>;

/// Rule where the data in the collection satisfies the condition after reversing
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ReverseRule<RULE>
where
RULE: Rule,
Expand Down
1 change: 1 addition & 0 deletions src/rule/collection/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub type SkipVecDeque<RULE, OPTION> = Refined<SkipVecDequeRule<RULE, OPTION>>;
pub type SkipString<RULE, OPTION> = Refined<SkipStringRule<RULE, OPTION>>;

/// Rule where the data in the collection satisfies the condition after skipping the first element
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SkipRule<RULE, ITERABLE, OPTION>
where
RULE: Rule,
Expand Down
1 change: 1 addition & 0 deletions src/rule/collection/skip/option/no_skip.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::rule::SkipOption;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NoSkip<T> {
_phantom_data: std::marker::PhantomData<T>,
}
Expand Down
1 change: 1 addition & 0 deletions src/rule/collection/skip/option/skip_even_index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::rule::SkipOption;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SkipEvenIndex<ITEM> {
_phantom_data: std::marker::PhantomData<ITEM>,
}
Expand Down
1 change: 1 addition & 0 deletions src/rule/collection/skip/option/skip_first.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::rule::SkipOption;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SkipFirst<ITEM> {
_phantom_data: std::marker::PhantomData<ITEM>,
}
Expand Down
1 change: 1 addition & 0 deletions src/rule/collection/skip/option/skip_odd_index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::rule::SkipOption;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SkipOddIndex<ITEM> {
_phantom_data: std::marker::PhantomData<ITEM>,
}
Expand Down
1 change: 1 addition & 0 deletions src/rule/string/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub use regex::Regex;
macro_rules! declare_regex_rule {
($vis:vis $rule:ident, $regex:literal) => {
$crate::paste::item! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
$vis struct $rule<STRING> {
_phantom: std::marker::PhantomData<STRING>,
}
Expand Down

0 comments on commit 93bd8d2

Please sign in to comment.