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 SearchDatasetsQuery {
    SearchText(String),
    /// Performs case insensitive exact match search on the name.
    ExactMatch(String),
    Label(String),
    Properties(super::super::super::api::Property),
    IngestStatus(super::IngestStatus),
    IngestedBeforeInclusive(conjure_object::DateTime<conjure_object::Utc>),
    IngestedAfterInclusive(conjure_object::DateTime<conjure_object::Utc>),
    ArchiveStatus(bool),
    /// Filters datasets by whether they have a derived definition.
    Derived(bool),
    And(std::collections::BTreeSet<super::SearchDatasetsQuery>),
    Or(std::collections::BTreeSet<super::SearchDatasetsQuery>),
    Workspace(conjure_object::ResourceIdentifier),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for SearchDatasetsQuery {
    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 {
            SearchDatasetsQuery::SearchText(value) => {
                map.serialize_entry(&"type", &"searchText")?;
                map.serialize_entry(&"searchText", value)?;
            }
            SearchDatasetsQuery::ExactMatch(value) => {
                map.serialize_entry(&"type", &"exactMatch")?;
                map.serialize_entry(&"exactMatch", value)?;
            }
            SearchDatasetsQuery::Label(value) => {
                map.serialize_entry(&"type", &"label")?;
                map.serialize_entry(&"label", value)?;
            }
            SearchDatasetsQuery::Properties(value) => {
                map.serialize_entry(&"type", &"properties")?;
                map.serialize_entry(&"properties", value)?;
            }
            SearchDatasetsQuery::IngestStatus(value) => {
                map.serialize_entry(&"type", &"ingestStatus")?;
                map.serialize_entry(&"ingestStatus", value)?;
            }
            SearchDatasetsQuery::IngestedBeforeInclusive(value) => {
                map.serialize_entry(&"type", &"ingestedBeforeInclusive")?;
                map.serialize_entry(&"ingestedBeforeInclusive", value)?;
            }
            SearchDatasetsQuery::IngestedAfterInclusive(value) => {
                map.serialize_entry(&"type", &"ingestedAfterInclusive")?;
                map.serialize_entry(&"ingestedAfterInclusive", value)?;
            }
            SearchDatasetsQuery::ArchiveStatus(value) => {
                map.serialize_entry(&"type", &"archiveStatus")?;
                map.serialize_entry(&"archiveStatus", value)?;
            }
            SearchDatasetsQuery::Derived(value) => {
                map.serialize_entry(&"type", &"derived")?;
                map.serialize_entry(&"derived", value)?;
            }
            SearchDatasetsQuery::And(value) => {
                map.serialize_entry(&"type", &"and")?;
                map.serialize_entry(&"and", value)?;
            }
            SearchDatasetsQuery::Or(value) => {
                map.serialize_entry(&"type", &"or")?;
                map.serialize_entry(&"or", value)?;
            }
            SearchDatasetsQuery::Workspace(value) => {
                map.serialize_entry(&"type", &"workspace")?;
                map.serialize_entry(&"workspace", value)?;
            }
            SearchDatasetsQuery::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for SearchDatasetsQuery {
    fn deserialize<D>(d: D) -> Result<SearchDatasetsQuery, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = SearchDatasetsQuery;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union SearchDatasetsQuery")
    }
    fn visit_map<A>(self, mut map: A) -> Result<SearchDatasetsQuery, 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_::SearchText, Some(Variant_::SearchText)) => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::SearchText(value)
                    }
                    (Variant_::ExactMatch, Some(Variant_::ExactMatch)) => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::ExactMatch(value)
                    }
                    (Variant_::Label, Some(Variant_::Label)) => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::Label(value)
                    }
                    (Variant_::Properties, Some(Variant_::Properties)) => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::Properties(value)
                    }
                    (Variant_::IngestStatus, Some(Variant_::IngestStatus)) => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::IngestStatus(value)
                    }
                    (
                        Variant_::IngestedBeforeInclusive,
                        Some(Variant_::IngestedBeforeInclusive),
                    ) => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::IngestedBeforeInclusive(value)
                    }
                    (
                        Variant_::IngestedAfterInclusive,
                        Some(Variant_::IngestedAfterInclusive),
                    ) => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::IngestedAfterInclusive(value)
                    }
                    (Variant_::ArchiveStatus, Some(Variant_::ArchiveStatus)) => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::ArchiveStatus(value)
                    }
                    (Variant_::Derived, Some(Variant_::Derived)) => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::Derived(value)
                    }
                    (Variant_::And, Some(Variant_::And)) => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::And(value)
                    }
                    (Variant_::Or, Some(Variant_::Or)) => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::Or(value)
                    }
                    (Variant_::Workspace, Some(Variant_::Workspace)) => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::Workspace(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            SearchDatasetsQuery::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_::SearchText => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::SearchText(value)
                    }
                    Variant_::ExactMatch => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::ExactMatch(value)
                    }
                    Variant_::Label => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::Label(value)
                    }
                    Variant_::Properties => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::Properties(value)
                    }
                    Variant_::IngestStatus => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::IngestStatus(value)
                    }
                    Variant_::IngestedBeforeInclusive => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::IngestedBeforeInclusive(value)
                    }
                    Variant_::IngestedAfterInclusive => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::IngestedAfterInclusive(value)
                    }
                    Variant_::ArchiveStatus => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::ArchiveStatus(value)
                    }
                    Variant_::Derived => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::Derived(value)
                    }
                    Variant_::And => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::And(value)
                    }
                    Variant_::Or => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::Or(value)
                    }
                    Variant_::Workspace => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::Workspace(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        SearchDatasetsQuery::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_ {
    SearchText,
    ExactMatch,
    Label,
    Properties,
    IngestStatus,
    IngestedBeforeInclusive,
    IngestedAfterInclusive,
    ArchiveStatus,
    Derived,
    And,
    Or,
    Workspace,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::SearchText => "searchText",
            Variant_::ExactMatch => "exactMatch",
            Variant_::Label => "label",
            Variant_::Properties => "properties",
            Variant_::IngestStatus => "ingestStatus",
            Variant_::IngestedBeforeInclusive => "ingestedBeforeInclusive",
            Variant_::IngestedAfterInclusive => "ingestedAfterInclusive",
            Variant_::ArchiveStatus => "archiveStatus",
            Variant_::Derived => "derived",
            Variant_::And => "and",
            Variant_::Or => "or",
            Variant_::Workspace => "workspace",
            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 {
            "searchText" => Variant_::SearchText,
            "exactMatch" => Variant_::ExactMatch,
            "label" => Variant_::Label,
            "properties" => Variant_::Properties,
            "ingestStatus" => Variant_::IngestStatus,
            "ingestedBeforeInclusive" => Variant_::IngestedBeforeInclusive,
            "ingestedAfterInclusive" => Variant_::IngestedAfterInclusive,
            "archiveStatus" => Variant_::ArchiveStatus,
            "derived" => Variant_::Derived,
            "and" => Variant_::And,
            "or" => Variant_::Or,
            "workspace" => Variant_::Workspace,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the SearchDatasetsQuery 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_
    }
}
