1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::fmt::{Display, Formatter};
use std::ops::Deref;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use crate::error::StringValueError;
pub mod error;
#[cfg(feature = "utils")]
pub mod utils;
pub type RequestId = NonBlankString;
#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
#[serde(try_from = "String", into = "String")]
pub struct NonBlankString(String);
impl FromStr for NonBlankString {
type Err = StringValueError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
if value.len() > 0 {
Ok(Self(value.to_string()))
} else {
Err(StringValueError::ParseError)
}
}
}
impl AsRef<str> for NonBlankString {
fn as_ref(&self) -> &str {
&self.0
}
}
impl TryFrom<String> for NonBlankString {
type Error = StringValueError;
fn try_from(value: String) -> Result<Self, Self::Error> {
NonBlankString::from_str(&value)
}
}
impl From<NonBlankString> for String {
fn from(value: NonBlankString) -> Self {
value.0
}
}
impl Deref for NonBlankString {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Display for NonBlankString {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use fake::{Fake, Faker};
use serde::{Deserialize, Serialize};
use crate::NonBlankString;
#[test]
fn return_string_value_for_non_blank_string() {
let value = get_random_string();
assert_eq!(value, NonBlankString::from_str(&value).unwrap().as_ref());
}
#[test]
fn return_error_for_blank_string() {
assert!(NonBlankString::from_str("").is_err());
let value = "".parse::<NonBlankString>();
assert!(value.is_err())
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Demo {
pub login: NonBlankString
}
#[test]
fn serialization_deserialization_test() {
let value = get_random_string();
let entity = Demo {
login: NonBlankString::from_str(&value).unwrap()
};
let json = serde_json::to_string(&entity).unwrap();
let expected_json = format!("{{\"login\":\"{}\"}}", value);
assert_eq!(json, expected_json);
let result_entity = serde_json::from_str::<Demo>(&expected_json).unwrap();
assert_eq!(result_entity, entity);
}
#[test]
fn serialization_deserialization_test_for_string_and_blank_value() {
let json = "{\"login\":\"\"}".to_string();
match serde_json::from_str::<Demo>(&json) {
Ok(_) => panic!("error expected"),
Err(e) => println!("{}", e)
}
}
#[test]
fn use_as_str_test() {
let non_blank_string = NonBlankString::from_str(&get_random_string()).unwrap();
assert_str_func(&non_blank_string);
}
fn assert_str_func(_: &str) {
assert!(true)
}
fn get_random_string() -> String {
Faker.fake::<String>()
}
}