nominal-api 0.1239.0

API bindings for the Nominal platform
Documentation
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 VariableValue {
    Double(#[derive_with(with = conjure_object::private::DoubleWrapper)] f64),
    ComputeNode(super::ComputeNodeWithContext),
    Duration(super::super::super::run::api::Duration),
    Integer(i32),
    Channel(Box<super::ChannelSeries>),
    Derived(Box<super::DerivedSeries>),
    String(String),
    StringSet(std::collections::BTreeSet<String>),
    Timestamp(super::super::super::super::api::Timestamp),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for VariableValue {
    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 {
            VariableValue::Double(value) => {
                map.serialize_entry(&"type", &"double")?;
                map.serialize_entry(&"double", value)?;
            }
            VariableValue::ComputeNode(value) => {
                map.serialize_entry(&"type", &"computeNode")?;
                map.serialize_entry(&"computeNode", value)?;
            }
            VariableValue::Duration(value) => {
                map.serialize_entry(&"type", &"duration")?;
                map.serialize_entry(&"duration", value)?;
            }
            VariableValue::Integer(value) => {
                map.serialize_entry(&"type", &"integer")?;
                map.serialize_entry(&"integer", value)?;
            }
            VariableValue::Channel(value) => {
                map.serialize_entry(&"type", &"channel")?;
                map.serialize_entry(&"channel", value)?;
            }
            VariableValue::Derived(value) => {
                map.serialize_entry(&"type", &"derived")?;
                map.serialize_entry(&"derived", value)?;
            }
            VariableValue::String(value) => {
                map.serialize_entry(&"type", &"string")?;
                map.serialize_entry(&"string", value)?;
            }
            VariableValue::StringSet(value) => {
                map.serialize_entry(&"type", &"stringSet")?;
                map.serialize_entry(&"stringSet", value)?;
            }
            VariableValue::Timestamp(value) => {
                map.serialize_entry(&"type", &"timestamp")?;
                map.serialize_entry(&"timestamp", value)?;
            }
            VariableValue::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for VariableValue {
    fn deserialize<D>(d: D) -> Result<VariableValue, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = VariableValue;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union VariableValue")
    }
    fn visit_map<A>(self, mut map: A) -> Result<VariableValue, 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_::Double, Some(Variant_::Double)) => {
                        let value = map.next_value()?;
                        VariableValue::Double(value)
                    }
                    (Variant_::ComputeNode, Some(Variant_::ComputeNode)) => {
                        let value = map.next_value()?;
                        VariableValue::ComputeNode(value)
                    }
                    (Variant_::Duration, Some(Variant_::Duration)) => {
                        let value = map.next_value()?;
                        VariableValue::Duration(value)
                    }
                    (Variant_::Integer, Some(Variant_::Integer)) => {
                        let value = map.next_value()?;
                        VariableValue::Integer(value)
                    }
                    (Variant_::Channel, Some(Variant_::Channel)) => {
                        let value = map.next_value()?;
                        VariableValue::Channel(value)
                    }
                    (Variant_::Derived, Some(Variant_::Derived)) => {
                        let value = map.next_value()?;
                        VariableValue::Derived(value)
                    }
                    (Variant_::String, Some(Variant_::String)) => {
                        let value = map.next_value()?;
                        VariableValue::String(value)
                    }
                    (Variant_::StringSet, Some(Variant_::StringSet)) => {
                        let value = map.next_value()?;
                        VariableValue::StringSet(value)
                    }
                    (Variant_::Timestamp, Some(Variant_::Timestamp)) => {
                        let value = map.next_value()?;
                        VariableValue::Timestamp(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            VariableValue::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_::Double => {
                        let value = map.next_value()?;
                        VariableValue::Double(value)
                    }
                    Variant_::ComputeNode => {
                        let value = map.next_value()?;
                        VariableValue::ComputeNode(value)
                    }
                    Variant_::Duration => {
                        let value = map.next_value()?;
                        VariableValue::Duration(value)
                    }
                    Variant_::Integer => {
                        let value = map.next_value()?;
                        VariableValue::Integer(value)
                    }
                    Variant_::Channel => {
                        let value = map.next_value()?;
                        VariableValue::Channel(value)
                    }
                    Variant_::Derived => {
                        let value = map.next_value()?;
                        VariableValue::Derived(value)
                    }
                    Variant_::String => {
                        let value = map.next_value()?;
                        VariableValue::String(value)
                    }
                    Variant_::StringSet => {
                        let value = map.next_value()?;
                        VariableValue::StringSet(value)
                    }
                    Variant_::Timestamp => {
                        let value = map.next_value()?;
                        VariableValue::Timestamp(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        VariableValue::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_ {
    Double,
    ComputeNode,
    Duration,
    Integer,
    Channel,
    Derived,
    String,
    StringSet,
    Timestamp,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Double => "double",
            Variant_::ComputeNode => "computeNode",
            Variant_::Duration => "duration",
            Variant_::Integer => "integer",
            Variant_::Channel => "channel",
            Variant_::Derived => "derived",
            Variant_::String => "string",
            Variant_::StringSet => "stringSet",
            Variant_::Timestamp => "timestamp",
            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 {
            "double" => Variant_::Double,
            "computeNode" => Variant_::ComputeNode,
            "duration" => Variant_::Duration,
            "integer" => Variant_::Integer,
            "channel" => Variant_::Channel,
            "derived" => Variant_::Derived,
            "string" => Variant_::String,
            "stringSet" => Variant_::StringSet,
            "timestamp" => Variant_::Timestamp,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the VariableValue 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_
    }
}