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 RangeSeries {
    ApproximateThreshold(super::ApproximateThresholdRanges),
    BooleanToRanges(super::BooleanToRanges),
    DurationFilter(super::DurationFilterRanges),
    EnumFilter(super::EnumFilterRanges),
    EnumSeriesEqualityRangesNode(super::EnumSeriesEqualityRanges),
    EventsSearch(super::EventsSearchRanges),
    IntersectRange(super::IntersectRanges),
    LiteralRanges(super::LiteralRanges),
    /// Computes ranges where the input time series matches a filter defined by lower and upper bounds, and an operator.
    MinMaxThreshold(super::MinMaxThresholdRanges),
    Not(super::NotRanges),
    OnChange(super::OnChangeRanges),
    Peak(super::PeakRanges),
    RangeNumericAggregation(super::RangesNumericAggregation),
    Raw(super::Reference),
    Derived(Box<super::DerivedSeries>),
    SeriesCrossoverRangesNode(super::SeriesCrossoverRanges),
    SeriesEqualityRangesNode(super::SeriesEqualityRanges),
    StabilityDetection(super::StabilityDetectionRanges),
    StaleRange(super::StaleRanges),
    /// Computes ranges where the input time series matches a filter defined by a single threshold and an operator.
    Threshold(super::ThresholdingRanges),
    UnionRange(super::UnionRanges),
    PaddedRanges(super::PaddedRanges),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for RangeSeries {
    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 {
            RangeSeries::ApproximateThreshold(value) => {
                map.serialize_entry(&"type", &"approximateThreshold")?;
                map.serialize_entry(&"approximateThreshold", value)?;
            }
            RangeSeries::BooleanToRanges(value) => {
                map.serialize_entry(&"type", &"booleanToRanges")?;
                map.serialize_entry(&"booleanToRanges", value)?;
            }
            RangeSeries::DurationFilter(value) => {
                map.serialize_entry(&"type", &"durationFilter")?;
                map.serialize_entry(&"durationFilter", value)?;
            }
            RangeSeries::EnumFilter(value) => {
                map.serialize_entry(&"type", &"enumFilter")?;
                map.serialize_entry(&"enumFilter", value)?;
            }
            RangeSeries::EnumSeriesEqualityRangesNode(value) => {
                map.serialize_entry(&"type", &"enumSeriesEqualityRangesNode")?;
                map.serialize_entry(&"enumSeriesEqualityRangesNode", value)?;
            }
            RangeSeries::EventsSearch(value) => {
                map.serialize_entry(&"type", &"eventsSearch")?;
                map.serialize_entry(&"eventsSearch", value)?;
            }
            RangeSeries::IntersectRange(value) => {
                map.serialize_entry(&"type", &"intersectRange")?;
                map.serialize_entry(&"intersectRange", value)?;
            }
            RangeSeries::LiteralRanges(value) => {
                map.serialize_entry(&"type", &"literalRanges")?;
                map.serialize_entry(&"literalRanges", value)?;
            }
            RangeSeries::MinMaxThreshold(value) => {
                map.serialize_entry(&"type", &"minMaxThreshold")?;
                map.serialize_entry(&"minMaxThreshold", value)?;
            }
            RangeSeries::Not(value) => {
                map.serialize_entry(&"type", &"not")?;
                map.serialize_entry(&"not", value)?;
            }
            RangeSeries::OnChange(value) => {
                map.serialize_entry(&"type", &"onChange")?;
                map.serialize_entry(&"onChange", value)?;
            }
            RangeSeries::Peak(value) => {
                map.serialize_entry(&"type", &"peak")?;
                map.serialize_entry(&"peak", value)?;
            }
            RangeSeries::RangeNumericAggregation(value) => {
                map.serialize_entry(&"type", &"rangeNumericAggregation")?;
                map.serialize_entry(&"rangeNumericAggregation", value)?;
            }
            RangeSeries::Raw(value) => {
                map.serialize_entry(&"type", &"raw")?;
                map.serialize_entry(&"raw", value)?;
            }
            RangeSeries::Derived(value) => {
                map.serialize_entry(&"type", &"derived")?;
                map.serialize_entry(&"derived", value)?;
            }
            RangeSeries::SeriesCrossoverRangesNode(value) => {
                map.serialize_entry(&"type", &"seriesCrossoverRangesNode")?;
                map.serialize_entry(&"seriesCrossoverRangesNode", value)?;
            }
            RangeSeries::SeriesEqualityRangesNode(value) => {
                map.serialize_entry(&"type", &"seriesEqualityRangesNode")?;
                map.serialize_entry(&"seriesEqualityRangesNode", value)?;
            }
            RangeSeries::StabilityDetection(value) => {
                map.serialize_entry(&"type", &"stabilityDetection")?;
                map.serialize_entry(&"stabilityDetection", value)?;
            }
            RangeSeries::StaleRange(value) => {
                map.serialize_entry(&"type", &"staleRange")?;
                map.serialize_entry(&"staleRange", value)?;
            }
            RangeSeries::Threshold(value) => {
                map.serialize_entry(&"type", &"threshold")?;
                map.serialize_entry(&"threshold", value)?;
            }
            RangeSeries::UnionRange(value) => {
                map.serialize_entry(&"type", &"unionRange")?;
                map.serialize_entry(&"unionRange", value)?;
            }
            RangeSeries::PaddedRanges(value) => {
                map.serialize_entry(&"type", &"paddedRanges")?;
                map.serialize_entry(&"paddedRanges", value)?;
            }
            RangeSeries::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for RangeSeries {
    fn deserialize<D>(d: D) -> Result<RangeSeries, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = RangeSeries;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union RangeSeries")
    }
    fn visit_map<A>(self, mut map: A) -> Result<RangeSeries, 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_::ApproximateThreshold,
                        Some(Variant_::ApproximateThreshold),
                    ) => {
                        let value = map.next_value()?;
                        RangeSeries::ApproximateThreshold(value)
                    }
                    (Variant_::BooleanToRanges, Some(Variant_::BooleanToRanges)) => {
                        let value = map.next_value()?;
                        RangeSeries::BooleanToRanges(value)
                    }
                    (Variant_::DurationFilter, Some(Variant_::DurationFilter)) => {
                        let value = map.next_value()?;
                        RangeSeries::DurationFilter(value)
                    }
                    (Variant_::EnumFilter, Some(Variant_::EnumFilter)) => {
                        let value = map.next_value()?;
                        RangeSeries::EnumFilter(value)
                    }
                    (
                        Variant_::EnumSeriesEqualityRangesNode,
                        Some(Variant_::EnumSeriesEqualityRangesNode),
                    ) => {
                        let value = map.next_value()?;
                        RangeSeries::EnumSeriesEqualityRangesNode(value)
                    }
                    (Variant_::EventsSearch, Some(Variant_::EventsSearch)) => {
                        let value = map.next_value()?;
                        RangeSeries::EventsSearch(value)
                    }
                    (Variant_::IntersectRange, Some(Variant_::IntersectRange)) => {
                        let value = map.next_value()?;
                        RangeSeries::IntersectRange(value)
                    }
                    (Variant_::LiteralRanges, Some(Variant_::LiteralRanges)) => {
                        let value = map.next_value()?;
                        RangeSeries::LiteralRanges(value)
                    }
                    (Variant_::MinMaxThreshold, Some(Variant_::MinMaxThreshold)) => {
                        let value = map.next_value()?;
                        RangeSeries::MinMaxThreshold(value)
                    }
                    (Variant_::Not, Some(Variant_::Not)) => {
                        let value = map.next_value()?;
                        RangeSeries::Not(value)
                    }
                    (Variant_::OnChange, Some(Variant_::OnChange)) => {
                        let value = map.next_value()?;
                        RangeSeries::OnChange(value)
                    }
                    (Variant_::Peak, Some(Variant_::Peak)) => {
                        let value = map.next_value()?;
                        RangeSeries::Peak(value)
                    }
                    (
                        Variant_::RangeNumericAggregation,
                        Some(Variant_::RangeNumericAggregation),
                    ) => {
                        let value = map.next_value()?;
                        RangeSeries::RangeNumericAggregation(value)
                    }
                    (Variant_::Raw, Some(Variant_::Raw)) => {
                        let value = map.next_value()?;
                        RangeSeries::Raw(value)
                    }
                    (Variant_::Derived, Some(Variant_::Derived)) => {
                        let value = map.next_value()?;
                        RangeSeries::Derived(value)
                    }
                    (
                        Variant_::SeriesCrossoverRangesNode,
                        Some(Variant_::SeriesCrossoverRangesNode),
                    ) => {
                        let value = map.next_value()?;
                        RangeSeries::SeriesCrossoverRangesNode(value)
                    }
                    (
                        Variant_::SeriesEqualityRangesNode,
                        Some(Variant_::SeriesEqualityRangesNode),
                    ) => {
                        let value = map.next_value()?;
                        RangeSeries::SeriesEqualityRangesNode(value)
                    }
                    (
                        Variant_::StabilityDetection,
                        Some(Variant_::StabilityDetection),
                    ) => {
                        let value = map.next_value()?;
                        RangeSeries::StabilityDetection(value)
                    }
                    (Variant_::StaleRange, Some(Variant_::StaleRange)) => {
                        let value = map.next_value()?;
                        RangeSeries::StaleRange(value)
                    }
                    (Variant_::Threshold, Some(Variant_::Threshold)) => {
                        let value = map.next_value()?;
                        RangeSeries::Threshold(value)
                    }
                    (Variant_::UnionRange, Some(Variant_::UnionRange)) => {
                        let value = map.next_value()?;
                        RangeSeries::UnionRange(value)
                    }
                    (Variant_::PaddedRanges, Some(Variant_::PaddedRanges)) => {
                        let value = map.next_value()?;
                        RangeSeries::PaddedRanges(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            RangeSeries::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_::ApproximateThreshold => {
                        let value = map.next_value()?;
                        RangeSeries::ApproximateThreshold(value)
                    }
                    Variant_::BooleanToRanges => {
                        let value = map.next_value()?;
                        RangeSeries::BooleanToRanges(value)
                    }
                    Variant_::DurationFilter => {
                        let value = map.next_value()?;
                        RangeSeries::DurationFilter(value)
                    }
                    Variant_::EnumFilter => {
                        let value = map.next_value()?;
                        RangeSeries::EnumFilter(value)
                    }
                    Variant_::EnumSeriesEqualityRangesNode => {
                        let value = map.next_value()?;
                        RangeSeries::EnumSeriesEqualityRangesNode(value)
                    }
                    Variant_::EventsSearch => {
                        let value = map.next_value()?;
                        RangeSeries::EventsSearch(value)
                    }
                    Variant_::IntersectRange => {
                        let value = map.next_value()?;
                        RangeSeries::IntersectRange(value)
                    }
                    Variant_::LiteralRanges => {
                        let value = map.next_value()?;
                        RangeSeries::LiteralRanges(value)
                    }
                    Variant_::MinMaxThreshold => {
                        let value = map.next_value()?;
                        RangeSeries::MinMaxThreshold(value)
                    }
                    Variant_::Not => {
                        let value = map.next_value()?;
                        RangeSeries::Not(value)
                    }
                    Variant_::OnChange => {
                        let value = map.next_value()?;
                        RangeSeries::OnChange(value)
                    }
                    Variant_::Peak => {
                        let value = map.next_value()?;
                        RangeSeries::Peak(value)
                    }
                    Variant_::RangeNumericAggregation => {
                        let value = map.next_value()?;
                        RangeSeries::RangeNumericAggregation(value)
                    }
                    Variant_::Raw => {
                        let value = map.next_value()?;
                        RangeSeries::Raw(value)
                    }
                    Variant_::Derived => {
                        let value = map.next_value()?;
                        RangeSeries::Derived(value)
                    }
                    Variant_::SeriesCrossoverRangesNode => {
                        let value = map.next_value()?;
                        RangeSeries::SeriesCrossoverRangesNode(value)
                    }
                    Variant_::SeriesEqualityRangesNode => {
                        let value = map.next_value()?;
                        RangeSeries::SeriesEqualityRangesNode(value)
                    }
                    Variant_::StabilityDetection => {
                        let value = map.next_value()?;
                        RangeSeries::StabilityDetection(value)
                    }
                    Variant_::StaleRange => {
                        let value = map.next_value()?;
                        RangeSeries::StaleRange(value)
                    }
                    Variant_::Threshold => {
                        let value = map.next_value()?;
                        RangeSeries::Threshold(value)
                    }
                    Variant_::UnionRange => {
                        let value = map.next_value()?;
                        RangeSeries::UnionRange(value)
                    }
                    Variant_::PaddedRanges => {
                        let value = map.next_value()?;
                        RangeSeries::PaddedRanges(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        RangeSeries::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_ {
    ApproximateThreshold,
    BooleanToRanges,
    DurationFilter,
    EnumFilter,
    EnumSeriesEqualityRangesNode,
    EventsSearch,
    IntersectRange,
    LiteralRanges,
    MinMaxThreshold,
    Not,
    OnChange,
    Peak,
    RangeNumericAggregation,
    Raw,
    Derived,
    SeriesCrossoverRangesNode,
    SeriesEqualityRangesNode,
    StabilityDetection,
    StaleRange,
    Threshold,
    UnionRange,
    PaddedRanges,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::ApproximateThreshold => "approximateThreshold",
            Variant_::BooleanToRanges => "booleanToRanges",
            Variant_::DurationFilter => "durationFilter",
            Variant_::EnumFilter => "enumFilter",
            Variant_::EnumSeriesEqualityRangesNode => "enumSeriesEqualityRangesNode",
            Variant_::EventsSearch => "eventsSearch",
            Variant_::IntersectRange => "intersectRange",
            Variant_::LiteralRanges => "literalRanges",
            Variant_::MinMaxThreshold => "minMaxThreshold",
            Variant_::Not => "not",
            Variant_::OnChange => "onChange",
            Variant_::Peak => "peak",
            Variant_::RangeNumericAggregation => "rangeNumericAggregation",
            Variant_::Raw => "raw",
            Variant_::Derived => "derived",
            Variant_::SeriesCrossoverRangesNode => "seriesCrossoverRangesNode",
            Variant_::SeriesEqualityRangesNode => "seriesEqualityRangesNode",
            Variant_::StabilityDetection => "stabilityDetection",
            Variant_::StaleRange => "staleRange",
            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 {
            "approximateThreshold" => Variant_::ApproximateThreshold,
            "booleanToRanges" => Variant_::BooleanToRanges,
            "durationFilter" => Variant_::DurationFilter,
            "enumFilter" => Variant_::EnumFilter,
            "enumSeriesEqualityRangesNode" => Variant_::EnumSeriesEqualityRangesNode,
            "eventsSearch" => Variant_::EventsSearch,
            "intersectRange" => Variant_::IntersectRange,
            "literalRanges" => Variant_::LiteralRanges,
            "minMaxThreshold" => Variant_::MinMaxThreshold,
            "not" => Variant_::Not,
            "onChange" => Variant_::OnChange,
            "peak" => Variant_::Peak,
            "rangeNumericAggregation" => Variant_::RangeNumericAggregation,
            "raw" => Variant_::Raw,
            "derived" => Variant_::Derived,
            "seriesCrossoverRangesNode" => Variant_::SeriesCrossoverRangesNode,
            "seriesEqualityRangesNode" => Variant_::SeriesEqualityRangesNode,
            "stabilityDetection" => Variant_::StabilityDetection,
            "staleRange" => Variant_::StaleRange,
            "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 RangeSeries 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_
    }
}
