Skip to content

Commit 63681ff

Browse files
committed
update readme
1 parent 568d845 commit 63681ff

File tree

2 files changed

+103
-10
lines changed

2 files changed

+103
-10
lines changed

README.md

+57-6
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,66 @@ All rules can be arbitrarily combined and extended as long as the target type ma
1313

1414
# Example Usage
1515

16+
As an example, let's convert from JSON to a struct.
17+
1618
```rust
17-
type NonEmptyString = Refined<NonEmptyStringRule>;
19+
use refined_type::Refined;
20+
use refined_type::rule::NonEmptyRule;
21+
use serde::Deserialize;
22+
use serde_json::json;
1823

19-
fn main() {
20-
let hello_world = NonEmptyString::new("hello world".to_string());
21-
assert_eq!(hello_world.unwrap().into_value(), "hello world");
24+
// define the constraints you expect by combining 'Refined' and 'Rule'.
25+
pub type MyNonEmptyString = Refined<NonEmptyRule<String>>;
26+
pub type MyNonEmptyVec<T> = Refined<NonEmptyRule<Vec<T>>>;
27+
28+
// define a struct for converting from JSON.
29+
#[derive(Debug, Eq, PartialEq, Deserialize)]
30+
struct Human {
31+
name: MyNonEmptyString,
32+
friends: MyNonEmptyVec<String>
33+
}
34+
35+
// In the first example, both 'name' and 'friends' satisfy the rule, so the conversion from JSON will succeed.
36+
fn example_1() -> anyhow::Result<()> {
37+
let json = json! {{
38+
"name": "john",
39+
"friends": ["tom", "taro"]
40+
}}
41+
.to_string();
42+
43+
let actual = serde_json::from_str::<Human>(&json)?;
44+
let expected = Human {
45+
name: MyNonEmptyString::new("john".to_string())?,
46+
friends: MyNonEmptyVec::new(vec!["tom".to_string(), "taro".to_string()])?
47+
};
48+
assert_eq!(actual, expected);
49+
Ok(())
50+
}
51+
52+
// In the second example, while `friends` meets the rule, `name` does not, causing the conversion from JSON to fail
53+
fn example_2() -> anyhow::Result<()> {
54+
let json = json! {{
55+
"name": "",
56+
"friends": ["tom", "taro"]
57+
}}
58+
.to_string();
59+
60+
// because `name` is empty
61+
assert!(serde_json::from_str::<Human>(&json).is_err());
62+
Ok(())
63+
}
64+
65+
// In the third example, while `name` satisfies the rule, `friends` does not, causing the conversion from JSON to fail.
66+
fn example_3() -> anyhow::Result<()> {
67+
let json = json! {{
68+
"name": "john",
69+
"friends": []
70+
}}
71+
.to_string();
2272

23-
let empty_string = NonEmptyString::new("".to_string());
24-
assert!(empty_string.is_err());
73+
// because `friends` is empty
74+
assert!(serde_json::from_str::<Human>(&json).is_err());
75+
Ok(())
2576
}
2677
```
2778

src/refined.rs

+46-4
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ where
9797
mod test {
9898
use crate::refined::Refined;
9999
use crate::result::Error;
100-
use crate::rule::NonEmptyStringRule;
100+
use crate::rule::{NonEmptyString, NonEmptyStringRule, NonEmptyVec};
101101
use serde::{Deserialize, Serialize};
102102
use serde_json::json;
103103

@@ -180,15 +180,16 @@ mod test {
180180
}
181181

182182
#[test]
183-
fn test_refined_deserialize_json_ok_struct() -> anyhow::Result<()> {
184-
type NonEmptyString = Refined<NonEmptyStringRule>;
183+
fn test_refined_deserialize_json_ok_1() -> anyhow::Result<()> {
185184
#[derive(Debug, Eq, PartialEq, Deserialize)]
186185
struct Human {
187186
name: NonEmptyString,
187+
friends: NonEmptyVec<String>,
188188
age: u8,
189189
}
190190
let json = json! {{
191191
"name": "john",
192+
"friends": ["tom", "taro"],
192193
"age": 8
193194
}}
194195
.to_string();
@@ -197,14 +198,55 @@ mod test {
197198

198199
let expected = Human {
199200
name: NonEmptyString::new("john".to_string())?,
201+
friends: NonEmptyVec::new(vec!["tom".to_string(), "taro".to_string()])?,
200202
age: 8,
201203
};
202204
assert_eq!(actual, expected);
203205
Ok(())
204206
}
205207

206208
#[test]
207-
fn test_refined_deserialize_json_err() -> anyhow::Result<()> {
209+
fn test_refined_deserialize_json_err_1() -> anyhow::Result<()> {
210+
#[derive(Debug, Eq, PartialEq, Deserialize)]
211+
struct Human {
212+
name: NonEmptyString,
213+
friends: NonEmptyVec<String>,
214+
age: u8,
215+
}
216+
let json = json! {{
217+
"name": "john",
218+
"friends": [],
219+
"age": 8
220+
}}
221+
.to_string();
222+
223+
// because `friends` is empty vec
224+
assert!(serde_json::from_str::<Human>(&json).is_err());
225+
Ok(())
226+
}
227+
228+
#[test]
229+
fn test_refined_deserialize_json_err_2() -> anyhow::Result<()> {
230+
#[derive(Debug, Eq, PartialEq, Deserialize)]
231+
struct Human {
232+
name: NonEmptyString,
233+
friends: NonEmptyVec<String>,
234+
age: u8,
235+
}
236+
let json = json! {{
237+
"name": "",
238+
"friends": ["tom", "taro"],
239+
"age": 8
240+
}}
241+
.to_string();
242+
243+
// because `name` is empty string
244+
assert!(serde_json::from_str::<Human>(&json).is_err());
245+
Ok(())
246+
}
247+
248+
#[test]
249+
fn test_refined_deserialize_json_err_3() -> anyhow::Result<()> {
208250
let json = json!("").to_string();
209251
let result = serde_json::from_str::<Refined<NonEmptyStringRule>>(&json);
210252
assert!(result.is_err());

0 commit comments

Comments
 (0)