shrike 0.1.1

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 GraphGetRelationshipsParams {
    /// Primary account requesting relationships for.
    pub actor: String,
    /// List of 'other' accounts to be related back to the primary.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub others: Vec<String>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphGetRelationshipsOutput {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub actor: Option<crate::syntax::Did>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub relationships: Vec<GraphGetRelationshipsOutputRelationshipsUnion>,
    /// Extra fields not defined in the schema.
    #[serde(flatten)]
    pub extra: std::collections::HashMap<String, serde_json::Value>,
}

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

impl serde::Serialize for GraphGetRelationshipsOutputRelationshipsUnion {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            GraphGetRelationshipsOutputRelationshipsUnion::GraphDefsRelationship(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.graph.defs#relationship".to_string()),
                    );
                }
                map.serialize(serializer)
            }
            GraphGetRelationshipsOutputRelationshipsUnion::GraphDefsNotFoundActor(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.graph.defs#notFoundActor".to_string()),
                    );
                }
                map.serialize(serializer)
            }
            GraphGetRelationshipsOutputRelationshipsUnion::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 GraphGetRelationshipsOutputRelationshipsUnion {
    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.graph.defs#relationship" => {
                let inner: crate::api::app::bsky::GraphDefsRelationship =
                    serde_json::from_value(value).map_err(serde::de::Error::custom)?;
                Ok(
                    GraphGetRelationshipsOutputRelationshipsUnion::GraphDefsRelationship(Box::new(
                        inner,
                    )),
                )
            }
            "app.bsky.graph.defs#notFoundActor" => {
                let inner: crate::api::app::bsky::GraphDefsNotFoundActor =
                    serde_json::from_value(value).map_err(serde::de::Error::custom)?;
                Ok(
                    GraphGetRelationshipsOutputRelationshipsUnion::GraphDefsNotFoundActor(
                        Box::new(inner),
                    ),
                )
            }
            _ => Ok(GraphGetRelationshipsOutputRelationshipsUnion::Unknown(
                crate::api::UnknownUnionVariant {
                    r#type: type_str.to_string(),
                    json: Some(value),
                    cbor: None,
                },
            )),
        }
    }
}

impl GraphGetRelationshipsOutputRelationshipsUnion {
    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 {
            GraphGetRelationshipsOutputRelationshipsUnion::GraphDefsRelationship(inner) => {
                inner.encode_cbor(buf)
            }
            GraphGetRelationshipsOutputRelationshipsUnion::GraphDefsNotFoundActor(inner) => {
                inner.encode_cbor(buf)
            }
            GraphGetRelationshipsOutputRelationshipsUnion::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.graph.defs#relationship" => {
                let mut dec = crate::cbor::Decoder::new(raw);
                let inner = crate::api::app::bsky::GraphDefsRelationship::decode_cbor(&mut dec)?;
                Ok(
                    GraphGetRelationshipsOutputRelationshipsUnion::GraphDefsRelationship(Box::new(
                        inner,
                    )),
                )
            }
            "app.bsky.graph.defs#notFoundActor" => {
                let mut dec = crate::cbor::Decoder::new(raw);
                let inner = crate::api::app::bsky::GraphDefsNotFoundActor::decode_cbor(&mut dec)?;
                Ok(
                    GraphGetRelationshipsOutputRelationshipsUnion::GraphDefsNotFoundActor(
                        Box::new(inner),
                    ),
                )
            }
            _ => Ok(GraphGetRelationshipsOutputRelationshipsUnion::Unknown(
                crate::api::UnknownUnionVariant {
                    r#type: type_str.to_string(),
                    json: None,
                    cbor: Some(raw.to_vec()),
                },
            )),
        }
    }
}

/// GraphGetRelationships — Enumerates public relationships between one account, and a list of other accounts. Does not require auth.
pub async fn graph_get_relationships(
    client: &crate::xrpc::Client,
    params: &GraphGetRelationshipsParams,
) -> Result<GraphGetRelationshipsOutput, crate::xrpc::Error> {
    client
        .query("app.bsky.graph.getRelationships", params)
        .await
}