Skip to content

Commit

Permalink
Merge pull request #35 from tomoikey/v0.5.20
Browse files Browse the repository at this point in the history
Release v.0.5.20
  • Loading branch information
tomoikey authored Nov 10, 2024
2 parents 93bd8d2 + 72a57c4 commit d83572f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
19 changes: 12 additions & 7 deletions examples/1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use refined_type::rule::composer::{If, IfElse};
use refined_type::rule::composer::{If, IfElse, Not};
use refined_type::rule::{
EmailStringRule, EvenRuleU8, ExistsVecRule, ForAllVecRule, GreaterRuleU8, HeadVecRule,
NonEmptyString, NonEmptyStringRule, NonEmptyVecRule,
Ipv4AddrRule, LastVecRule, NonEmptyString, NonEmptyStringRule,
};
use refined_type::{And, Refined};
use serde::Deserialize;
Expand All @@ -24,9 +24,12 @@ pub struct Data {
age: Refined<If<GreaterRuleU8<10>, EvenRuleU8>>,
friends: Refined<
IfElse<
And![ForAllVecRule<NonEmptyStringRule>, NonEmptyVecRule<String>],
And![
ForAllVecRule<NonEmptyStringRule>,
ExistsVecRule<Ipv4AddrRule<String>>
],
HeadVecRule<EmailStringRule>,
ExistsVecRule<EmailStringRule>,
LastVecRule<Not<EmailStringRule>>,
>,
>,
}
Expand All @@ -36,10 +39,12 @@ fn main() {
{
"name": "John Doe",
"age": 20,
"friends": ["alice@example.com", "Bob"]
"friends": ["alice@example.com", "192.168.11.1"]
}
"#;

let data: Data = serde_json::from_str(data).unwrap();
println!("{}", data); // name: John Doe, age: 20, friends: Refined { value: ["alice@example.com", "Bob"] }
let data = serde_json::from_str::<Data>(data).map(|n| n.to_string());

// Ok("name: John Doe, age: 20, friends: Refined { value: [\"alice@example.com\", \"192.168.11.1\"] }")
println!("{:?}", data);
}
1 change: 1 addition & 0 deletions src/rule/string/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::str::FromStr;
pub type Ipv4Addr<STRING> = Refined<Ipv4AddrRule<STRING>>;

/// Rule where the target value must be a valid IPv4 address
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Ipv4AddrRule<T> {
_phantom: std::marker::PhantomData<T>,
}
Expand Down
1 change: 1 addition & 0 deletions src/rule/string/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::str::FromStr;
pub type Ipv6Addr<STRING> = Refined<Ipv6AddrRule<STRING>>;

/// Rule where the target value must be a valid IPv6 address
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Ipv6AddrRule<T> {
_phantom: std::marker::PhantomData<T>,
}
Expand Down

0 comments on commit d83572f

Please sign in to comment.