shrike 0.1.1

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

/// ContactDefsMatchAndContactIndex — Associates a profile with the positional index of the contact import input in the call to `app.bsky.contact.importContacts`, so clients can know which phone caused a particular match.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ContactDefsMatchAndContactIndex {
    /// The index of this match in the import contact input.
    pub contact_index: i64,
    /// Profile of the matched user.
    pub r#match: crate::api::app::bsky::ActorDefsProfileView,
    /// Extra fields not defined in the schema (JSON).
    #[serde(flatten)]
    pub extra: std::collections::HashMap<String, serde_json::Value>,
    /// Extra fields not defined in the schema (CBOR).
    #[serde(skip)]
    pub extra_cbor: Vec<(String, Vec<u8>)>,
}

impl ContactDefsMatchAndContactIndex {
    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> {
        if self.extra_cbor.is_empty() {
            // Fast path: no extra fields to merge.
            let count = 2u64;
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("match")?;
            self.r#match.encode_cbor(buf)?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("contactIndex")?;
            crate::cbor::Encoder::new(&mut *buf).encode_i64(self.contact_index)?;
        } else {
            // Slow path: merge known fields with extra_cbor, sort, encode.
            let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
            {
                let mut vbuf = Vec::new();
                self.r#match.encode_cbor(&mut vbuf)?;
                pairs.push(("match", vbuf));
            }
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_i64(self.contact_index)?;
                pairs.push(("contactIndex", vbuf));
            }
            for (k, v) in &self.extra_cbor {
                pairs.push((k.as_str(), v.clone()));
            }
            pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
            for (k, v) in &pairs {
                crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
                buf.extend_from_slice(v);
            }
        }
        Ok(())
    }

    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> {
        let val = decoder.decode()?;
        let entries = match val {
            crate::cbor::Value::Map(entries) => entries,
            _ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
        };

        let mut field_match: Option<crate::api::app::bsky::ActorDefsProfileView> = None;
        let mut field_contact_index: Option<i64> = None;
        let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();

        for (key, value) in entries {
            match key {
                "match" => {
                    let raw = crate::cbor::encode_value(&value)?;
                    let mut dec = crate::cbor::Decoder::new(&raw);
                    field_match = Some(crate::api::app::bsky::ActorDefsProfileView::decode_cbor(
                        &mut dec,
                    )?);
                }
                "contactIndex" => match value {
                    crate::cbor::Value::Unsigned(n) => {
                        field_contact_index = Some(n as i64);
                    }
                    crate::cbor::Value::Signed(n) => {
                        field_contact_index = Some(n);
                    }
                    _ => {
                        return Err(crate::cbor::CborError::InvalidCbor(
                            "expected integer".into(),
                        ));
                    }
                },
                _ => {
                    let raw = crate::cbor::encode_value(&value)?;
                    extra_cbor.push((key.to_string(), raw));
                }
            }
        }

        Ok(ContactDefsMatchAndContactIndex {
            r#match: field_match.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'match'".into())
            })?,
            contact_index: field_contact_index.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'contactIndex'".into())
            })?,
            extra: std::collections::HashMap::new(),
            extra_cbor,
        })
    }
}

/// ContactDefsNotification — A stash object to be sent via bsync representing a notification to be created.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ContactDefsNotification {
    /// The DID of who this notification comes from.
    pub from: crate::syntax::Did,
    /// The DID of who this notification should go to.
    pub to: crate::syntax::Did,
    /// Extra fields not defined in the schema (JSON).
    #[serde(flatten)]
    pub extra: std::collections::HashMap<String, serde_json::Value>,
    /// Extra fields not defined in the schema (CBOR).
    #[serde(skip)]
    pub extra_cbor: Vec<(String, Vec<u8>)>,
}

