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, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum LastFailure {
    /// The last failure time has not been computed, and is not currently being computed.
    NotComputed(super::NotComputed),
    /// The last failure time is currently being computed. This may require evaluating the history
    /// of data for the check, so could be slow depending on the data frequency and lookback period.
    Computing(super::Computing),
    /// No previous failure was found for the check.
    NoPreviousFailure(super::NoPreviousFailure),
    /// The most recent failure for the check.
    Failure(super::Failure),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for LastFailure {
    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 {
            LastFailure::NotComputed(value) => {
                map.serialize_entry(&"type", &"notComputed")?;
                map.serialize_entry(&"notComputed", value)?;
            }
            LastFailure::Computing(value) => {
                map.serialize_entry(&"type", &"computing")?;
                map.serialize_entry(&"computing", value)?;
            }
            LastFailure::NoPreviousFailure(value) => {
                map.serialize_entry(&"type", &"noPreviousFailure")?;
                map.serialize_entry(&"noPreviousFailure", value)?;
            }
            LastFailure::Failure(value) => {
                map.serialize_entry(&"type", &"failure")?;
                map.serialize_entry(&"failure", value)?;
            }
            LastFailure::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for LastFailure {
    fn deserialize<D>(d: D) -> Result<LastFailure, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = LastFailure;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union LastFailure")
    }
    fn visit_map<A>(self, mut map: A) -> Result<LastFailure, 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_::NotComputed, Some(Variant_::NotComputed)) => {
                        let value = map.next_value()?;
                        LastFailure::NotComputed(value)
                    }
                    (Variant_::Computing, Some(Variant_::Computing)) => {
                        let value = map.next_value()?;
                        LastFailure::Computing(value)
                    }
                    (Variant_::NoPreviousFailure, Some(Variant_::NoPreviousFailure)) => {
                        let value = map.next_value()?;
                        LastFailure::NoPreviousFailure(value)
                    }
                    (Variant_::Failure, Some(Variant_::Failure)) => {
                        let value = map.next_value()?;
                        LastFailure::Failure(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            LastFailure::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_::NotComputed => {
                        let value = map.next_value()?;
                        LastFailure::NotComputed(value)
                    }
                    Variant_::Computing => {
                        let value = map.next_value()?;
                        LastFailure::Computing(value)
                    }
                    Variant_::NoPreviousFailure => {
                        let value = map.next_value()?;
                        LastFailure::NoPreviousFailure(value)
                    }
                    Variant_::Failure => {
                        let value = map.next_value()?;
                        LastFailure::Failure(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        LastFailure::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_ {
    NotComputed,
    Computing,
    NoPreviousFailure,
    Failure,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::NotComputed => "notComputed",
            Variant_::Computing => "computing",
            Variant_::NoPreviousFailure => "noPreviousFailure",
            Variant_::Failure => "failure",
            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 {
            "notComputed" => Variant_::NotComputed,
            "computing" => Variant_::Computing,
            "noPreviousFailure" => Variant_::NoPreviousFailure,
            "failure" => Variant_::Failure,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the LastFailure 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_
    }
}
