elasticsearch_dsl/search/params/
text.rs

1use crate::util::*;
2use std::borrow::Cow;
3
4/// Search text
5#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
6pub struct Text(Option<String>);
7
8impl std::fmt::Debug for Text {
9    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
10        self.0.fmt(f)
11    }
12}
13
14impl ShouldSkip for Text {
15    fn should_skip(&self) -> bool {
16        self.0.as_ref().is_none_or(ShouldSkip::should_skip)
17    }
18}
19
20impl From<String> for Text {
21    fn from(value: String) -> Self {
22        Self(Some(value))
23    }
24}
25
26impl From<Option<String>> for Text {
27    fn from(value: Option<String>) -> Self {
28        Self(value)
29    }
30}
31
32impl From<&str> for Text {
33    fn from(value: &str) -> Self {
34        Self(Some(value.into()))
35    }
36}
37
38impl From<Option<&str>> for Text {
39    fn from(value: Option<&str>) -> Self {
40        Self(value.map(Into::into))
41    }
42}
43
44impl<'a> From<Cow<'a, str>> for Text {
45    fn from(value: Cow<'a, str>) -> Self {
46        Self(Some(value.into()))
47    }
48}
49
50impl<'a> From<Option<Cow<'a, str>>> for Text {
51    fn from(value: Option<Cow<'a, str>>) -> Self {
52        Self(value.map(Into::into))
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn skips_correctly() {
62        assert!(Text::from(None::<String>).should_skip());
63        assert!(Text::from("").should_skip());
64        assert!(Text::from("  ").should_skip());
65    }
66
67    #[test]
68    fn compares_correctly() {
69        assert_eq!(Text::from("abc"), Text::from(Some("abc")));
70    }
71}