os_query_builder_rs/term/
wildcard.rs

1use serde::{
2    Serialize,
3    Serializer,
4    ser::SerializeMap
5};
6use crate::misc::rewrite::Rewrite;
7
8#[derive(Debug, Default, Clone)]
9pub struct Wildcard {
10    field: Option<String>,
11    value: WildcardValue
12}
13
14#[derive(Debug, Default, Clone, Serialize)]
15struct WildcardValue {
16    value: String,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    boost: Option<f64>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    case_insensitive: Option<bool>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    rewrite: Option<Rewrite>
23}
24
25impl Serialize for Wildcard {
26    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
27        where
28            S: Serializer,
29    {
30        let mut state = serializer.serialize_map(Some(1))?;
31        state.serialize_entry(&self.field.as_deref().unwrap_or_default(), &self.value)?;
32        state.end()
33    }
34}
35
36impl Wildcard {
37    pub fn new() -> Self {
38        Self::default()
39    }
40
41    pub fn field<T:Into<String> + Serialize>(self, field: T) -> Self {
42        Self {
43            field: Some(field.into()),
44            ..self
45        }
46    }
47
48    pub fn value<T:Into<String> + Serialize>(self, value: T) -> Self {
49        Self {
50            value: WildcardValue {
51                value: value.into(),
52                ..self.value
53            },
54            ..self
55        }
56    }
57
58    pub fn boost<T:Into<f64> + Serialize>(self, boost: T) -> Self {
59        Self {
60            value: WildcardValue {
61                boost: Some(boost.into()),
62                ..self.value
63            },
64            ..self
65        }
66    }
67
68    pub fn case_insensitive<T:Into<bool> + Serialize>(self, case_insensitive: T) -> Self {
69        Self {
70            value: WildcardValue {
71                case_insensitive: Some(case_insensitive.into()),
72                ..self.value
73            },
74            ..self
75        }
76    }
77
78
79    pub fn rewrite<T:Into<Rewrite> + Serialize>(self, rewrite: T) -> Self {
80        Self {
81            value: WildcardValue {
82                rewrite: Some(rewrite.into()),
83                ..self.value
84            },
85            ..self
86        }
87    }
88}