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