use conjure_object::serde::{ser, de};
use conjure_object::serde::ser::SerializeMap as SerializeMap_;
use conjure_object::private::{UnionField_, UnionTypeField_};
use std::fmt;
#[derive(Debug, Clone, conjure_object::private::DeriveWith)]
#[derive_with(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum NumericValuePredicate {
Equals(#[derive_with(with = conjure_object::private::DoubleWrapper)] f64),
LessThan(#[derive_with(with = conjure_object::private::DoubleWrapper)] f64),
GreaterThan(#[derive_with(with = conjure_object::private::DoubleWrapper)] f64),
Gte(#[derive_with(with = conjure_object::private::DoubleWrapper)] f64),
Lte(#[derive_with(with = conjure_object::private::DoubleWrapper)] f64),
NotEqual(#[derive_with(with = conjure_object::private::DoubleWrapper)] f64),
Between(super::NumericBetweenPredicate),
Unknown(Unknown),
}
impl ser::Serialize for NumericValuePredicate {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
let mut map = s.serialize_map(Some(2))?;
match self {
NumericValuePredicate::Equals(value) => {
map.serialize_entry(&"type", &"equals")?;
map.serialize_entry(&"equals", value)?;
}
NumericValuePredicate::LessThan(value) => {
map.serialize_entry(&"type", &"lessThan")?;
map.serialize_entry(&"lessThan", value)?;
}
NumericValuePredicate::GreaterThan(value) => {
map.serialize_entry(&"type", &"greaterThan")?;
map.serialize_entry(&"greaterThan", value)?;
}
NumericValuePredicate::Gte(value) => {
map.serialize_entry(&"type", &"gte")?;
map.serialize_entry(&"gte", value)?;
}
NumericValuePredicate::Lte(value) => {
map.serialize_entry(&"type", &"lte")?;
map.serialize_entry(&"lte", value)?;
}
NumericValuePredicate::NotEqual(value) => {
map.serialize_entry(&"type", &"notEqual")?;
map.serialize_entry(&"notEqual", value)?;
}
NumericValuePredicate::Between(value) => {
map.serialize_entry(&"type", &"between")?;
map.serialize_entry(&"between", value)?;
}
NumericValuePredicate::Unknown(value) => {
map.serialize_entry(&"type", &value.type_)?;
map.serialize_entry(&value.type_, &value.value)?;
}
}
map.end()
}
}
impl<'de> de::Deserialize<'de> for NumericValuePredicate {
fn deserialize<D>(d: D) -> Result<NumericValuePredicate, D::Error>
where
D: de::Deserializer<'de>,
{
d.deserialize_map(Visitor_)
}
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
type Value = NumericValuePredicate;
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str("union NumericValuePredicate")
}
fn visit_map<A>(self, mut map: A) -> Result<NumericValuePredicate, A::Error>
where
A: de::MapAccess<'de>,
{
let v = match map.next_key::<UnionField_<Variant_>>()? {
Some(UnionField_::Type) => {
let variant = map.next_value()?;
let key = map.next_key()?;
match (variant, key) {
(Variant_::Equals, Some(Variant_::Equals)) => {
let value = map.next_value()?;
NumericValuePredicate::Equals(value)
}
(Variant_::LessThan, Some(Variant_::LessThan)) => {
let value = map.next_value()?;
NumericValuePredicate::LessThan(value)
}
(Variant_::GreaterThan, Some(Variant_::GreaterThan)) => {
let value = map.next_value()?;
NumericValuePredicate::GreaterThan(value)
}
(Variant_::Gte, Some(Variant_::Gte)) => {
let value = map.next_value()?;
NumericValuePredicate::Gte(value)
}
(Variant_::Lte, Some(Variant_::Lte)) => {
let value = map.next_value()?;
NumericValuePredicate::Lte(value)
}
(Variant_::NotEqual, Some(Variant_::NotEqual)) => {
let value = map.next_value()?;
NumericValuePredicate::NotEqual(value)
}
(Variant_::Between, Some(Variant_::Between)) => {
let value = map.next_value()?;
NumericValuePredicate::Between(value)
}
(Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
if type_ == b {
let value = map.next_value()?;
NumericValuePredicate::Unknown(Unknown { type_, value })
} else {
return Err(
de::Error::invalid_value(de::Unexpected::Str(&type_), &&*b),
)
}
}
(variant, Some(key)) => {
return Err(
de::Error::invalid_value(
de::Unexpected::Str(key.as_str()),
&variant.as_str(),
),
);
}
(variant, None) => {
return Err(de::Error::missing_field(variant.as_str()));
}
}
}
Some(UnionField_::Value(variant)) => {
let value = match &variant {
Variant_::Equals => {
let value = map.next_value()?;
NumericValuePredicate::Equals(value)
}
Variant_::LessThan => {
let value = map.next_value()?;
NumericValuePredicate::LessThan(value)
}
Variant_::GreaterThan => {
let value = map.next_value()?;
NumericValuePredicate::GreaterThan(value)
}
Variant_::Gte => {
let value = map.next_value()?;
NumericValuePredicate::Gte(value)
}
Variant_::Lte => {
let value = map.next_value()?;
NumericValuePredicate::Lte(value)
}
Variant_::NotEqual => {
let value = map.next_value()?;
NumericValuePredicate::NotEqual(value)
}
Variant_::Between => {
let value = map.next_value()?;
NumericValuePredicate::Between(value)
}
Variant_::Unknown(type_) => {
let value = map.next_value()?;
NumericValuePredicate::Unknown(Unknown {
type_: type_.clone(),
value,
})
}
};
if map.next_key::<UnionTypeField_>()?.is_none() {
return Err(de::Error::missing_field("type"));
}
let type_variant = map.next_value::<Variant_>()?;
if variant != type_variant {
return Err(
de::Error::invalid_value(
de::Unexpected::Str(type_variant.as_str()),
&variant.as_str(),
),
);
}
value
}
None => return Err(de::Error::missing_field("type")),
};
if map.next_key::<UnionField_<Variant_>>()?.is_some() {
return Err(de::Error::invalid_length(3, &"type and value fields"));
}
Ok(v)
}
}
#[derive(PartialEq)]
enum Variant_ {
Equals,
LessThan,
GreaterThan,
Gte,
Lte,
NotEqual,
Between,
Unknown(Box<str>),
}
impl Variant_ {
fn as_str(&self) -> &'static str {
match *self {
Variant_::Equals => "equals",
Variant_::LessThan => "lessThan",
Variant_::GreaterThan => "greaterThan",
Variant_::Gte => "gte",
Variant_::Lte => "lte",
Variant_::NotEqual => "notEqual",
Variant_::Between => "between",
Variant_::Unknown(_) => "unknown variant",
}
}
}
impl<'de> de::Deserialize<'de> for Variant_ {
fn deserialize<D>(d: D) -> Result<Variant_, D::Error>
where
D: de::Deserializer<'de>,
{
d.deserialize_str(VariantVisitor_)
}
}
struct VariantVisitor_;
impl<'de> de::Visitor<'de> for VariantVisitor_ {
type Value = Variant_;
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str("string")
}
fn visit_str<E>(self, value: &str) -> Result<Variant_, E>
where
E: de::Error,
{
let v = match value {
"equals" => Variant_::Equals,
"lessThan" => Variant_::LessThan,
"greaterThan" => Variant_::GreaterThan,
"gte" => Variant_::Gte,
"lte" => Variant_::Lte,
"notEqual" => Variant_::NotEqual,
"between" => Variant_::Between,
value => Variant_::Unknown(value.to_string().into_boxed_str()),
};
Ok(v)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Unknown {
type_: Box<str>,
value: conjure_object::Any,
}
impl Unknown {
#[inline]
pub fn type_(&self) -> &str {
&self.type_
}
}