Formatting and clippy

This commit is contained in:
Aleksei Voronov 2023-09-24 20:27:51 +02:00
parent 0cd3202a9c
commit fadf882a1f
4 changed files with 36 additions and 31 deletions

View File

@ -82,7 +82,7 @@ impl CommitProcessor for PostIndexer {
Operation::DeletePost { uri } => { Operation::DeletePost { uri } => {
info!("Received a post to delete: {uri}"); info!("Received a post to delete: {uri}");
self.database.delete_post(&uri).await?; self.database.delete_post(uri).await?;
} }
_ => continue, _ => continue,
} }

View File

@ -1,7 +1,7 @@
mod client; mod client;
mod decode;
mod proto; mod proto;
mod streaming; mod streaming;
mod decode;
pub use client::Bluesky; pub use client::Bluesky;
pub use streaming::{CommitDetails, CommitProcessor, Operation}; pub use streaming::{CommitDetails, CommitProcessor, Operation};

View File

@ -1,11 +1,11 @@
use anyhow::{anyhow, Error, Result};
use sk_cbor::Value; use sk_cbor::Value;
use anyhow::{Result, Error, anyhow};
type CborMap = Vec<(Value, Value)>; type CborMap = Vec<(Value, Value)>;
pub struct PostRecord { pub struct PostRecord {
pub langs: Option<Vec<String>>, pub langs: Option<Vec<String>>,
pub text: String pub text: String,
} }
impl TryFrom<&CborMap> for PostRecord { impl TryFrom<&CborMap> for PostRecord {
@ -15,16 +15,18 @@ impl TryFrom<&CborMap> for PostRecord {
let mut text: Option<&str> = None; let mut text: Option<&str> = None;
let mut langs: Option<Vec<&str>> = None; let mut langs: Option<Vec<&str>> = None;
for (key, value) in iter_string_keys(&root) { for (key, value) in iter_string_keys(root) {
match key { match key {
"text" => { text = Some(string(value)?) }, "text" => text = Some(string(value)?),
"langs" => { langs = Some(array_of_strings(value)?) }, "langs" => langs = Some(array_of_strings(value)?),
_ => continue, _ => continue,
} }
} }
Ok(PostRecord { 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()), 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<Self> { fn try_from(root: &CborMap) -> Result<Self> {
let mut subject = None; let mut subject = None;
for (key, value) in iter_string_keys(&root) { for (key, value) in iter_string_keys(root) {
match key { match key {
"subject" => { subject = Some(map(value)?.try_into()?) }, "subject" => subject = Some(map(value)?.try_into()?),
_ => continue, _ => continue,
} }
} }
Ok(LikeRecord { Ok(LikeRecord {
subject: subject.ok_or_else(|| anyhow!("Missing field: subject"))? subject: subject.ok_or_else(|| anyhow!("Missing field: subject"))?,
}) })
} }
} }
@ -65,10 +67,10 @@ impl TryFrom<&CborMap> for Subject {
let mut cid = None; let mut cid = None;
let mut uri = None; let mut uri = None;
for (key, value) in iter_string_keys(&root) { for (key, value) in iter_string_keys(root) {
match key { match key {
"cid" => { cid = Some(string(value)?) }, "cid" => cid = Some(string(value)?),
"uri" => { uri = Some(string(value)?) }, "uri" => uri = Some(string(value)?),
_ => continue, _ => continue,
} }
} }
@ -90,15 +92,17 @@ impl TryFrom<&CborMap> for FollowRecord {
fn try_from(root: &CborMap) -> Result<Self> { fn try_from(root: &CborMap) -> Result<Self> {
let mut subject = None; let mut subject = None;
for (key, value) in iter_string_keys(&root) { for (key, value) in iter_string_keys(root) {
match key { match key {
"subject" => { subject = Some(string(value)?) }, "subject" => subject = Some(string(value)?),
_ => continue, _ => continue,
} }
} }
Ok(FollowRecord { 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<T: for<'a> TryFrom<&'a CborMap, Error = Error>>(bytes: &[u8]) -> Result<T> { pub fn read_record<T: for<'a> TryFrom<&'a CborMap, Error = Error>>(bytes: &[u8]) -> Result<T> {
let root = match sk_cbor::read(bytes) { let root = match sk_cbor::read(bytes) {
Err(_) => return Err(anyhow!("Could not decode anything")), Err(_) => return Err(anyhow!("Could not decode anything")),
Ok(v) => v Ok(v) => v,
}; };
let root_map = match root { let root_map = match root {
@ -118,11 +122,9 @@ pub fn read_record<T: for<'a> TryFrom<&'a CborMap, Error = Error>>(bytes: &[u8])
} }
fn iter_string_keys(map: &CborMap) -> impl Iterator<Item = (&str, &Value)> { fn iter_string_keys(map: &CborMap) -> impl Iterator<Item = (&str, &Value)> {
map.into_iter().flat_map(|(k, v)| { map.iter().flat_map(|(k, v)| match k {
match k { Value::TextString(k) => Some((k.as_str(), v)),
Value::TextString(k) => Some((k.as_str(), v)), _ => None,
_ => None
}
}) })
} }
@ -136,7 +138,7 @@ fn map(value: &Value) -> Result<&CborMap> {
fn string(value: &Value) -> Result<&str> { fn string(value: &Value) -> Result<&str> {
match value { match value {
Value::TextString(value) => Ok(value.as_str()), 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<Vec<&str>> {
Value::Array(vec) => { Value::Array(vec) => {
let mut res = Vec::with_capacity(vec.len()); let mut res = Vec::with_capacity(vec.len());
for vec_value in vec { for vec_value in vec {
res.push(string(&vec_value)?) res.push(string(vec_value)?)
} }
Ok(res) Ok(res)
} }
_ => Err(anyhow!("Expected array")) _ => Err(anyhow!("Expected array")),
} }
} }

View File

@ -4,7 +4,10 @@ use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use atrium_api::com::atproto::sync::subscribe_repos::{Commit, Message}; 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_POST: &str = "app.bsky.feed.post";
const COLLECTION_LIKE: &str = "app.bsky.feed.like"; const COLLECTION_LIKE: &str = "app.bsky.feed.like";
@ -109,7 +112,7 @@ async fn extract_operations(commit: &Commit) -> Result<Vec<Operation>> {
match collection { match collection {
COLLECTION_POST => { COLLECTION_POST => {
let record: PostRecord = read_record(&block)?; let record: PostRecord = read_record(block)?;
Operation::CreatePost { Operation::CreatePost {
author_did: commit.repo.clone(), author_did: commit.repo.clone(),
@ -120,7 +123,7 @@ async fn extract_operations(commit: &Commit) -> Result<Vec<Operation>> {
} }
} }
COLLECTION_LIKE => { COLLECTION_LIKE => {
let record: LikeRecord = read_record(&block)?; let record: LikeRecord = read_record(block)?;
Operation::CreateLike { Operation::CreateLike {
author_did: commit.repo.clone(), author_did: commit.repo.clone(),
@ -131,7 +134,7 @@ async fn extract_operations(commit: &Commit) -> Result<Vec<Operation>> {
} }
} }
COLLECTION_FOLLOW => { COLLECTION_FOLLOW => {
let record: FollowRecord = read_record(&block)?; let record: FollowRecord = read_record(block)?;
Operation::CreateFollow { Operation::CreateFollow {
author_did: commit.repo.clone(), author_did: commit.repo.clone(),