Skip to main content

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