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 AutomaticCheckEvaluationState {
    PendingExecution(super::PendingExecutionState),
    FailedToExecute(super::FailedToExecuteState),
    Passing(super::PassingExecutionState),
    GeneratedAlerts(super::GeneratedAlertsState),
    TooManyAlerts(super::TooManyAlertsState),
    Executing(super::ExecutingState),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for AutomaticCheckEvaluationState {
    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 {
            AutomaticCheckEvaluationState::PendingExecution(value) => {
                map.serialize_entry(&"type", &"pendingExecution")?;
                map.serialize_entry(&"pendingExecution", value)?;
            }
            AutomaticCheckEvaluationState::FailedToExecute(value) => {
                map.serialize_entry(&"type", &"failedToExecute")?;
                map.serialize_entry(&"failedToExecute", value)?;
            }
            AutomaticCheckEvaluationState::Passing(value) => {
                map.serialize_entry(&"type", &"passing")?;
                map.serialize_entry(&"passing", value)?;
            }
            AutomaticCheckEvaluationState::GeneratedAlerts(value) => {
                map.serialize_entry(&"type", &"generatedAlerts")?;
                map.serialize_entry(&"generatedAlerts", value)?;
            }
            AutomaticCheckEvaluationState::TooManyAlerts(value) => {
                map.serialize_entry(&"type", &"tooManyAlerts")?;
                map.serialize_entry(&"tooManyAlerts", value)?;
            }
            AutomaticCheckEvaluationState::Executing(value) => {
                map.serialize_entry(&"type", &"executing")?;
                map.serialize_entry(&"executing", value)?;
            }
            AutomaticCheckEvaluationState::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for AutomaticCheckEvaluationState {
    fn deserialize<D>(d: D) -> Result<AutomaticCheckEvaluationState, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = AutomaticCheckEvaluationState;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union AutomaticCheckEvaluationState")
    }
    fn visit_map<A>(self, mut map: A) -> Result<AutomaticCheckEvaluationState, 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_::PendingExecution, Some(Variant_::PendingExecution)) => {
                        let value = map.next_value()?;
                        AutomaticCheckEvaluationState::PendingExecution(value)
                    }
                    (Variant_::FailedToExecute, Some(Variant_::FailedToExecute)) => {
                        let value = map.next_value()?;
                        AutomaticCheckEvaluationState::FailedToExecute(value)
                    }
                    (Variant_::Passing, Some(Variant_::Passing)) => {
                        let value = map.next_value()?;
                        AutomaticCheckEvaluationState::Passing(value)
                    }
                    (Variant_::GeneratedAlerts, Some(Variant_::GeneratedAlerts)) => {
                        let value = map.next_value()?;
                        AutomaticCheckEvaluationState::GeneratedAlerts(value)
                    }
                    (Variant_::TooManyAlerts, Some(Variant_::TooManyAlerts)) => {
                        let value = map.next_value()?;
                        AutomaticCheckEvaluationState::TooManyAlerts(value)
                    }
                    (Variant_::Executing, Some(Variant_::Executing)) => {
                        let value = map.next_value()?;
                        AutomaticCheckEvaluationState::Executing(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            AutomaticCheckEvaluationState::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_::PendingExecution => {
                        let value = map.next_value()?;
                        AutomaticCheckEvaluationState::PendingExecution(value)
                    }
                    Variant_::FailedToExecute => {
                        let value = map.next_value()?;
                        AutomaticCheckEvaluationState::FailedToExecute(value)
                    }
                    Variant_::Passing => {
                        let value = map.next_value()?;
                        AutomaticCheckEvaluationState::Passing(value)
                    }
                    Variant_::GeneratedAlerts => {
                        let value = map.next_value()?;
                        AutomaticCheckEvaluationState::GeneratedAlerts(value)
                    }
                    Variant_::TooManyAlerts => {
                        let value = map.next_value()?;
                        AutomaticCheckEvaluationState::TooManyAlerts(value)
                    }
                    Variant_::Executing => {
                        let value = map.next_value()?;
                        AutomaticCheckEvaluationState::Executing(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        AutomaticCheckEvaluationState::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_ {
    PendingExecution,
    FailedToExecute,
    Passing,
    GeneratedAlerts,
    TooManyAlerts,
    Executing,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::PendingExecution => "pendingExecution",
            Variant_::FailedToExecute => "failedToExecute",
            Variant_::Passing => "passing",
            Variant_::GeneratedAlerts => "generatedAlerts",
            Variant_::TooManyAlerts => "tooManyAlerts",
            Variant_::Executing => "executing",
            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 {
            "pendingExecution" => Variant_::PendingExecution,
            "failedToExecute" => Variant_::FailedToExecute,
            "passing" => Variant_::Passing,
            "generatedAlerts" => Variant_::GeneratedAlerts,
            "tooManyAlerts" => Variant_::TooManyAlerts,
            "executing" => Variant_::Executing,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the AutomaticCheckEvaluationState 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_
    }
}
