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, conjure_object::private::DeriveWith)]
#[derive_with(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum IngestOptions {
    Dataflash(super::DataflashOpts),
    McapProtobufTimeseries(super::McapProtobufTimeseriesOpts),
    JournalJson(super::JournalJsonOpts),
    Csv(super::CsvOpts),
    Parquet(super::ParquetOpts),
    Video(super::VideoOpts),
    VideoV2(super::VideoOptsV2),
    Containerized(super::ContainerizedOpts),
    AvroStream(super::AvroStreamOpts),
    PointCloud(super::PointCloudOpts),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for IngestOptions {
    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 {
            IngestOptions::Dataflash(value) => {
                map.serialize_entry(&"type", &"dataflash")?;
                map.serialize_entry(&"dataflash", value)?;
            }
            IngestOptions::McapProtobufTimeseries(value) => {
                map.serialize_entry(&"type", &"mcapProtobufTimeseries")?;
                map.serialize_entry(&"mcapProtobufTimeseries", value)?;
            }
            IngestOptions::JournalJson(value) => {
                map.serialize_entry(&"type", &"journalJson")?;
                map.serialize_entry(&"journalJson", value)?;
            }
            IngestOptions::Csv(value) => {
                map.serialize_entry(&"type", &"csv")?;
                map.serialize_entry(&"csv", value)?;
            }
            IngestOptions::Parquet(value) => {
                map.serialize_entry(&"type", &"parquet")?;
                map.serialize_entry(&"parquet", value)?;
            }
            IngestOptions::Video(value) => {
                map.serialize_entry(&"type", &"video")?;
                map.serialize_entry(&"video", value)?;
            }
            IngestOptions::VideoV2(value) => {
                map.serialize_entry(&"type", &"videoV2")?;
                map.serialize_entry(&"videoV2", value)?;
            }
            IngestOptions::Containerized(value) => {
                map.serialize_entry(&"type", &"containerized")?;
                map.serialize_entry(&"containerized", value)?;
            }
            IngestOptions::AvroStream(value) => {
                map.serialize_entry(&"type", &"avroStream")?;
                map.serialize_entry(&"avroStream", value)?;
            }
            IngestOptions::PointCloud(value) => {
                map.serialize_entry(&"type", &"pointCloud")?;
                map.serialize_entry(&"pointCloud", value)?;
            }
            IngestOptions::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for IngestOptions {
    fn deserialize<D>(d: D) -> Result<IngestOptions, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = IngestOptions;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union IngestOptions")
    }
    fn visit_map<A>(self, mut map: A) -> Result<IngestOptions, 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_::Dataflash, Some(Variant_::Dataflash)) => {
                        let value = map.next_value()?;
                        IngestOptions::Dataflash(value)
                    }
                    (
                        Variant_::McapProtobufTimeseries,
                        Some(Variant_::McapProtobufTimeseries),
                    ) => {
                        let value = map.next_value()?;
                        IngestOptions::McapProtobufTimeseries(value)
                    }
                    (Variant_::JournalJson, Some(Variant_::JournalJson)) => {
                        let value = map.next_value()?;
                        IngestOptions::JournalJson(value)
                    }
                    (Variant_::Csv, Some(Variant_::Csv)) => {
                        let value = map.next_value()?;
                        IngestOptions::Csv(value)
                    }
                    (Variant_::Parquet, Some(Variant_::Parquet)) => {
                        let value = map.next_value()?;
                        IngestOptions::Parquet(value)
                    }
                    (Variant_::Video, Some(Variant_::Video)) => {
                        let value = map.next_value()?;
                        IngestOptions::Video(value)
                    }
                    (Variant_::VideoV2, Some(Variant_::VideoV2)) => {
                        let value = map.next_value()?;
                        IngestOptions::VideoV2(value)
                    }
                    (Variant_::Containerized, Some(Variant_::Containerized)) => {
                        let value = map.next_value()?;
                        IngestOptions::Containerized(value)
                    }
                    (Variant_::AvroStream, Some(Variant_::AvroStream)) => {
                        let value = map.next_value()?;
                        IngestOptions::AvroStream(value)
                    }
                    (Variant_::PointCloud, Some(Variant_::PointCloud)) => {
                        let value = map.next_value()?;
                        IngestOptions::PointCloud(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            IngestOptions::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_::Dataflash => {
                        let value = map.next_value()?;
                        IngestOptions::Dataflash(value)
                    }
                    Variant_::McapProtobufTimeseries => {
                        let value = map.next_value()?;
                        IngestOptions::McapProtobufTimeseries(value)
                    }
                    Variant_::JournalJson => {
                        let value = map.next_value()?;
                        IngestOptions::JournalJson(value)
                    }
                    Variant_::Csv => {
                        let value = map.next_value()?;
                        IngestOptions::Csv(value)
                    }
                    Variant_::Parquet => {
                        let value = map.next_value()?;
                        IngestOptions::Parquet(value)
                    }
                    Variant_::Video => {
                        let value = map.next_value()?;
                        IngestOptions::Video(value)
                    }
                    Variant_::VideoV2 => {
                        let value = map.next_value()?;
                        IngestOptions::VideoV2(value)
                    }
                    Variant_::Containerized => {
                        let value = map.next_value()?;
                        IngestOptions::Containerized(value)
                    }
                    Variant_::AvroStream => {
                        let value = map.next_value()?;
                        IngestOptions::AvroStream(value)
                    }
                    Variant_::PointCloud => {
                        let value = map.next_value()?;
                        IngestOptions::PointCloud(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        IngestOptions::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_ {
    Dataflash,
    McapProtobufTimeseries,
    JournalJson,
    Csv,
    Parquet,
    Video,
    VideoV2,
    Containerized,
    AvroStream,
    PointCloud,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Dataflash => "dataflash",
            Variant_::McapProtobufTimeseries => "mcapProtobufTimeseries",
            Variant_::JournalJson => "journalJson",
            Variant_::Csv => "csv",
            Variant_::Parquet => "parquet",
            Variant_::Video => "video",
            Variant_::VideoV2 => "videoV2",
            Variant_::Containerized => "containerized",
            Variant_::AvroStream => "avroStream",
            Variant_::PointCloud => "pointCloud",
            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 {
            "dataflash" => Variant_::Dataflash,
            "mcapProtobufTimeseries" => Variant_::McapProtobufTimeseries,
            "journalJson" => Variant_::JournalJson,
            "csv" => Variant_::Csv,
            "parquet" => Variant_::Parquet,
            "video" => Variant_::Video,
            "videoV2" => Variant_::VideoV2,
            "containerized" => Variant_::Containerized,
            "avroStream" => Variant_::AvroStream,
            "pointCloud" => Variant_::PointCloud,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the IngestOptions 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_
    }
}
