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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#![doc(issue_tracker_base_url = "https://github.com/MidasLamb/non-empty-string/issues/")]
#[cfg(doctest)]
mod test_readme {
#[doc = include_str!("../README.md")]
mod something {}
}
use delegate::delegate;
#[cfg(feature = "serde")]
mod serde_support;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct NonEmptyString(String);
#[allow(clippy::len_without_is_empty)] impl NonEmptyString {
pub fn new(string: String) -> Result<NonEmptyString, String> {
if string.is_empty() {
Err(string)
} else {
Ok(NonEmptyString(string))
}
}
pub fn get(&self) -> &str {
&self.0
}
pub fn into_inner(self) -> String {
self.0
}
delegate! {
to self.0 {
pub fn into_bytes(self) -> Vec<u8>;
pub fn as_str(&self) -> &str;
pub fn push_str(&mut self, string: &str);
pub fn capacity(&self) -> usize;
pub fn reserve(&mut self, additional: usize);
pub fn reserve_exact(&mut self, additional: usize);
pub fn try_reserve_exact(
&mut self,
additional: usize
) -> Result<(), std::collections::TryReserveError>;
pub fn shrink_to_fit(&mut self);
pub fn shrink_to(&mut self, min_capacity: usize);
pub fn push(&mut self, ch: char);
pub fn as_bytes(&self) -> &[u8];
pub fn insert(&mut self, idx: usize, ch: char);
pub fn insert_str(&mut self, idx: usize, string: &str);
pub fn len(&self) -> usize;
pub fn into_boxed_str(self) -> Box<str>;
}
}
}
impl std::convert::AsRef<str> for NonEmptyString {
fn as_ref(&self) -> &str {
&self.0
}
}
impl std::convert::AsRef<String> for NonEmptyString {
fn as_ref(&self) -> &String {
&self.0
}
}
impl<'s> std::convert::TryFrom<&'s str> for NonEmptyString {
type Error = ();
fn try_from(value: &'s str) -> Result<Self, Self::Error> {
if value.is_empty() {
Err(())
} else {
Ok(NonEmptyString::new(value.to_owned()).expect("Value is not empty"))
}
}
}
impl std::convert::TryFrom<String> for NonEmptyString {
type Error = String;
fn try_from(value: String) -> Result<Self, Self::Error> {
NonEmptyString::new(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
use assert_matches::assert_matches;
#[test]
fn empty_string_returns_none() {
assert_eq!(NonEmptyString::new("".to_owned()), Err("".to_owned()));
}
#[test]
fn non_empty_string_returns_some() {
assert_matches!(NonEmptyString::new("string".to_owned()), Ok(_));
}
#[test]
fn what_goes_in_comes_out() {
assert_eq!(
NonEmptyString::new("string".to_owned())
.unwrap()
.into_inner(),
"string".to_owned()
);
}
#[test]
fn as_ref_str_works() {
let nes = NonEmptyString::new("string".to_owned()).unwrap();
let val: &str = nes.as_ref();
assert_eq!(val, "string");
}
#[test]
fn as_ref_string_works() {
let nes = NonEmptyString::new("string".to_owned()).unwrap();
let val: &String = nes.as_ref();
assert_eq!(val, "string");
}
#[test]
fn calling_string_methods_works() {
let nes = NonEmptyString::new("string".to_owned()).unwrap();
assert!(nes.len() > 0);
}
}