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 IngestStatusV2 {
    Success(super::SuccessResult),
    Error(super::ErrorResult),
    InProgress(super::InProgressResult),
    DeletionInProgress(super::DeletionInProgress),
    Deleted(super::Deleted),
    Queued(super::Queued),
    Parsing(super::Parsing),
    Ingesting(super::Ingesting),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for IngestStatusV2 {
    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 {
            IngestStatusV2::Success(value) => {
                map.serialize_entry(&"type", &"success")?;
                map.serialize_entry(&"success", value)?;
            }
            IngestStatusV2::Error(value) => {
                map.serialize_entry(&"type", &"error")?;
                map.serialize_entry(&"error", value)?;
            }
            IngestStatusV2::InProgress(value) => {
                map.serialize_entry(&"type", &"inProgress")?;
                map.serialize_entry(&"inProgress", value)?;
            }
            IngestStatusV2::DeletionInProgress(value) => {
                map.serialize_entry(&"type", &"deletionInProgress")?;
                map.serialize_entry(&"deletionInProgress", value)?;
            }
            IngestStatusV2::Deleted(value) => {
                map.serialize_entry(&"type", &"deleted")?;
                map.serialize_entry(&"deleted", value)?;
            }
            IngestStatusV2::Queued(value) => {
                map.serialize_entry(&"type", &"queued")?;
                map.serialize_entry(&"queued", value)?;
            }
            IngestStatusV2::Parsing(value) => {
                map.serialize_entry(&"type", &"parsing")?;
                map.serialize_entry(&"parsing", value)?;
            }
            IngestStatusV2::Ingesting(value) => {
                map.serialize_entry(&"type", &"ingesting")?;
                map.serialize_entry(&"ingesting", value)?;
            }
            IngestStatusV2::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for IngestStatusV2 {
    fn deserialize<D>(d: D) -> Result<IngestStatusV2, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = IngestStatusV2;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union IngestStatusV2")
    }
    fn visit_map<A>(self, mut map: A) -> Result<IngestStatusV2, 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_::Success, Some(Variant_::Success)) => {
                        let value = map.next_value()?;
                        IngestStatusV2::Success(value)
                    }
                    (Variant_::Error, Some(Variant_::Error)) => {
                        let value = map.next_value()?;
                        IngestStatusV2::Error(value)
                    }
                    (Variant_::InProgress, Some(Variant_::InProgress)) => {
                        let value = map.next_value()?;
                        IngestStatusV2::InProgress(value)
                    }
                    (
                        Variant_::DeletionInProgress,
                        Some(Variant_::DeletionInProgress),
                    ) => {
                        let value = map.next_value()?;
                        IngestStatusV2::DeletionInProgress(value)
                    }
                    (Variant_::Deleted, Some(Variant_::Deleted)) => {
                        let value = map.next_value()?;
                        IngestStatusV2::Deleted(value)
                    }
                    (Variant_::Queued, Some(Variant_::Queued)) => {
                        let value = map.next_value()?;
                        IngestStatusV2::Queued(value)
                    }
                    (Variant_::Parsing, Some(Variant_::Parsing)) => {
                        let value = map.next_value()?;
                        IngestStatusV2::Parsing(value)
                    }
                    (Variant_::Ingesting, Some(Variant_::Ingesting)) => {
                        let value = map.next_value()?;
                        IngestStatusV2::Ingesting(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            IngestStatusV2::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_::Success => {
                        let value = map.next_value()?;
                        IngestStatusV2::Success(value)
                    }
                    Variant_::Error => {
                        let value = map.next_value()?;
                        IngestStatusV2::Error(value)
                    }
                    Variant_::InProgress => {
                        let value = map.next_value()?;
                        IngestStatusV2::InProgress(value)
                    }
                    Variant_::DeletionInProgress => {
                        let value = map.next_value()?;
                        IngestStatusV2::DeletionInProgress(value)
                    }
                    Variant_::Deleted => {
                        let value = map.next_value()?;
                        IngestStatusV2::Deleted(value)
                    }
                    Variant_::Queued => {
                        let value = map.next_value()?;
                        IngestStatusV2::Queued(value)
                    }
                    Variant_::Parsing => {
                        let value = map.next_value()?;
                        IngestStatusV2::Parsing(value)
                    }
                    Variant_::Ingesting => {
                        let value = map.next_value()?;
                        IngestStatusV2::Ingesting(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        IngestStatusV2::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_ {
    Success,
    Error,
    InProgress,
    DeletionInProgress,
    Deleted,
    Queued,
    Parsing,
    Ingesting,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Success => "success",
            Variant_::Error => "error",
            Variant_::InProgress => "inProgress",
            Variant_::DeletionInProgress => "deletionInProgress",
            Variant_::Deleted => "deleted",
            Variant_::Queued => "queued",
            Variant_::Parsing => "parsing",
            Variant_::Ingesting => "ingesting",
            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 {
            "success" => Variant_::Success,
            "error" => Variant_::Error,
            "inProgress" => Variant_::InProgress,
            "deletionInProgress" => Variant_::DeletionInProgress,
            "deleted" => Variant_::Deleted,
            "queued" => Variant_::Queued,
            "parsing" => Variant_::Parsing,
            "ingesting" => Variant_::Ingesting,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the IngestStatusV2 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_
    }
}
