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 RangesNode {
    DurationFilter(super::DurationFilterRangesNode),
    EnumEquality(super::EnumEqualityRangesNode),
    EnumFilter(super::EnumFilterRangesNode),
    Extrema(super::ExtremaRangesNode),
    IntersectRange(super::IntersectRangesNode),
    LiteralRanges(super::LiteralRangesNode),
    MinMaxThreshold(super::MinMaxThresholdRangesNode),
    Not(super::NotRangesNode),
    OnChange(super::OnChangeRangesNode),
    RangeNumericAggregation(super::RangesNumericAggregationNode),
    SeriesCrossoverRangesNode(super::SeriesCrossoverRangesNode),
    StaleRange(super::StaleRangesNode),
    StabilityDetection(super::StabilityDetectionRangesNode),
    Threshold(super::ThresholdingRangesNode),
    UnionRange(super::UnionRangesNode),
    PaddedRanges(super::PaddedRangesNode),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for RangesNode {
    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 {
            RangesNode::DurationFilter(value) => {
                map.serialize_entry(&"type", &"durationFilter")?;
                map.serialize_entry(&"durationFilter", value)?;
            }
            RangesNode::EnumEquality(value) => {
                map.serialize_entry(&"type", &"enumEquality")?;
                map.serialize_entry(&"enumEquality", value)?;
            }
            RangesNode::EnumFilter(value) => {
                map.serialize_entry(&"type", &"enumFilter")?;
                map.serialize_entry(&"enumFilter", value)?;
            }
            RangesNode::Extrema(value) => {
                map.serialize_entry(&"type", &"extrema")?;
                map.serialize_entry(&"extrema", value)?;
            }
            RangesNode::IntersectRange(value) => {
                map.serialize_entry(&"type", &"intersectRange")?;
                map.serialize_entry(&"intersectRange", value)?;
            }
            RangesNode::LiteralRanges(value) => {
                map.serialize_entry(&"type", &"literalRanges")?;
                map.serialize_entry(&"literalRanges", value)?;
            }
            RangesNode::MinMaxThreshold(value) => {
                map.serialize_entry(&"type", &"minMaxThreshold")?;
                map.serialize_entry(&"minMaxThreshold", value)?;
            }
            RangesNode::Not(value) => {
                map.serialize_entry(&"type", &"not")?;
                map.serialize_entry(&"not", value)?;
            }
            RangesNode::OnChange(value) => {
                map.serialize_entry(&"type", &"onChange")?;
                map.serialize_entry(&"onChange", value)?;
            }
            RangesNode::RangeNumericAggregation(value) => {
                map.serialize_entry(&"type", &"rangeNumericAggregation")?;
                map.serialize_entry(&"rangeNumericAggregation", value)?;
            }
            RangesNode::SeriesCrossoverRangesNode(value) => {
                map.serialize_entry(&"type", &"seriesCrossoverRangesNode")?;
                map.serialize_entry(&"seriesCrossoverRangesNode", value)?;
            }
            RangesNode::StaleRange(value) => {
                map.serialize_entry(&"type", &"staleRange")?;
                map.serialize_entry(&"staleRange", value)?;
            }
            RangesNode::StabilityDetection(value) => {
                map.serialize_entry(&"type", &"stabilityDetection")?;
                map.serialize_entry(&"stabilityDetection", value)?;
            }
            RangesNode::Threshold(value) => {
                map.serialize_entry(&"type", &"threshold")?;
                map.serialize_entry(&"threshold", value)?;
            }
            RangesNode::UnionRange(value) => {
                map.serialize_entry(&"type", &"unionRange")?;
                map.serialize_entry(&"unionRange", value)?;
            }
            RangesNode::PaddedRanges(value) => {
                map.serialize_entry(&"type", &"paddedRanges")?;
                map.serialize_entry(&"paddedRanges", value)?;
            }
            RangesNode::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for RangesNode {
    fn deserialize<D>(d: D) -> Result<RangesNode, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = RangesNode;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union RangesNode")
    }
    fn visit_map<A>(self, mut map: A) -> Result<RangesNode, 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_::DurationFilter, Some(Variant_::DurationFilter)) => {
                        let value = map.next_value()?;
                        RangesNode::DurationFilter(value)
                    }
                    (Variant_::EnumEquality, Some(Variant_::EnumEquality)) => {
                        let value = map.next_value()?;
                        RangesNode::EnumEquality(value)
                    }
                    (Variant_::EnumFilter, Some(Variant_::EnumFilter)) => {
                        let value = map.next_value()?;
                        RangesNode::EnumFilter(value)
                    }
                    (Variant_::Extrema, Some(Variant_::Extrema)) => {
                        let value = map.next_value()?;
                        RangesNode::Extrema(value)
                    }
                    (Variant_::IntersectRange, Some(Variant_::IntersectRange)) => {
                        let value = map.next_value()?;
                        RangesNode::IntersectRange(value)
                    }
                    (Variant_::LiteralRanges, Some(Variant_::LiteralRanges)) => {
                        let value = map.next_value()?;
                        RangesNode::LiteralRanges(value)
                    }
                    (Variant_::MinMaxThreshold, Some(Variant_::MinMaxThreshold)) => {
                        let value = map.next_value()?;
                        RangesNode::MinMaxThreshold(value)
                    }
                    (Variant_::Not, Some(Variant_::Not)) => {
                        let value = map.next_value()?;
                        RangesNode::Not(value)
                    }
                    (Variant_::OnChange, Some(Variant_::OnChange)) => {
                        let value = map.next_value()?;
                        RangesNode::OnChange(value)
                    }
                    (
                        Variant_::RangeNumericAggregation,
                        Some(Variant_::RangeNumericAggregation),
                    ) => {
                        let value = map.next_value()?;
                        RangesNode::RangeNumericAggregation(value)
                    }
                    (
                        Variant_::SeriesCrossoverRangesNode,
                        Some(Variant_::SeriesCrossoverRangesNode),
                    ) => {
                        let value = map.next_value()?;
                        RangesNode::SeriesCrossoverRangesNode(value)
                    }
                    (Variant_::StaleRange, Some(Variant_::StaleRange)) => {
                        let value = map.next_value()?;
                        RangesNode::StaleRange(value)
                    }
                    (
                        Variant_::StabilityDetection,
                        Some(Variant_::StabilityDetection),
                    ) => {
                        let value = map.next_value()?;
                        RangesNode::StabilityDetection(value)
                    }
                    (Variant_::Threshold, Some(Variant_::Threshold)) => {
                        let value = map.next_value()?;
                        RangesNode::Threshold(value)
                    }
                    (Variant_::UnionRange, Some(Variant_::UnionRange)) => {
                        let value = map.next_value()?;
                        RangesNode::UnionRange(value)
                    }
                    (Variant_::PaddedRanges, Some(Variant_::PaddedRanges)) => {
                        let value = map.next_value()?;
                        RangesNode::PaddedRanges(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            RangesNode::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_::DurationFilter => {
                        let value = map.next_value()?;
                        RangesNode::DurationFilter(value)
                    }
                    Variant_::EnumEquality => {
                        let value = map.next_value()?;
                        RangesNode::EnumEquality(value)
                    }
                    Variant_::EnumFilter => {
                        let value = map.next_value()?;
                        RangesNode::EnumFilter(value)
                    }
                    Variant_::Extrema => {
                        let value = map.next_value()?;
                        RangesNode::Extrema(value)
                    }
                    Variant_::IntersectRange => {
                        let value = map.next_value()?;
                        RangesNode::IntersectRange(value)
                    }
                    Variant_::LiteralRanges => {
                        let value = map.next_value()?;
                        RangesNode::LiteralRanges(value)
                    }
                    Variant_::MinMaxThreshold => {
                        let value = map.next_value()?;
                        RangesNode::MinMaxThreshold(value)
                    }
                    Variant_::Not => {
                        let value = map.next_value()?;
                        RangesNode::Not(value)
                    }
                    Variant_::OnChange => {
                        let value = map.next_value()?;
                        RangesNode::OnChange(value)
                    }
                    Variant_::RangeNumericAggregation => {
                        let value = map.next_value()?;
                        RangesNode::RangeNumericAggregation(value)
                    }
                    Variant_::SeriesCrossoverRangesNode => {
                        let value = map.next_value()?;
                        RangesNode::SeriesCrossoverRangesNode(value)
                    }
                    Variant_::StaleRange => {
                        let value = map.next_value()?;
                        RangesNode::StaleRange(value)
                    }
                    Variant_::StabilityDetection => {
                        let value = map.next_value()?;
                        RangesNode::StabilityDetection(value)
                    }
                    Variant_::Threshold => {
                        let value = map.next_value()?;
                        RangesNode::Threshold(value)
                    }
                    Variant_::UnionRange => {
                        let value = map.next_value()?;
                        RangesNode::UnionRange(value)
                    }
                    Variant_::PaddedRanges => {
                        let value = map.next_value()?;
                        RangesNode::PaddedRanges(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        RangesNode::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_ {
    DurationFilter,
    EnumEquality,
    EnumFilter,
    Extrema,
    IntersectRange,
    LiteralRanges,
    MinMaxThreshold,
    Not,
    OnChange,
    RangeNumericAggregation,
    SeriesCrossoverRangesNode,
    StaleRange,
    StabilityDetection,
    Threshold,
    UnionRange,
    PaddedRanges,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::DurationFilter => "durationFilter",
            Variant_::EnumEquality => "enumEquality",
            Variant_::EnumFilter => "enumFilter",
            Variant_::Extrema => "extrema",
            Variant_::IntersectRange => "intersectRange",
            Variant_::LiteralRanges => "literalRanges",
            Variant_::MinMaxThreshold => "minMaxThreshold",
            Variant_::Not => "not",
            Variant_::OnChange => "onChange",
            Variant_::RangeNumericAggregation => "rangeNumericAggregation",
            Variant_::SeriesCrossoverRangesNode => "seriesCrossoverRangesNode",
            Variant_::StaleRange => "staleRange",
            Variant_::StabilityDetection => "stabilityDetection",
            Variant_::Threshold => "threshold",
            Variant_::UnionRange => "unionRange",
            Variant_::PaddedRanges => "paddedRanges",
            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 {
            "durationFilter" => Variant_::DurationFilter,
            "enumEquality" => Variant_::EnumEquality,
            "enumFilter" => Variant_::EnumFilter,
            "extrema" => Variant_::Extrema,
            "intersectRange" => Variant_::IntersectRange,
            "literalRanges" => Variant_::LiteralRanges,
            "minMaxThreshold" => Variant_::MinMaxThreshold,
            "not" => Variant_::Not,
            "onChange" => Variant_::OnChange,
            "rangeNumericAggregation" => Variant_::RangeNumericAggregation,
            "seriesCrossoverRangesNode" => Variant_::SeriesCrossoverRangesNode,
            "staleRange" => Variant_::StaleRange,
            "stabilityDetection" => Variant_::StabilityDetection,
            "threshold" => Variant_::Threshold,
            "unionRange" => Variant_::UnionRange,
            "paddedRanges" => Variant_::PaddedRanges,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the RangesNode 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_
    }
}
