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 Numeric1dArraySeries {
    Channel(Box<super::ChannelSeries>),
    Raw(super::Reference),
    Derived(Box<super::DerivedSeries>),
    SelectNumeric1dArray(super::SelectSeries),
    ToStartOfInterval(super::Numeric1dArrayToStartOfIntervalSeries),
    TimeShift(super::Numeric1dArrayTimeShiftSeries),
    FilterByTag(super::Numeric1dArrayTagFilterSeries),
    SelectTags(super::Numeric1dArraySelectTagsSeries),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for Numeric1dArraySeries {
    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 {
            Numeric1dArraySeries::Channel(value) => {
                map.serialize_entry(&"type", &"channel")?;
                map.serialize_entry(&"channel", value)?;
            }
            Numeric1dArraySeries::Raw(value) => {
                map.serialize_entry(&"type", &"raw")?;
                map.serialize_entry(&"raw", value)?;
            }
            Numeric1dArraySeries::Derived(value) => {
                map.serialize_entry(&"type", &"derived")?;
                map.serialize_entry(&"derived", value)?;
            }
            Numeric1dArraySeries::SelectNumeric1dArray(value) => {
                map.serialize_entry(&"type", &"selectNumeric1dArray")?;
                map.serialize_entry(&"selectNumeric1dArray", value)?;
            }
            Numeric1dArraySeries::ToStartOfInterval(value) => {
                map.serialize_entry(&"type", &"toStartOfInterval")?;
                map.serialize_entry(&"toStartOfInterval", value)?;
            }
            Numeric1dArraySeries::TimeShift(value) => {
                map.serialize_entry(&"type", &"timeShift")?;
                map.serialize_entry(&"timeShift", value)?;
            }
            Numeric1dArraySeries::FilterByTag(value) => {
                map.serialize_entry(&"type", &"filterByTag")?;
                map.serialize_entry(&"filterByTag", value)?;
            }
            Numeric1dArraySeries::SelectTags(value) => {
                map.serialize_entry(&"type", &"selectTags")?;
                map.serialize_entry(&"selectTags", value)?;
            }
            Numeric1dArraySeries::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for Numeric1dArraySeries {
    fn deserialize<D>(d: D) -> Result<Numeric1dArraySeries, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = Numeric1dArraySeries;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union Numeric1dArraySeries")
    }
    fn visit_map<A>(self, mut map: A) -> Result<Numeric1dArraySeries, 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_::Channel, Some(Variant_::Channel)) => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::Channel(value)
                    }
                    (Variant_::Raw, Some(Variant_::Raw)) => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::Raw(value)
                    }
                    (Variant_::Derived, Some(Variant_::Derived)) => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::Derived(value)
                    }
                    (
                        Variant_::SelectNumeric1dArray,
                        Some(Variant_::SelectNumeric1dArray),
                    ) => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::SelectNumeric1dArray(value)
                    }
                    (Variant_::ToStartOfInterval, Some(Variant_::ToStartOfInterval)) => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::ToStartOfInterval(value)
                    }
                    (Variant_::TimeShift, Some(Variant_::TimeShift)) => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::TimeShift(value)
                    }
                    (Variant_::FilterByTag, Some(Variant_::FilterByTag)) => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::FilterByTag(value)
                    }
                    (Variant_::SelectTags, Some(Variant_::SelectTags)) => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::SelectTags(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            Numeric1dArraySeries::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_::Channel => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::Channel(value)
                    }
                    Variant_::Raw => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::Raw(value)
                    }
                    Variant_::Derived => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::Derived(value)
                    }
                    Variant_::SelectNumeric1dArray => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::SelectNumeric1dArray(value)
                    }
                    Variant_::ToStartOfInterval => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::ToStartOfInterval(value)
                    }
                    Variant_::TimeShift => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::TimeShift(value)
                    }
                    Variant_::FilterByTag => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::FilterByTag(value)
                    }
                    Variant_::SelectTags => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::SelectTags(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        Numeric1dArraySeries::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_ {
    Channel,
    Raw,
    Derived,
    SelectNumeric1dArray,
    ToStartOfInterval,
    TimeShift,
    FilterByTag,
    SelectTags,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Channel => "channel",
            Variant_::Raw => "raw",
            Variant_::Derived => "derived",
            Variant_::SelectNumeric1dArray => "selectNumeric1dArray",
            Variant_::ToStartOfInterval => "toStartOfInterval",
            Variant_::TimeShift => "timeShift",
            Variant_::FilterByTag => "filterByTag",
            Variant_::SelectTags => "selectTags",
            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 {
            "channel" => Variant_::Channel,
            "raw" => Variant_::Raw,
            "derived" => Variant_::Derived,
            "selectNumeric1dArray" => Variant_::SelectNumeric1dArray,
            "toStartOfInterval" => Variant_::ToStartOfInterval,
            "timeShift" => Variant_::TimeShift,
            "filterByTag" => Variant_::FilterByTag,
            "selectTags" => Variant_::SelectTags,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the Numeric1dArraySeries union.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Unknown {
    type_: Box<str>,
    value: conjure_object::Any,
}
impl Unknown {
    /// Returns the unknown variant's type name.
    #[inline]
    pub fn type_(&self) -> &str {
        &self.type_
    }
}
