33 releases
new 0.5.16 | Nov 6, 2024 |
---|---|
0.5.14 | Oct 30, 2024 |
0.5.4 | May 29, 2024 |
0.4.14 | Mar 31, 2024 |
0.2.4 |
|
#189 in Rust patterns
797 downloads per month
140KB
3K
SLoC
Refined Type
refined_type is a library developed for Rust. It enhances your types, making them more robust and expanding the range of guarantees your applications can statically ensure.
You can create various rules for a certain type, such as phone numbers, addresses, times, and so on. Once you have established the rules, you can easily combine them. Specifically, if you create rules for 'non-empty strings' and 'strings composed only of alphabets,' you do not need to redefine a new rule for 'non-empty strings composed only of alphabets'. All rules can be arbitrarily combined and extended as long as the target type matches. Enjoy a wonderful type life!
Installation
cargo add refined_type
Get Started
As an example, let's convert from JSON to a struct.
// define a struct for converting from JSON.
#[derive(Debug, Deserialize)]
struct Human {
name: NonEmptyString,
age: MinMaxU8<18, 80>,
friends: NonEmptyVec<String>,
}
// In the 1st example, all fields satisfy the rule, causing the conversion from JSON to succeed.
fn get_started_simple_example() -> anyhow::Result<()> {
let json = json! {{
"name": "john",
"age": 20,
"friends": ["tom", "taro"]
}}
.to_string();
let human = serde_json::from_str::<Human>(&json)?;
assert_eq!(human.name.into_value(), "john");
assert_eq!(human.age.into_value(), 20);
assert_eq!(human.friends.into_value(), vec!["tom", "taro"]);
Ok(())
}
// In the 2nd example, while `name` does not satisfy the rule, `age` and `friends` do, causing the conversion from JSON to fail.
fn get_started_empty_name_example() -> anyhow::Result<()> {
let json = json! {{
"name": "",
"age": 20,
"friends": ["tom", "taro"]
}}
.to_string();
// because `name` is empty
assert!(serde_json::from_str::<Human>(&json).is_err());
Ok(())
}
// In the 3rd example, while `age` does not satisfy the rule, `name` and `friends` do, causing the conversion from JSON to fail.
fn get_started_outbound_age_example() -> anyhow::Result<()> {
let json = json! {{
"name": "john",
"age": 100,
"friends": ["tom", "taro"]
}}
.to_string();
// because `age` is not in the range of 18 to 80
assert!(serde_json::from_str::<Human>(&json).is_err());
Ok(())
}
// In the 4th example, while `friends` does not satisfy the rule, `name` and `age` do, causing the conversion from JSON to fail.
fn get_started_empty_vec_example() -> anyhow::Result<()> {
let json = json! {{
"name": "john",
"age": 20,
"friends": []
}}
.to_string();
// because `friends` is empty
assert!(serde_json::from_str::<Human>(&json).is_err());
Ok(())
}
Compose Rules
As mentioned earlier, it is possible to combine any rules as long as the target types match.
In the example below, there are standalone rules for 'strings containing Hello' and 'strings containing World'.
Since their target type is String, combining them is possible.
I have prepared something called Rule Composer (And
, Or
, Not
).
By using Rule Composer, composite rules can be easily created.
1: And
Rule Composer
And
Rule Composer is a rule that satisfies both of the two rules.
It is generally effective when you want to narrow down the condition range.
type Target = Refined<And![EvenRuleU8, MinMaxRuleU8<0, 100>]>;
fn and_example() -> Result<(), Error<u8>> {
let target = Target::new(50)?;
assert_eq!(target.into_value(), 50);
let target = Target::new(51);
assert!(target.is_err());
Ok(())
}
2: Or
Rule Composer
Or
Rule Composer is a rule that satisfies either of the two rules.
It is generally effective when you want to expand the condition range.
type Target = Refined<Or![LessRuleU8<10>, GreaterRuleU8<50>]>;
fn or_example() -> Result<(), Error<u8>> {
let target = Target::new(5)?;
assert_eq!(target.into_value(), 5);
let target = Target::new(10);
assert!(target.is_err());
let target = Target::new(50);
assert!(target.is_err());
let target = Target::new(51)?;
assert_eq!(target.into_value(), 51);
Ok(())
}
3: Not
Rule Composer
Not
Rule Composer is a rule that does not satisfy a specific condition.
It is generally effective when you want to discard only certain situations.
type Target = Refined<Not<EqualRuleU8<50>>>;
fn not_example() -> Result<(), Error<u8>> {
let target = Target::new(49)?;
assert_eq!(target.into_value(), 49);
let target = Target::new(50);
assert!(target.is_err());
let target = Target::new(51)?;
assert_eq!(target.into_value(), 51);
Ok(())
}
Number
MinMax
MinMax
is a type that signifies the target exists between a certain number and another number.
type Age = MinMaxU8<18, 80>;
fn min_max_example() -> Result<(), Error<u8>> {
let age = Age::new(18)?;
assert_eq!(age.into_value(), 18);
let age = Age::new(80)?;
assert_eq!(age.into_value(), 80);
let age = Age::new(17);
assert!(age.is_err());
let age = Age::new(81);
assert!(age.is_err());
Ok(())
}
Less
Less
is a type that signifies the target is less than a certain number.
type Age = LessU8<80>;
fn less_example() -> Result<(), Error<u8>> {
let age = Age::new(79)?;
assert_eq!(age.into_value(), 79);
let age = Age::new(80);
assert!(age.is_err());
Ok(())
}
Greater
Greater
is a type that signifies the target is greater than a certain number.
type Age = GreaterU8<18>;
fn greater_example() -> Result<(), Error<u8>> {
let age = Age::new(19)?;
assert_eq!(age.into_value(), 19);
let age = Age::new(18);
assert!(age.is_err());
Ok(())
}
Equal
Equal
is a type that signifies the target is equal to a certain number.
type Age = EqualU8<18>;
fn equal_example() -> Result<(), Error<u8>> {
let age = Age::new(18)?;
assert_eq!(age.into_value(), 18);
let age = Age::new(19);
assert!(age.is_err());
Ok(())
}
LessEqual
LessEqual
is a type that signifies the target is less than or equal to a certain number.
type Age = LessEqualU8<80>;
fn less_equal_example() -> Result<(), Error<u8>> {
let age = Age::new(79)?;
assert_eq!(age.into_value(), 79);
let age = Age::new(80)?;
assert_eq!(age.into_value(), 80);
let age = Age::new(81);
assert!(age.is_err());
Ok(())
}
GreaterEqual
GreaterEqual
is a type that signifies the target is greater than or equal to a certain number.
type Age = GreaterEqualU8<18>;
fn greater_equal_example() -> Result<(), Error<u8>> {
let age = Age::new(19)?;
assert_eq!(age.into_value(), 19);
let age = Age::new(18)?;
assert_eq!(age.into_value(), 18);
let age = Age::new(17);
assert!(age.is_err());
Ok(())
}
Range
Range
is a type that signifies the target exists between a certain number and another number.
type Age = RangeU8<18, 80>;
fn range_example() -> Result<(), Error<u8>> {
let age = Age::new(17);
assert!(age.is_err());
let age = Age::new(18)?;
assert_eq!(age.into_value(), 18);
let age = Age::new(79)?;
assert_eq!(age.into_value(), 79);
let age = Age::new(80);
assert!(age.is_err());
Ok(())
}
Iterator
refined_type
has several useful refined types for Iterators.
ForAll
ForAll
is a rule that applies a specific rule to all elements in the Iterator.
fn example_11() -> anyhow::Result<()> {
let vec = vec!["Hello".to_string(), "World".to_string()];
let for_all_ok = ForAllVec::<NonEmptyStringRule>::new(vec.clone())?;
assert_eq!(vec, for_all_ok.into_value());
let vec = vec!["Hello".to_string(), "".to_string()];
let for_all_err = ForAllVec::<NonEmptyStringRule>::new(vec.clone());
assert!(for_all_err.is_err());
Ok(())
}
Exists
Exists
is a rule that applies a specific rule to at least one element in the Iterator.
fn example_12() -> anyhow::Result<()> {
let vec = vec!["Hello".to_string(), "".to_string()];
let exists_ok = ExistsVec::<NonEmptyStringRule>::new(vec.clone())?;
assert_eq!(vec, exists_ok.into_value());
let vec = vec!["".to_string(), "".to_string()];
let exists_err = ExistsVec::<NonEmptyStringRule>::new(vec.clone());
assert!(exists_err.is_err());
Ok(())
}
Head
Head
is a rule that applies a specific rule to the first element in the Iterator.
fn example_13() -> anyhow::Result<()> {
let table = vec![
(vec!["good morning".to_string(), "".to_string()], true), // PASS
(vec!["hello".to_string(), "hello".to_string()], true), // PASS
(vec![], false), // FAIL
(vec!["".to_string()], false), // FAIL
(vec!["".to_string(), "hello".to_string()], false), // FAIL
];
for (value, ok) in table {
let head = HeadVec::<NonEmptyStringRule>::new(value.clone());
assert_eq!(head.is_ok(), ok);
}
Ok(())
}
Last
Last
is a rule that applies a specific rule to the last element in the Iterator.
fn example_14() -> anyhow::Result<()> {
let table = vec![
(vec!["".to_string(), "hello".to_string()], true), // PASS
(vec!["good morning".to_string(), "hello".to_string()], true), // PASS
(vec![], false), // FAIL
(vec!["".to_string()], false), // FAIL
(vec!["hello".to_string(), "".to_string()], false), // FAIL
];
for (value, ok) in table {
let last = LastVec::<NonEmptyStringRule>::new(value.clone());
assert_eq!(last.is_ok(), ok);
}
Ok(())
}
Tail
Tail
is a rule that applies a specific rule to all elements except the first element in the Iterator.
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(())
}
Init
Init
is a rule that applies a specific rule to all elements except the last element in the Iterator.
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(())
}
Index
Index
is a rule that applies a specific rule to the element at a specific index in the Iterator.
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 = IndexVec::<1, 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.
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::<IndexRuleVec<0, NonEmptyStringRule>>::new(value.clone());
assert_eq!(refined.is_ok(), expected);
}
Ok(())
}
Skip
Skip
is a rule that applies a specific rule to the elements of the Iterator while skipping the elements according
to SkipOption
.
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.
pub struct NoSkip<T> {
_phantom_data: std::marker::PhantomData<T>,
}
impl<ITEM> SkipOption for NoSkip<ITEM> {
type Item = ITEM;
type Accumulator = ();
fn should_skip(_: usize, _: Option<&mut Self::Accumulator>, _: &Self::Item) -> bool {
false
}
}
Length
You can impose constraints on objects that have a length, such as String
or Vec
.
LengthMinMax
LengthMinMax
is a type that signifies the target has a length between a certain number and another number.
fn length_min_max_example() -> Result<(), Error<String>> {
type Password = LengthMinMax<5, 10, String>;
let password = Password::new("123456".to_string())?;
assert_eq!(password.into_value(), "123456");
let password = Password::new("1234".to_string());
assert!(password.is_err());
let password = Password::new("12345678901".to_string());
assert!(password.is_err());
Ok(())
}
LengthGreater
LengthGreater
is a type that signifies the target has a length greater than a certain number.
fn length_greater_example() -> Result<(), Error<String>> {
type Password = LengthGreater<5, String>;
let password = Password::new("123456".to_string())?;
assert_eq!(password.into_value(), "123456");
let password = Password::new("1234".to_string());
assert!(password.is_err());
Ok(())
}
LengthLess
LengthLess
is a type that signifies the target has a length less than a certain number.
fn length_less_example() -> Result<(), Error<String>> {
type Password = LengthLess<10, String>;
let password = Password::new("123456".to_string())?;
assert_eq!(password.into_value(), "123456");
let password = Password::new("12345678901".to_string());
assert!(password.is_err());
Ok(())
}
LengthEqual
LengthEqual
is a type that signifies the target has a length equal to a certain number.
fn length_equal_example() -> Result<(), Error<String>> {
type Password = LengthEqual<5, String>;
let password = Password::new("12345".to_string())?;
assert_eq!(password.into_value(), "12345");
let password = Password::new("1234".to_string());
assert!(password.is_err());
Ok(())
}
Custom Length
You can define a length for any type. Therefore, if you want to implement a length that is not provided
by refined_type
, you can easily do so using LengthDefinition
.
#[derive(Debug, PartialEq)]
struct Hello;
impl LengthDefinition for Hello {
fn length(&self) -> usize {
5
}
}
fn custom_length_example() -> Result<(), Error<Hello>> {
let hello = Refined::<LengthEqualRule<5, Hello>>::new(Hello)?;
assert_eq!(hello.into_value(), Hello);
Ok(())
}
JSON
refined_type
is compatible with serde_json
. This ensures type-safe communication and eliminates the need to write
new validation processes. All you need to do is implement a set of rules once and implement serde
’s Serialize
and Deserialize
.
Serialize
#[derive(Debug, Eq, PartialEq, Deserialize, Serialize)]
struct Human2 {
name: NonEmptyString,
age: u8,
}
fn example_9() -> anyhow::Result<()> {
let john = Human2 {
name: NonEmptyString::new("john".to_string())?,
age: 8,
};
let actual = json!(john);
let expected = json! {{
"name": "john",
"age": 8
}};
assert_eq!(actual, expected);
Ok(())
}
Deserialize
fn example_10() -> anyhow::Result<()> {
let json = json! {{
"name": "john",
"age": 8
}}
.to_string();
let actual = serde_json::from_str::<Human2>(&json)?;
let expected = Human2 {
name: NonEmptyString::new("john".to_string())?,
age: 8,
};
assert_eq!(actual, expected);
Ok(())
}
Dependencies
~2.5–4MB
~76K SLoC