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 ConnectionDetails {
    Timescale(super::TimescaleConnectionDetails),
    Influx(super::Influx2ConnectionDetails),
    Influx1(super::Influx1ConnectionDetails),
    Nominal(super::NominalConnectionDetails),
    Timestream(super::TimestreamConnectionDetails),
    VisualCrossing(super::VisualCrossingConnectionDetails),
    BigQuery(super::BigQueryConnectionDetails),
    Api(super::ApiConnectionDetails),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for ConnectionDetails {
    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 {
            ConnectionDetails::Timescale(value) => {
                map.serialize_entry(&"type", &"timescale")?;
                map.serialize_entry(&"timescale", value)?;
            }
            ConnectionDetails::Influx(value) => {
                map.serialize_entry(&"type", &"influx")?;
                map.serialize_entry(&"influx", value)?;
            }
            ConnectionDetails::Influx1(value) => {
                map.serialize_entry(&"type", &"influx1")?;
                map.serialize_entry(&"influx1", value)?;
            }
            ConnectionDetails::Nominal(value) => {
                map.serialize_entry(&"type", &"nominal")?;
                map.serialize_entry(&"nominal", value)?;
            }
            ConnectionDetails::Timestream(value) => {
                map.serialize_entry(&"type", &"timestream")?;
                map.serialize_entry(&"timestream", value)?;
            }
            ConnectionDetails::VisualCrossing(value) => {
                map.serialize_entry(&"type", &"visualCrossing")?;
                map.serialize_entry(&"visualCrossing", value)?;
            }
            ConnectionDetails::BigQuery(value) => {
                map.serialize_entry(&"type", &"bigQuery")?;
                map.serialize_entry(&"bigQuery", value)?;
            }
            ConnectionDetails::Api(value) => {
                map.serialize_entry(&"type", &"api")?;
                map.serialize_entry(&"api", value)?;
            }
            ConnectionDetails::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for ConnectionDetails {
    fn deserialize<D>(d: D) -> Result<ConnectionDetails, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = ConnectionDetails;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union ConnectionDetails")
    }
    fn visit_map<A>(self, mut map: A) -> Result<ConnectionDetails, 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_::Timescale, Some(Variant_::Timescale)) => {
                        let value = map.next_value()?;
                        ConnectionDetails::Timescale(value)
                    }
                    (Variant_::Influx, Some(Variant_::Influx)) => {
                        let value = map.next_value()?;
                        ConnectionDetails::Influx(value)
                    }
                    (Variant_::Influx1, Some(Variant_::Influx1)) => {
                        let value = map.next_value()?;
                        ConnectionDetails::Influx1(value)
                    }
                    (Variant_::Nominal, Some(Variant_::Nominal)) => {
                        let value = map.next_value()?;
                        ConnectionDetails::Nominal(value)
                    }
                    (Variant_::Timestream, Some(Variant_::Timestream)) => {
                        let value = map.next_value()?;
                        ConnectionDetails::Timestream(value)
                    }
                    (Variant_::VisualCrossing, Some(Variant_::VisualCrossing)) => {
                        let value = map.next_value()?;
                        ConnectionDetails::VisualCrossing(value)
                    }
                    (Variant_::BigQuery, Some(Variant_::BigQuery)) => {
                        let value = map.next_value()?;
                        ConnectionDetails::BigQuery(value)
                    }
                    (Variant_::Api, Some(Variant_::Api)) => {
                        let value = map.next_value()?;
                        ConnectionDetails::Api(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            ConnectionDetails::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_::Timescale => {
                        let value = map.next_value()?;
                        ConnectionDetails::Timescale(value)
                    }
                    Variant_::Influx => {
                        let value = map.next_value()?;
                        ConnectionDetails::Influx(value)
                    }
                    Variant_::Influx1 => {
                        let value = map.next_value()?;
                        ConnectionDetails::Influx1(value)
                    }
                    Variant_::Nominal => {
                        let value = map.next_value()?;
                        ConnectionDetails::Nominal(value)
                    }
                    Variant_::Timestream => {
                        let value = map.next_value()?;
                        ConnectionDetails::Timestream(value)
                    }
                    Variant_::VisualCrossing => {
                        let value = map.next_value()?;
                        ConnectionDetails::VisualCrossing(value)
                    }
                    Variant_::BigQuery => {
                        let value = map.next_value()?;
                        ConnectionDetails::BigQuery(value)
                    }
                    Variant_::Api => {
                        let value = map.next_value()?;
                        ConnectionDetails::Api(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        ConnectionDetails::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_ {
    Timescale,
    Influx,
    Influx1,
    Nominal,
    Timestream,
    VisualCrossing,
    BigQuery,
    Api,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Timescale => "timescale",
            Variant_::Influx => "influx",
            Variant_::Influx1 => "influx1",
            Variant_::Nominal => "nominal",
            Variant_::Timestream => "timestream",
            Variant_::VisualCrossing => "visualCrossing",
            Variant_::BigQuery => "bigQuery",
            Variant_::Api => "api",
            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 {
            "timescale" => Variant_::Timescale,
            "influx" => Variant_::Influx,
            "influx1" => Variant_::Influx1,
            "nominal" => Variant_::Nominal,
            "timestream" => Variant_::Timestream,
            "visualCrossing" => Variant_::VisualCrossing,
            "bigQuery" => Variant_::BigQuery,
            "api" => Variant_::Api,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the ConnectionDetails 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_
    }
}
