nominal_api/conjure/objects/scout/dataexport/api/
timestamp_format.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, PartialEq, Eq, PartialOrd, Ord, Hash)]
6pub enum TimestampFormat {
7 Iso8601(super::Iso8601TimestampFormat),
8 Relative(super::RelativeTimestampFormat),
9 Unknown(Unknown),
11}
12impl ser::Serialize for TimestampFormat {
13 fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
14 where
15 S: ser::Serializer,
16 {
17 let mut map = s.serialize_map(Some(2))?;
18 match self {
19 TimestampFormat::Iso8601(value) => {
20 map.serialize_entry(&"type", &"iso8601")?;
21 map.serialize_entry(&"iso8601", value)?;
22 }
23 TimestampFormat::Relative(value) => {
24 map.serialize_entry(&"type", &"relative")?;
25 map.serialize_entry(&"relative", value)?;
26 }
27 TimestampFormat::Unknown(value) => {
28 map.serialize_entry(&"type", &value.type_)?;
29 map.serialize_entry(&value.type_, &value.value)?;
30 }
31 }
32 map.end()
33 }
34}
35impl<'de> de::Deserialize<'de> for TimestampFormat {
36 fn deserialize<D>(d: D) -> Result<TimestampFormat, D::Error>
37 where
38 D: de::Deserializer<'de>,
39 {
40 d.deserialize_map(Visitor_)
41 }
42}
43struct Visitor_;
44impl<'de> de::Visitor<'de> for Visitor_ {
45 type Value = TimestampFormat;
46 fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
47 fmt.write_str("union TimestampFormat")
48 }
49 fn visit_map<A>(self, mut map: A) -> Result<TimestampFormat, A::Error>
50 where
51 A: de::MapAccess<'de>,
52 {
53 let v = match map.next_key::<UnionField_<Variant_>>()? {
54 Some(UnionField_::Type) => {
55 let variant = map.next_value()?;
56 let key = map.next_key()?;
57 match (variant, key) {
58 (Variant_::Iso8601, Some(Variant_::Iso8601)) => {
59 let value = map.next_value()?;
60 TimestampFormat::Iso8601(value)
61 }
62 (Variant_::Relative, Some(Variant_::Relative)) => {
63 let value = map.next_value()?;
64 TimestampFormat::Relative(value)
65 }
66 (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
67 if type_ == b {
68 let value = map.next_value()?;
69 TimestampFormat::Unknown(Unknown { type_, value })
70 } else {
71 return Err(
72 de::Error::invalid_value(de::Unexpected::Str(&type_), &&*b),
73 )
74 }
75 }
76 (variant, Some(key)) => {
77 return Err(
78 de::Error::invalid_value(
79 de::Unexpected::Str(key.as_str()),
80 &variant.as_str(),
81 ),
82 );
83 }
84 (variant, None) => {
85 return Err(de::Error::missing_field(variant.as_str()));
86 }
87 }
88 }
89 Some(UnionField_::Value(variant)) => {
90 let value = match &variant {
91 Variant_::Iso8601 => {
92 let value = map.next_value()?;
93 TimestampFormat::Iso8601(value)
94 }
95 Variant_::Relative => {
96 let value = map.next_value()?;
97 TimestampFormat::Relative(value)
98 }
99 Variant_::Unknown(type_) => {
100 let value = map.next_value()?;
101 TimestampFormat::Unknown(Unknown {
102 type_: type_.clone(),
103 value,
104 })
105 }
106 };
107 if map.next_key::<UnionTypeField_>()?.is_none() {
108 return Err(de::Error::missing_field("type"));
109 }
110 let type_variant = map.next_value::<Variant_>()?;
111 if variant != type_variant {
112 return Err(
113 de::Error::invalid_value(
114 de::Unexpected::Str(type_variant.as_str()),
115 &variant.as_str(),
116 ),
117 );
118 }
119 value
120 }
121 None => return Err(de::Error::missing_field("type")),
122 };
123 if map.next_key::<UnionField_<Variant_>>()?.is_some() {
124 return Err(de::Error::invalid_length(3, &"type and value fields"));
125 }
126 Ok(v)
127 }
128}
129#[derive(PartialEq)]
130enum Variant_ {
131 Iso8601,
132 Relative,
133 Unknown(Box<str>),
134}
135impl Variant_ {
136 fn as_str(&self) -> &'static str {
137 match *self {
138 Variant_::Iso8601 => "iso8601",
139 Variant_::Relative => "relative",
140 Variant_::Unknown(_) => "unknown variant",
141 }
142 }
143}
144impl<'de> de::Deserialize<'de> for Variant_ {
145 fn deserialize<D>(d: D) -> Result<Variant_, D::Error>
146 where
147 D: de::Deserializer<'de>,
148 {
149 d.deserialize_str(VariantVisitor_)
150 }
151}
152struct VariantVisitor_;
153impl<'de> de::Visitor<'de> for VariantVisitor_ {
154 type Value = Variant_;
155 fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
156 fmt.write_str("string")
157 }
158 fn visit_str<E>(self, value: &str) -> Result<Variant_, E>
159 where
160 E: de::Error,
161 {
162 let v = match value {
163 "iso8601" => Variant_::Iso8601,
164 "relative" => Variant_::Relative,
165 value => Variant_::Unknown(value.to_string().into_boxed_str()),
166 };
167 Ok(v)
168 }
169}
170#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
172pub struct Unknown {
173 type_: Box<str>,
174 value: conjure_object::Any,
175}
176impl Unknown {
177 #[inline]
179 pub fn type_(&self) -> &str {
180 &self.type_
181 }
182}