nominal_api/conjure/objects/storage/writer/api/
points.rs1use 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 Points {
8 String(Vec<super::StringPoint>),
9 Double(Vec<super::DoublePoint>),
10 Log(Vec<super::LogPoint>),
11 Int(Vec<super::IntPoint>),
12 Uint64(Vec<super::Uint64Point>),
13 Array(Box<super::ArrayPoints>),
14 Struct(Vec<super::StructPoint>),
15 Unknown(Unknown),
17}
18impl ser::Serialize for Points {
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 Points::String(value) => {
26 map.serialize_entry(&"type", &"string")?;
27 map.serialize_entry(&"string", value)?;
28 }
29 Points::Double(value) => {
30 map.serialize_entry(&"type", &"double")?;
31 map.serialize_entry(&"double", value)?;
32 }
33 Points::Log(value) => {
34 map.serialize_entry(&"type", &"log")?;
35 map.serialize_entry(&"log", value)?;
36 }
37 Points::Int(value) => {
38 map.serialize_entry(&"type", &"int")?;
39 map.serialize_entry(&"int", value)?;
40 }
41 Points::Uint64(value) => {
42 map.serialize_entry(&"type", &"uint64")?;
43 map.serialize_entry(&"uint64", value)?;
44 }
45 Points::Array(value) => {
46 map.serialize_entry(&"type", &"array")?;
47 map.serialize_entry(&"array", value)?;
48 }
49 Points::Struct(value) => {
50 map.serialize_entry(&"type", &"struct")?;
51 map.serialize_entry(&"struct", value)?;
52 }
53 Points::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 Points {
62 fn deserialize<D>(d: D) -> Result<Points, 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 = Points;
72 fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
73 fmt.write_str("union Points")
74 }
75 fn visit_map<A>(self, mut map: A) -> Result<Points, 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_::String, Some(Variant_::String)) => {
85 let value = map.next_value()?;
86 Points::String(value)
87 }
88 (Variant_::Double, Some(Variant_::Double)) => {
89 let value = map.next_value()?;
90 Points::Double(value)
91 }
92 (Variant_::Log, Some(Variant_::Log)) => {
93 let value = map.next_value()?;
94 Points::Log(value)
95 }
96 (Variant_::Int, Some(Variant_::Int)) => {
97 let value = map.next_value()?;
98 Points::Int(value)
99 }
100 (Variant_::Uint64, Some(Variant_::Uint64)) => {
101 let value = map.next_value()?;
102 Points::Uint64(value)
103 }
104 (Variant_::Array, Some(Variant_::Array)) => {
105 let value = map.next_value()?;
106 Points::Array(value)
107 }
108 (Variant_::Struct, Some(Variant_::Struct)) => {
109 let value = map.next_value()?;
110 Points::Struct(value)
111 }
112 (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
113 if type_ == b {
114 let value = map.next_value()?;
115 Points::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_::String => {
138 let value = map.next_value()?;
139 Points::String(value)
140 }
141 Variant_::Double => {
142 let value = map.next_value()?;
143 Points::Double(value)
144 }
145 Variant_::Log => {
146 let value = map.next_value()?;
147 Points::Log(value)
148 }
149 Variant_::Int => {
150 let value = map.next_value()?;
151 Points::Int(value)
152 }
153 Variant_::Uint64 => {
154 let value = map.next_value()?;
155 Points::Uint64(value)
156 }
157 Variant_::Array => {
158 let value = map.next_value()?;
159 Points::Array(value)
160 }
161 Variant_::Struct => {
162 let value = map.next_value()?;
163 Points::Struct(value)
164 }
165 Variant_::Unknown(type_) => {
166 let value = map.next_value()?;
167 Points::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 String,
198 Double,
199 Log,
200 Int,
201 Uint64,
202 Array,
203 Struct,
204 Unknown(Box<str>),
205}
206impl Variant_ {
207 fn as_str(&self) -> &'static str {
208 match *self {
209 Variant_::String => "string",
210 Variant_::Double => "double",
211 Variant_::Log => "log",
212 Variant_::Int => "int",
213 Variant_::Uint64 => "uint64",
214 Variant_::Array => "array",
215 Variant_::Struct => "struct",
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 "string" => Variant_::String,
240 "double" => Variant_::Double,
241 "log" => Variant_::Log,
242 "int" => Variant_::Int,
243 "uint64" => Variant_::Uint64,
244 "array" => Variant_::Array,
245 "struct" => Variant_::Struct,
246 value => Variant_::Unknown(value.to_string().into_boxed_str()),
247 };
248 Ok(v)
249 }
250}
251#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
253pub struct Unknown {
254 type_: Box<str>,
255 value: conjure_object::Any,
256}
257impl Unknown {
258 #[inline]
260 pub fn type_(&self) -> &str {
261 &self.type_
262 }
263}