Skip to main content

serde_firestore_value/typ/
pipeline.rs

1#[cfg(feature = "btree-map")]
2use std::collections::BTreeMap;
3#[cfg(feature = "hash-map")]
4use std::collections::HashMap;
5
6use crate::google::firestore::v1::Value;
7
8/// Pipeline
9///
10/// `pipelineValue` inner type.
11///
12/// <https://firebase.google.com/docs/firestore/reference/rest/Shared.Types/ArrayValue#Value>
13#[derive(Clone, Debug, PartialEq)]
14pub struct Pipeline {
15    /// Required. Ordered list of stages to evaluate.
16    pub stages: Vec<Stage>,
17}
18
19impl Pipeline {
20    pub(crate) const FIELDS: &'static [&'static str] = &["stages"];
21    pub(crate) const NAME: &'static str = "$__serde-firestore-value_private_pipeline";
22}
23
24impl<'de> serde::Deserialize<'de> for Pipeline {
25    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
26    where
27        D: serde::Deserializer<'de>,
28    {
29        deserializer.deserialize_struct(Self::NAME, Self::FIELDS, PipelineVisitor)
30    }
31}
32
33impl serde::Serialize for Pipeline {
34    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
35    where
36        S: serde::Serializer,
37    {
38        use serde::ser::SerializeStruct;
39
40        let mut state = serializer.serialize_struct(Self::NAME, Self::FIELDS.len())?;
41        state.serialize_field("stages", &self.stages)?;
42        state.end()
43    }
44}
45
46/// Stage
47///
48/// A single operation within a pipeline.
49///
50/// <https://firebase.google.com/docs/firestore/reference/rest/Shared.Types/ArrayValue#Value>
51#[derive(Clone, Debug, PartialEq)]
52pub struct Stage {
53    /// Required. The name of the stage to evaluate.
54    pub name: String,
55    /// Optional. Ordered list of arguments the given stage expects.
56    pub args: Vec<Value>,
57    /// Optional. Optional named arguments that certain functions may support.
58    #[cfg(feature = "btree-map")]
59    pub options: BTreeMap<String, Value>,
60    /// Optional. Optional named arguments that certain functions may support.
61    #[cfg(feature = "hash-map")]
62    pub options: HashMap<String, Value>,
63}
64
65impl Stage {
66    pub(crate) const FIELDS: &'static [&'static str] = &["name", "args", "options"];
67    pub(crate) const NAME: &'static str = "$__serde-firestore-value_private_pipeline_stage";
68}
69
70impl serde::Serialize for Stage {
71    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
72    where
73        S: serde::Serializer,
74    {
75        use serde::ser::SerializeStruct;
76
77        let mut state = serializer.serialize_struct(Self::NAME, Self::FIELDS.len())?;
78        state.serialize_field("name", &self.name)?;
79        state.serialize_field(
80            "args",
81            &self
82                .args
83                .iter()
84                .map(crate::typ::private::ValueWrapper)
85                .collect::<Vec<_>>(),
86        )?;
87        state.serialize_field(
88            "options",
89            &self
90                .options
91                .iter()
92                .map(|(k, v)| (k.as_str(), crate::typ::private::ValueWrapper(v)))
93                .collect::<std::collections::HashMap<_, _>>(),
94        )?;
95        state.end()
96    }
97}
98
99impl<'de> serde::Deserialize<'de> for Stage {
100    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
101    where
102        D: serde::Deserializer<'de>,
103    {
104        deserializer.deserialize_struct(Self::NAME, Self::FIELDS, StageVisitor)
105    }
106}
107
108struct StageVisitor;
109
110impl<'de> serde::de::Visitor<'de> for StageVisitor {
111    type Value = Stage;
112
113    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
114        formatter.write_str("a Stage struct")
115    }
116
117    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
118    where
119        A: serde::de::MapAccess<'de>,
120    {
121        let mut name: Option<String> = None;
122        let mut args: Option<Vec<Value>> = None;
123        #[cfg(feature = "btree-map")]
124        let mut options: Option<BTreeMap<String, Value>> = None;
125        #[cfg(feature = "hash-map")]
126        let mut options: Option<HashMap<String, Value>> = None;
127
128        while let Some(key) = map.next_key::<String>()? {
129            match key.as_str() {
130                "name" => {
131                    if name.is_some() {
132                        return Err(serde::de::Error::duplicate_field("name"));
133                    }
134                    name = Some(map.next_value()?);
135                }
136                "args" => {
137                    if args.is_some() {
138                        return Err(serde::de::Error::duplicate_field("args"));
139                    }
140                    args = Some(map.next_value_seed(crate::typ::private::ValueVecSeed)?);
141                }
142                "options" => {
143                    if options.is_some() {
144                        return Err(serde::de::Error::duplicate_field("options"));
145                    }
146                    options = Some(map.next_value_seed(crate::typ::private::ValueMapSeed)?);
147                }
148                _ => {
149                    let _: serde::de::IgnoredAny = map.next_value()?;
150                }
151            }
152        }
153
154        let name = name.ok_or_else(|| serde::de::Error::missing_field("name"))?;
155        let args = args.ok_or_else(|| serde::de::Error::missing_field("args"))?;
156        let options = options.ok_or_else(|| serde::de::Error::missing_field("options"))?;
157
158        Ok(Stage {
159            name,
160            args,
161            options,
162        })
163    }
164}
165
166struct PipelineVisitor;
167
168impl<'de> serde::de::Visitor<'de> for PipelineVisitor {
169    type Value = Pipeline;
170
171    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
172        formatter.write_str("a Pipeline struct")
173    }
174
175    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
176    where
177        A: serde::de::MapAccess<'de>,
178    {
179        let mut stages: Option<Vec<Stage>> = None;
180
181        while let Some(key) = map.next_key::<String>()? {
182            match key.as_str() {
183                "stages" => {
184                    if stages.is_some() {
185                        return Err(serde::de::Error::duplicate_field("stages"));
186                    }
187                    stages = Some(map.next_value_seed(StageVecSeed)?);
188                }
189                _ => {
190                    let _: serde::de::IgnoredAny = map.next_value()?;
191                }
192            }
193        }
194
195        let stages = stages.ok_or_else(|| serde::de::Error::missing_field("stages"))?;
196
197        Ok(Pipeline { stages })
198    }
199}
200
201struct StageVecSeed;
202
203impl<'de> serde::de::DeserializeSeed<'de> for StageVecSeed {
204    type Value = Vec<Stage>;
205
206    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
207    where
208        D: serde::Deserializer<'de>,
209    {
210        deserializer.deserialize_seq(StageVecVisitor)
211    }
212}
213
214struct StageVecVisitor;
215
216impl<'de> serde::de::Visitor<'de> for StageVecVisitor {
217    type Value = Vec<Stage>;
218
219    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
220        formatter.write_str("a sequence of Stages")
221    }
222
223    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
224    where
225        A: serde::de::SeqAccess<'de>,
226    {
227        let mut stages = Vec::new();
228        while let Some(stage) = seq.next_element_seed(StageSeed)? {
229            stages.push(stage);
230        }
231        Ok(stages)
232    }
233}
234
235struct StageSeed;
236
237impl<'de> serde::de::DeserializeSeed<'de> for StageSeed {
238    type Value = Stage;
239
240    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
241    where
242        D: serde::Deserializer<'de>,
243    {
244        serde::Deserialize::deserialize(deserializer)
245    }
246}