Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0.5.19 #34

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": ["[email protected]", "Bob"]
}
"#;

let data: Data = serde_json::from_str(data).unwrap();
println!("{}", data); // name: John Doe, age: 20, friends: Refined { value: ["[email protected]", "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
Loading