Skip to main content

nominal_api_conjure/conjure/objects/scout/layout/api/
panel.rs

1use conjure_object::serde::{ser, de};
2use conjure_object::serde::ser::SerializeMap as SerializeMap_;
3use conjure_object::private::{UnionField_, UnionTypeField_};
4use std::fmt;
5#[derive(Debug, Clone, conjure_object::private::DeriveWith)]
6#[derive_with(PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub enum Panel {
8    Viz(Box<super::VizPanel>),
9    Chart(Box<super::ChartPanel>),
10    Empty(Box<super::EmptyPanel>),
11    Split(Box<super::SplitPanel>),
12    Tabbed(Box<super::TabbedPanel>),
13    Canvas(super::CanvasLayout),
14    Document(super::DocumentLayout),
15    /// An unknown variant.
16    Unknown(Unknown),
17}
18impl ser::Serialize for Panel {
19    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
20    where
21        S: ser::Serializer,
22    {
23        let mut map = s.serialize_map(Some(2))?;
24        match self {
25            Panel::Viz(value) => {
26                map.serialize_entry(&"type", &"viz")?;
27                map.serialize_entry(&"viz", value)?;
28            }
29            Panel::Chart(value) => {
30                map.serialize_entry(&"type", &"chart")?;
31                map.serialize_entry(&"chart", value)?;
32            }
33            Panel::Empty(value) => {
34                map.serialize_entry(&"type", &"empty")?;
35                map.serialize_entry(&"empty", value)?;
36            }
37            Panel::Split(value) => {
38                map.serialize_entry(&"type", &"split")?;
39                map.serialize_entry(&"split", value)?;
40            }
41            Panel::Tabbed(value) => {
42                map.serialize_entry(&"type", &"tabbed")?;
43                map.serialize_entry(&"tabbed", value)?;
44            }
45            Panel::Canvas(value) => {
46                map.serialize_entry(&"type", &"canvas")?;
47                map.serialize_entry(&"canvas", value)?;
48            }
49            Panel::Document(value) => {
50                map.serialize_entry(&"type", &"document")?;
51                map.serialize_entry(&"document", value)?;
52            }
53            Panel::Unknown(value) => {
54                map.serialize_entry(&"type", &value.type_)?;
55                map.serialize_entry(&value.type_, &value.value)?;
56            }
57        }
58        map.end()
59    }
60}
61impl<'de> de::Deserialize<'de> for Panel {
62    fn deserialize<D>(d: D) -> Result<Panel, D::Error>
63    where
64        D: de::Deserializer<'de>,
65    {
66        d.deserialize_map(Visitor_)
67    }
68}
69struct Visitor_;
70impl<'de> de::Visitor<'de> for Visitor_ {
71    type Value = Panel;
72    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
73        fmt.write_str("union Panel")
74    }
75    fn visit_map<A>(self, mut map: A) -> Result<Panel, A::Error>
76    where
77        A: de::MapAccess<'de>,
78    {
79        let v = match map.next_key::<UnionField_<Variant_>>()? {
80            Some(UnionField_::Type) => {
81                let variant = map.next_value()?;
82                let key = map.next_key()?;
83                match (variant, key) {
84                    (Variant_::Viz, Some(Variant_::Viz)) => {
85                        let value = map.next_value()?;
86                        Panel::Viz(value)
87                    }
88                    (Variant_::Chart, Some(Variant_::Chart)) => {
89                        let value = map.next_value()?;
90                        Panel::Chart(value)
91                    }
92                    (Variant_::Empty, Some(Variant_::Empty)) => {
93                        let value = map.next_value()?;
94                        Panel::Empty(value)
95                    }
96                    (Variant_::Split, Some(Variant_::Split)) => {
97                        let value = map.next_value()?;
98                        Panel::Split(value)
99                    }
100                    (Variant_::Tabbed, Some(Variant_::Tabbed)) => {
101                        let value = map.next_value()?;
102                        Panel::Tabbed(value)
103                    }
104                    (Variant_::Canvas, Some(Variant_::Canvas)) => {
105                        let value = map.next_value()?;
106                        Panel::Canvas(value)
107                    }
108                    (Variant_::Document, Some(Variant_::Document)) => {
109                        let value = map.next_value()?;
110                        Panel::Document(value)
111                    }
112                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
113                        if type_ == b {
114                            let value = map.next_value()?;
115                            Panel::Unknown(Unknown { type_, value })
116                        } else {
117                            return Err(
118                                de::Error::invalid_value(de::Unexpected::Str(&type_), &&*b),
119                            )
120                        }
121                    }
122                    (variant, Some(key)) => {
123                        return Err(
124                            de::Error::invalid_value(
125                                de::Unexpected::Str(key.as_str()),
126                                &variant.as_str(),
127                            ),
128                        );
129                    }
130                    (variant, None) => {
131                        return Err(de::Error::missing_field(variant.as_str()));
132                    }
133                }
134            }
135            Some(UnionField_::Value(variant)) => {
136                let value = match &variant {
137                    Variant_::Viz => {
138                        let value = map.next_value()?;
139                        Panel::Viz(value)
140                    }
141                    Variant_::Chart => {
142                        let value = map.next_value()?;
143                        Panel::Chart(value)
144                    }
145                    Variant_::Empty => {
146                        let value = map.next_value()?;
147                        Panel::Empty(value)
148                    }
149                    Variant_::Split => {
150                        let value = map.next_value()?;
151                        Panel::Split(value)
152                    }
153                    Variant_::Tabbed => {
154                        let value = map.next_value()?;
155                        Panel::Tabbed(value)
156                    }
157                    Variant_::Canvas => {
158                        let value = map.next_value()?;
159                        Panel::Canvas(value)
160                    }
161                    Variant_::Document => {
162                        let value = map.next_value()?;
163                        Panel::Document(value)
164                    }
165                    Variant_::Unknown(type_) => {
166                        let value = map.next_value()?;
167                        Panel::Unknown(Unknown {
168                            type_: type_.clone(),
169                            value,
170                        })
171                    }
172                };
173                if map.next_key::<UnionTypeField_>()?.is_none() {
174                    return Err(de::Error::missing_field("type"));
175                }
176                let type_variant = map.next_value::<Variant_>()?;
177                if variant != type_variant {
178                    return Err(
179                        de::Error::invalid_value(
180                            de::Unexpected::Str(type_variant.as_str()),
181                            &variant.as_str(),
182                        ),
183                    );
184                }
185                value
186            }
187            None => return Err(de::Error::missing_field("type")),
188        };
189        if map.next_key::<UnionField_<Variant_>>()?.is_some() {
190            return Err(de::Error::invalid_length(3, &"type and value fields"));
191        }
192        Ok(v)
193    }
194}
195#[derive(PartialEq)]
196enum Variant_ {
197    Viz,
198    Chart,
199    Empty,
200    Split,
201    Tabbed,
202    Canvas,
203    Document,
204    Unknown(Box<str>),
205}
206impl Variant_ {
207    fn as_str(&self) -> &'static str {
208        match *self {
209            Variant_::Viz => "viz",
210            Variant_::Chart => "chart",
211            Variant_::Empty => "empty",
212            Variant_::Split => "split",
213            Variant_::Tabbed => "tabbed",
214            Variant_::Canvas => "canvas",
215            Variant_::Document => "document",
216            Variant_::Unknown(_) => "unknown variant",
217        }
218    }
219}
220impl<'de> de::Deserialize<'de> for Variant_ {
221    fn deserialize<D>(d: D) -> Result<Variant_, D::Error>
222    where
223        D: de::Deserializer<'de>,
224    {
225        d.deserialize_str(VariantVisitor_)
226    }
227}
228struct VariantVisitor_;
229impl<'de> de::Visitor<'de> for VariantVisitor_ {
230    type Value = Variant_;
231    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
232        fmt.write_str("string")
233    }
234    fn visit_str<E>(self, value: &str) -> Result<Variant_, E>
235    where
236        E: de::Error,
237    {
238        let v = match value {
239            "viz" => Variant_::Viz,
240            "chart" => Variant_::Chart,
241            "empty" => Variant_::Empty,
242            "split" => Variant_::Split,
243            "tabbed" => Variant_::Tabbed,
244            "canvas" => Variant_::Canvas,
245            "document" => Variant_::Document,
246            value => Variant_::Unknown(value.to_string().into_boxed_str()),
247        };
248        Ok(v)
249    }
250}
251///An unknown variant of the Panel union.
252#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
253pub struct Unknown {
254    type_: Box<str>,
255    value: conjure_object::Any,
256}
257impl Unknown {
258    /// Returns the unknown variant's type name.
259    #[inline]
260    pub fn type_(&self) -> &str {
261        &self.type_
262    }
263}