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 } => {
info!("Received a post to delete: {uri}");
self.database.delete_post(&uri).await?;
self.database.delete_post(uri).await?;
}
_ => continue,
}

View File

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

View File

@ -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<Vec<String>>,
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<Vec<&str>> = 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<Self> {
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<Self> {
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<T: for<'a> TryFrom<&'a CborMap, Error = Error>>(bytes: &[u8]) -> Result<T> {
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<T: for<'a> TryFrom<&'a CborMap, Error = Error>>(bytes: &[u8])
}
fn iter_string_keys(map: &CborMap) -> impl Iterator<Item = (&str, &Value)> {
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<Vec<&str>> {
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")),
}
}

View File

@ -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<Vec<Operation>> {
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<Vec<Operation>> {
}
}
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<Vec<Operation>> {
}
}
COLLECTION_FOLLOW => {
let record: FollowRecord = read_record(&block)?;
let record: FollowRecord = read_record(block)?;
Operation::CreateFollow {
author_did: commit.repo.clone(),