Deserialize reply information

This may come in useful if I want to, you know, remove replies from the feed
This commit is contained in:
Aleksei Voronov 2023-10-15 17:27:43 +02:00
parent 892d600754
commit e1baeffc6e
1 changed files with 55 additions and 1 deletions

View File

@ -6,8 +6,9 @@ use crate::services::bluesky::internals::cbor::CborValue;
#[derive(Debug)]
pub struct PostRecord {
pub langs: Option<Vec<String>>,
pub text: String,
pub langs: Option<Vec<String>>,
pub reply: Option<ReplyRef>,
}
impl TryFrom<CborValue> for PostRecord {
@ -25,6 +26,59 @@ impl TryFrom<CborValue> for PostRecord {
.remove("langs")
.map(|value| value.try_into())
.transpose()?,
reply: map.remove("reply")
.map(|value| value.try_into())
.transpose()?,
})
}
}
#[derive(Debug)]
pub struct ReplyRef {
pub parent: Ref,
pub root: Ref,
}
impl TryFrom<CborValue> for ReplyRef {
type Error = Error;
fn try_from(root: CborValue) -> Result<Self> {
let mut map: HashMap<_, _> = root.try_into()?;
Ok(ReplyRef {
parent: map
.remove("parent")
.ok_or_else(|| anyhow!("Missing field: parent"))?
.try_into()?,
root: map
.remove("root")
.ok_or_else(|| anyhow!("Missing field: root"))?
.try_into()?,
})
}
}
#[derive(Debug)]
pub struct Ref {
pub cid: String,
pub uri: String,
}
impl TryFrom<CborValue> for Ref {
type Error = Error;
fn try_from(root: CborValue) -> Result<Self> {
let mut map: HashMap<_, _> = root.try_into()?;
Ok(Ref {
cid: map
.remove("cid")
.ok_or_else(|| anyhow!("Missing field: cid"))?
.try_into()?,
uri: map
.remove("uri")
.ok_or_else(|| anyhow!("Missing field: uri"))?
.try_into()?,
})
}
}