impl ContactDefsNotification {
    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> {
        if self.extra_cbor.is_empty() {
            // Fast path: no extra fields to merge.
            let count = 2u64;
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("to")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(self.to.as_str())?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("from")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(self.from.as_str())?;
        } else {
            // Slow path: merge known fields with extra_cbor, sort, encode.
            let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(self.to.as_str())?;
                pairs.push(("to", vbuf));
            }
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(self.from.as_str())?;
                pairs.push(("from", vbuf));
            }
            for (k, v) in &self.extra_cbor {
                pairs.push((k.as_str(), v.clone()));
            }
            pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
            for (k, v) in &pairs {
                crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
                buf.extend_from_slice(v);
            }
        }
        Ok(())
    }

    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> {
        let val = decoder.decode()?;
        let entries = match val {
            crate::cbor::Value::Map(entries) => entries,
            _ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
        };

        let mut field_to: Option<crate::syntax::Did> = None;
        let mut field_from: Option<crate::syntax::Did> = None;
        let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();

        for (key, value) in entries {
            match key {
                "to" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_to = Some(
                            crate::syntax::Did::try_from(s)
                                .map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
                        );
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                "from" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_from = Some(
                            crate::syntax::Did::try_from(s)
                                .map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
                        );
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                _ => {
                    let raw = crate::cbor::encode_value(&value)?;
                    extra_cbor.push((key.to_string(), raw));
                }
            }
        }

        Ok(ContactDefsNotification {
            to: field_to.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'to'".into())
            })?,
            from: field_from.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'from'".into())
            })?,
            extra: std::collections::HashMap::new(),
            extra_cbor,
        })
    }
}

/// ContactDefsSyncStatus object from app.bsky.contact.defs.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ContactDefsSyncStatus {
    /// Number of existing contact matches resulting of the user imports and of their imported contacts having imported the user. Matches stop being counted when the user either follows the matched contact or dismisses the match.
    pub matches_count: i64,
    /// Last date when contacts where imported.
    pub synced_at: crate::syntax::Datetime,
    /// Extra fields not defined in the schema (JSON).
    #[serde(flatten)]
    pub extra: std::collections::HashMap<String, serde_json::Value>,
    /// Extra fields not defined in the schema (CBOR).
    #[serde(skip)]
    pub extra_cbor: Vec<(String, Vec<u8>)>,
}

impl ContactDefsSyncStatus {
    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> {
        if self.extra_cbor.is_empty() {
            // Fast path: no extra fields to merge.
            let count = 2u64;
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("syncedAt")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(self.synced_at.as_str())?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("matchesCount")?;
            crate::cbor::Encoder::new(&mut *buf).encode_i64(self.matches_count)?;
        } else {
            // Slow path: merge known fields with extra_cbor, sort, encode.
            let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(self.synced_at.as_str())?;
                pairs.push(("syncedAt", vbuf));
            }
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_i64(self.matches_count)?;
                pairs.push(("matchesCount", vbuf));
            }
            for (k, v) in &self.extra_cbor {
                pairs.push((k.as_str(), v.clone()));
            }
            pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
            for (k, v) in &pairs {
                crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
                buf.extend_from_slice(v);
            }
        }
        Ok(())
    }

    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> {
        let val = decoder.decode()?;
        let entries = match val {
            crate::cbor::Value::Map(entries) => entries,
            _ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
        };

        let mut field_synced_at: Option<crate::syntax::Datetime> = None;
        let mut field_matches_count: Option<i64> = None;
        let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();

        for (key, value) in entries {
            match key {
                "syncedAt" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_synced_at = Some(
                            crate::syntax::Datetime::try_from(s)
                                .map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
                        );
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                "matchesCount" => match value {
                    crate::cbor::Value::Unsigned(n) => {
                        field_matches_count = Some(n as i64);
                    }
                    crate::cbor::Value::Signed(n) => {
                        field_matches_count = Some(n);
                    }
                    _ => {
                        return Err(crate::cbor::CborError::InvalidCbor(
                            "expected integer".into(),
                        ));
                    }
                },
                _ => {
                    let raw = crate::cbor::encode_value(&value)?;
                    extra_cbor.push((key.to_string(), raw));
                }
            }
        }

        Ok(ContactDefsSyncStatus {
            synced_at: field_synced_at.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'syncedAt'".into())
            })?,
            matches_count: field_matches_count.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'matchesCount'".into())
            })?,
            extra: std::collections::HashMap::new(),
            extra_cbor,
        })
    }
}