diff --git a/src/processes/post_indexer.rs b/src/processes/post_indexer.rs index 4a7665c..4bc9bdb 100644 --- a/src/processes/post_indexer.rs +++ b/src/processes/post_indexer.rs @@ -82,7 +82,7 @@ impl CommitProcessor for PostIndexer { Operation::DeletePost { uri } => { info!("Received a post to delete: {uri}"); - self.database.delete_post(&uri).await?; + self.database.delete_post(uri).await?; } _ => continue, } diff --git a/src/services/bluesky.rs b/src/services/bluesky.rs index a0cf0a7..e8ea666 100644 --- a/src/services/bluesky.rs +++ b/src/services/bluesky.rs @@ -1,7 +1,7 @@ mod client; +mod decode; mod proto; mod streaming; -mod decode; pub use client::Bluesky; pub use streaming::{CommitDetails, CommitProcessor, Operation}; diff --git a/src/services/bluesky/decode.rs b/src/services/bluesky/decode.rs index b8fb4bd..ffeb2b5 100644 --- a/src/services/bluesky/decode.rs +++ b/src/services/bluesky/decode.rs @@ -1,11 +1,11 @@ +use anyhow::{anyhow, Error, Result}; use sk_cbor::Value; -use anyhow::{Result, Error, anyhow}; type CborMap = Vec<(Value, Value)>; pub struct PostRecord { pub langs: Option>, - pub text: String + pub text: String, } impl TryFrom<&CborMap> for PostRecord { @@ -15,16 +15,18 @@ impl TryFrom<&CborMap> for PostRecord { let mut text: Option<&str> = None; let mut langs: Option> = None; - for (key, value) in iter_string_keys(&root) { + for (key, value) in iter_string_keys(root) { match key { - "text" => { text = Some(string(value)?) }, - "langs" => { langs = Some(array_of_strings(value)?) }, + "text" => text = Some(string(value)?), + "langs" => langs = Some(array_of_strings(value)?), _ => continue, } } Ok(PostRecord { - text: text.ok_or_else(|| anyhow!("Missing field: text"))?.to_owned(), + text: text + .ok_or_else(|| anyhow!("Missing field: text"))? + .to_owned(), langs: langs.map(|v| v.into_iter().map(str::to_owned).collect()), }) } @@ -40,15 +42,15 @@ impl TryFrom<&CborMap> for LikeRecord { fn try_from(root: &CborMap) -> Result { let mut subject = None; - for (key, value) in iter_string_keys(&root) { + for (key, value) in iter_string_keys(root) { match key { - "subject" => { subject = Some(map(value)?.try_into()?) }, + "subject" => subject = Some(map(value)?.try_into()?), _ => continue, } } - Ok(LikeRecord { - subject: subject.ok_or_else(|| anyhow!("Missing field: subject"))? + Ok(LikeRecord { + subject: subject.ok_or_else(|| anyhow!("Missing field: subject"))?, }) } } @@ -65,10 +67,10 @@ impl TryFrom<&CborMap> for Subject { let mut cid = None; let mut uri = None; - for (key, value) in iter_string_keys(&root) { + for (key, value) in iter_string_keys(root) { match key { - "cid" => { cid = Some(string(value)?) }, - "uri" => { uri = Some(string(value)?) }, + "cid" => cid = Some(string(value)?), + "uri" => uri = Some(string(value)?), _ => continue, } } @@ -90,15 +92,17 @@ impl TryFrom<&CborMap> for FollowRecord { fn try_from(root: &CborMap) -> Result { let mut subject = None; - for (key, value) in iter_string_keys(&root) { + for (key, value) in iter_string_keys(root) { match key { - "subject" => { subject = Some(string(value)?) }, + "subject" => subject = Some(string(value)?), _ => continue, } } Ok(FollowRecord { - subject: subject.ok_or_else(|| anyhow!("Missing field: subject"))?.to_owned(), + subject: subject + .ok_or_else(|| anyhow!("Missing field: subject"))? + .to_owned(), }) } } @@ -106,7 +110,7 @@ impl TryFrom<&CborMap> for FollowRecord { pub fn read_record TryFrom<&'a CborMap, Error = Error>>(bytes: &[u8]) -> Result { let root = match sk_cbor::read(bytes) { Err(_) => return Err(anyhow!("Could not decode anything")), - Ok(v) => v + Ok(v) => v, }; let root_map = match root { @@ -118,11 +122,9 @@ pub fn read_record TryFrom<&'a CborMap, Error = Error>>(bytes: &[u8]) } fn iter_string_keys(map: &CborMap) -> impl Iterator { - map.into_iter().flat_map(|(k, v)| { - match k { - Value::TextString(k) => Some((k.as_str(), v)), - _ => None - } + map.iter().flat_map(|(k, v)| match k { + Value::TextString(k) => Some((k.as_str(), v)), + _ => None, }) } @@ -136,7 +138,7 @@ fn map(value: &Value) -> Result<&CborMap> { fn string(value: &Value) -> Result<&str> { match value { Value::TextString(value) => Ok(value.as_str()), - _ => Err(anyhow!("Expected string")) + _ => Err(anyhow!("Expected string")), } } @@ -145,10 +147,10 @@ fn array_of_strings(value: &Value) -> Result> { Value::Array(vec) => { let mut res = Vec::with_capacity(vec.len()); for vec_value in vec { - res.push(string(&vec_value)?) + res.push(string(vec_value)?) } Ok(res) } - _ => Err(anyhow!("Expected array")) + _ => Err(anyhow!("Expected array")), } } diff --git a/src/services/bluesky/streaming.rs b/src/services/bluesky/streaming.rs index 00ff1b2..8ba0084 100644 --- a/src/services/bluesky/streaming.rs +++ b/src/services/bluesky/streaming.rs @@ -4,7 +4,10 @@ use anyhow::Result; use async_trait::async_trait; use atrium_api::com::atproto::sync::subscribe_repos::{Commit, Message}; -use super::{proto::Frame, decode::{read_record, PostRecord, LikeRecord, FollowRecord}}; +use super::{ + decode::{read_record, FollowRecord, LikeRecord, PostRecord}, + proto::Frame, +}; const COLLECTION_POST: &str = "app.bsky.feed.post"; const COLLECTION_LIKE: &str = "app.bsky.feed.like"; @@ -109,7 +112,7 @@ async fn extract_operations(commit: &Commit) -> Result> { match collection { COLLECTION_POST => { - let record: PostRecord = read_record(&block)?; + let record: PostRecord = read_record(block)?; Operation::CreatePost { author_did: commit.repo.clone(), @@ -120,7 +123,7 @@ async fn extract_operations(commit: &Commit) -> Result> { } } COLLECTION_LIKE => { - let record: LikeRecord = read_record(&block)?; + let record: LikeRecord = read_record(block)?; Operation::CreateLike { author_did: commit.repo.clone(), @@ -131,7 +134,7 @@ async fn extract_operations(commit: &Commit) -> Result> { } } COLLECTION_FOLLOW => { - let record: FollowRecord = read_record(&block)?; + let record: FollowRecord = read_record(block)?; Operation::CreateFollow { author_did: commit.repo.clone(),