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