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
use crate::util::*;
use std::borrow::Cow;
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
pub struct Text(Option<String>);
impl std::fmt::Debug for Text {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl ShouldSkip for Text {
fn should_skip(&self) -> bool {
self.0.as_ref().map_or(true, ShouldSkip::should_skip)
}
}
impl From<String> for Text {
fn from(value: String) -> Self {
Self(Some(value))
}
}
impl From<Option<String>> for Text {
fn from(value: Option<String>) -> Self {
Self(value)
}
}
impl From<&str> for Text {
fn from(value: &str) -> Self {
Self(Some(value.into()))
}
}
impl From<Option<&str>> for Text {
fn from(value: Option<&str>) -> Self {
Self(value.map(Into::into))
}
}
impl<'a> From<Cow<'a, str>> for Text {
fn from(value: Cow<'a, str>) -> Self {
Self(Some(value.into()))
}
}
impl<'a> From<Option<Cow<'a, str>>> for Text {
fn from(value: Option<Cow<'a, str>>) -> Self {
Self(value.map(Into::into))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn skips_correctly() {
assert!(Text::from(None::<String>).should_skip());
assert!(Text::from("").should_skip());
assert!(Text::from(" ").should_skip());
}
#[test]
fn compares_correctly() {
assert_eq!(Text::from("abc"), Text::from(Some("abc")));
}
}