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 Locator {
    TimescaleDbLocator(super::TimescaleDbLocator),
    InfluxLocator(super::Influx2Locator),
    Influx1Locator(super::Influx1Locator),
    NominalLocator(super::NominalLocator),
    TimestreamLocator(super::TimestreamLocator),
    VisualCrossingLocator(super::VisualCrossingLocator),
    BigQueryLocator(super::BigQueryLocator),
    ApiLocator(super::ApiLocator),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for Locator {
    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 {
            Locator::TimescaleDbLocator(value) => {
                map.serialize_entry(&"type", &"timescaleDbLocator")?;
                map.serialize_entry(&"timescaleDbLocator", value)?;
            }
            Locator::InfluxLocator(value) => {
                map.serialize_entry(&"type", &"influxLocator")?;
                map.serialize_entry(&"influxLocator", value)?;
            }
            Locator::Influx1Locator(value) => {
                map.serialize_entry(&"type", &"influx1Locator")?;
                map.serialize_entry(&"influx1Locator", value)?;
            }
            Locator::NominalLocator(value) => {
                map.serialize_entry(&"type", &"nominalLocator")?;
                map.serialize_entry(&"nominalLocator", value)?;
            }
            Locator::TimestreamLocator(value) => {
                map.serialize_entry(&"type", &"timestreamLocator")?;
                map.serialize_entry(&"timestreamLocator", value)?;
            }
            Locator::VisualCrossingLocator(value) => {
                map.serialize_entry(&"type", &"visualCrossingLocator")?;
                map.serialize_entry(&"visualCrossingLocator", value)?;
            }
            Locator::BigQueryLocator(value) => {
                map.serialize_entry(&"type", &"bigQueryLocator")?;
                map.serialize_entry(&"bigQueryLocator", value)?;
            }
            Locator::ApiLocator(value) => {
                map.serialize_entry(&"type", &"apiLocator")?;
                map.serialize_entry(&"apiLocator", value)?;
            }
            Locator::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for Locator {
    fn deserialize<D>(d: D) -> Result<Locator, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = Locator;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union Locator")
    }
    fn visit_map<A>(self, mut map: A) -> Result<Locator, 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_::TimescaleDbLocator,
                        Some(Variant_::TimescaleDbLocator),
                    ) => {
                        let value = map.next_value()?;
                        Locator::TimescaleDbLocator(value)
                    }
                    (Variant_::InfluxLocator, Some(Variant_::InfluxLocator)) => {
                        let value = map.next_value()?;
                        Locator::InfluxLocator(value)
                    }
                    (Variant_::Influx1Locator, Some(Variant_::Influx1Locator)) => {
                        let value = map.next_value()?;
                        Locator::Influx1Locator(value)
                    }
                    (Variant_::NominalLocator, Some(Variant_::NominalLocator)) => {
                        let value = map.next_value()?;
                        Locator::NominalLocator(value)
                    }
                    (Variant_::TimestreamLocator, Some(Variant_::TimestreamLocator)) => {
                        let value = map.next_value()?;
                        Locator::TimestreamLocator(value)
                    }
                    (
                        Variant_::VisualCrossingLocator,
                        Some(Variant_::VisualCrossingLocator),
                    ) => {
                        let value = map.next_value()?;
                        Locator::VisualCrossingLocator(value)
                    }
                    (Variant_::BigQueryLocator, Some(Variant_::BigQueryLocator)) => {
                        let value = map.next_value()?;
                        Locator::BigQueryLocator(value)
                    }
                    (Variant_::ApiLocator, Some(Variant_::ApiLocator)) => {
                        let value = map.next_value()?;
                        Locator::ApiLocator(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            Locator::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_::TimescaleDbLocator => {
                        let value = map.next_value()?;
                        Locator::TimescaleDbLocator(value)
                    }
                    Variant_::InfluxLocator => {
                        let value = map.next_value()?;
                        Locator::InfluxLocator(value)
                    }
                    Variant_::Influx1Locator => {
                        let value = map.next_value()?;
                        Locator::Influx1Locator(value)
                    }
                    Variant_::NominalLocator => {
                        let value = map.next_value()?;
                        Locator::NominalLocator(value)
                    }
                    Variant_::TimestreamLocator => {
                        let value = map.next_value()?;
                        Locator::TimestreamLocator(value)
                    }
                    Variant_::VisualCrossingLocator => {
                        let value = map.next_value()?;
                        Locator::VisualCrossingLocator(value)
                    }
                    Variant_::BigQueryLocator => {
                        let value = map.next_value()?;
                        Locator::BigQueryLocator(value)
                    }
                    Variant_::ApiLocator => {
                        let value = map.next_value()?;
                        Locator::ApiLocator(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        Locator::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_ {
    TimescaleDbLocator,
    InfluxLocator,
    Influx1Locator,
    NominalLocator,
    TimestreamLocator,
    VisualCrossingLocator,
    BigQueryLocator,
    ApiLocator,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::TimescaleDbLocator => "timescaleDbLocator",
            Variant_::InfluxLocator => "influxLocator",
            Variant_::Influx1Locator => "influx1Locator",
            Variant_::NominalLocator => "nominalLocator",
            Variant_::TimestreamLocator => "timestreamLocator",
            Variant_::VisualCrossingLocator => "visualCrossingLocator",
            Variant_::BigQueryLocator => "bigQueryLocator",
            Variant_::ApiLocator => "apiLocator",
            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 {
            "timescaleDbLocator" => Variant_::TimescaleDbLocator,
            "influxLocator" => Variant_::InfluxLocator,
            "influx1Locator" => Variant_::Influx1Locator,
            "nominalLocator" => Variant_::NominalLocator,
            "timestreamLocator" => Variant_::TimestreamLocator,
            "visualCrossingLocator" => Variant_::VisualCrossingLocator,
            "bigQueryLocator" => Variant_::BigQueryLocator,
            "apiLocator" => Variant_::ApiLocator,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the Locator 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_
    }
}
