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