shrike 0.1.0

AT Protocol library for Rust
Documentation
// Code generated by lexgen. DO NOT EDIT.

#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelerGetServicesParams {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub detailed: Option<bool>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub dids: Vec<String>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelerGetServicesOutput {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub views: Vec<LabelerGetServicesOutputViewsUnion>,
    /// Extra fields not defined in the schema.
    #[serde(flatten)]
    pub extra: std::collections::HashMap<String, serde_json::Value>,
}

/// LabelerGetServicesOutputViewsUnion is a union type.
#[derive(Debug, Clone)]
pub enum LabelerGetServicesOutputViewsUnion {
    LabelerDefsLabelerView(Box<crate::api::app::bsky::LabelerDefsLabelerView>),
    LabelerDefsLabelerViewDetailed(Box<crate::api::app::bsky::LabelerDefsLabelerViewDetailed>),
    Unknown(crate::api::UnknownUnionVariant),
}

impl serde::Serialize for LabelerGetServicesOutputViewsUnion {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            LabelerGetServicesOutputViewsUnion::LabelerDefsLabelerView(inner) => {
                let mut map =
                    serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
                if let serde_json::Value::Object(ref mut m) = map {
                    m.insert(
                        "$type".to_string(),
                        serde_json::Value::String("app.bsky.labeler.defs#labelerView".to_string()),
                    );
                }
                map.serialize(serializer)
            }
            LabelerGetServicesOutputViewsUnion::LabelerDefsLabelerViewDetailed(inner) => {
                let mut map =
                    serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
                if let serde_json::Value::Object(ref mut m) = map {
                    m.insert(
                        "$type".to_string(),
                        serde_json::Value::String(
                            "app.bsky.labeler.defs#labelerViewDetailed".to_string(),
                        ),
                    );
                }
                map.serialize(serializer)
            }
            LabelerGetServicesOutputViewsUnion::Unknown(v) => {
                if let Some(ref j) = v.json {
                    j.serialize(serializer)
                } else {
                    Err(serde::ser::Error::custom(
                        "no JSON data for unknown union variant",
                    ))
                }
            }
        }
    }
}

impl<'de> serde::Deserialize<'de> for LabelerGetServicesOutputViewsUnion {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let value = serde_json::Value::deserialize(deserializer)?;
        let type_str = value
            .get("$type")
            .and_then(|v| v.as_str())
            .unwrap_or_default();
        match type_str {
            "app.bsky.labeler.defs#labelerView" => {
                let inner: crate::api::app::bsky::LabelerDefsLabelerView =
                    serde_json::from_value(value).map_err(serde::de::Error::custom)?;
                Ok(LabelerGetServicesOutputViewsUnion::LabelerDefsLabelerView(
                    Box::new(inner),
                ))
            }
            "app.bsky.labeler.defs#labelerViewDetailed" => {
                let inner: crate::api::app::bsky::LabelerDefsLabelerViewDetailed =
                    serde_json::from_value(value).map_err(serde::de::Error::custom)?;
                Ok(
                    LabelerGetServicesOutputViewsUnion::LabelerDefsLabelerViewDetailed(Box::new(
                        inner,
                    )),
                )
            }
            _ => Ok(LabelerGetServicesOutputViewsUnion::Unknown(
                crate::api::UnknownUnionVariant {
                    r#type: type_str.to_string(),
                    json: Some(value),
                    cbor: None,
                },
            )),
        }
    }
}

impl LabelerGetServicesOutputViewsUnion {
    pub fn to_cbor(&self) -> Result<Vec<u8>, crate::cbor::CborError> {
        let mut buf = Vec::new();
        self.encode_cbor(&mut buf)?;
        Ok(buf)
    }

    pub fn encode_cbor(&self, buf: &mut Vec<u8>) -> Result<(), crate::cbor::CborError> {
        match self {
            LabelerGetServicesOutputViewsUnion::LabelerDefsLabelerView(inner) => {
                inner.encode_cbor(buf)
            }
            LabelerGetServicesOutputViewsUnion::LabelerDefsLabelerViewDetailed(inner) => {
                inner.encode_cbor(buf)
            }
            LabelerGetServicesOutputViewsUnion::Unknown(v) => {
                if let Some(ref data) = v.cbor {
                    buf.extend_from_slice(data);
                    Ok(())
                } else {
                    Err(crate::cbor::CborError::InvalidCbor(
                        "no CBOR data for unknown union variant".into(),
                    ))
                }
            }
        }
    }

    pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
        let mut decoder = crate::cbor::Decoder::new(data);
        let result = Self::decode_cbor(&mut decoder)?;
        if !decoder.is_empty() {
            return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
        }
        Ok(result)
    }

    pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
        // Save position, decode the value, look for $type key.
        let start = decoder.position();
        let val = decoder.decode()?;
        let end = decoder.position();
        let raw = &decoder.raw_input()[start..end];
        let entries = match val {
            crate::cbor::Value::Map(entries) => entries,
            _ => {
                return Err(crate::cbor::CborError::InvalidCbor(
                    "expected map for union".into(),
                ));
            }
        };
        let type_str = entries
            .iter()
            .find(|(k, _)| *k == "$type")
            .and_then(|(_, v)| match v {
                crate::cbor::Value::Text(s) => Some(*s),
                _ => None,
            })
            .unwrap_or_default();
        match type_str {
            "app.bsky.labeler.defs#labelerView" => {
                let mut dec = crate::cbor::Decoder::new(raw);
                let inner = crate::api::app::bsky::LabelerDefsLabelerView::decode_cbor(&mut dec)?;
                Ok(LabelerGetServicesOutputViewsUnion::LabelerDefsLabelerView(
                    Box::new(inner),
                ))
            }
            "app.bsky.labeler.defs#labelerViewDetailed" => {
                let mut dec = crate::cbor::Decoder::new(raw);
                let inner =
                    crate::api::app::bsky::LabelerDefsLabelerViewDetailed::decode_cbor(&mut dec)?;
                Ok(
                    LabelerGetServicesOutputViewsUnion::LabelerDefsLabelerViewDetailed(Box::new(
                        inner,
                    )),
                )
            }
            _ => Ok(LabelerGetServicesOutputViewsUnion::Unknown(
                crate::api::UnknownUnionVariant {
                    r#type: type_str.to_string(),
                    json: None,
                    cbor: Some(raw.to_vec()),
                },
            )),
        }
    }
}

/// LabelerGetServices — Get information about a list of labeler services.
pub async fn labeler_get_services(
    client: &crate::xrpc::Client,
    params: &LabelerGetServicesParams,
) -> Result<LabelerGetServicesOutput, crate::xrpc::Error> {
    client.query("app.bsky.labeler.getServices", params).await
}