From 5b65175710a19daf43c81e52e180fd66ecfe4353 Mon Sep 17 00:00:00 2001 From: Vika Date: Tue, 4 May 2021 23:12:59 +0300 Subject: Clippy lints --- src/database/mod.rs | 8 ++++---- src/database/redis/mod.rs | 30 ++++++++++++------------------ 2 files changed, 16 insertions(+), 22 deletions(-) (limited to 'src/database') diff --git a/src/database/mod.rs b/src/database/mod.rs index 1708cff..f0a1e4b 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -22,7 +22,7 @@ pub struct MicropubChannel { pub enum ErrorKind { Backend, PermissionDenied, - JSONParsing, + JsonParsing, NotFound, BadRequest, Other @@ -62,7 +62,7 @@ impl From for StorageError { Self { msg: format!("{}", err), source: Some(Box::new(err)), - kind: ErrorKind::JSONParsing + kind: ErrorKind::JsonParsing } } } @@ -70,7 +70,7 @@ impl std::fmt::Display for StorageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match match self.kind { ErrorKind::Backend => write!(f, "backend error: "), - ErrorKind::JSONParsing => write!(f, "error while parsing JSON: "), + ErrorKind::JsonParsing => write!(f, "error while parsing JSON: "), ErrorKind::PermissionDenied => write!(f, "permission denied: "), ErrorKind::NotFound => write!(f, "not found: "), ErrorKind::BadRequest => write!(f, "bad request: "), @@ -89,7 +89,7 @@ impl serde::Serialize for StorageError { impl StorageError { /// Create a new StorageError of an ErrorKind with a message. fn new(kind: ErrorKind, msg: &str) -> Self { - return StorageError { + StorageError { msg: msg.to_string(), source: None, kind diff --git a/src/database/redis/mod.rs b/src/database/redis/mod.rs index faf9c13..5ab93af 100644 --- a/src/database/redis/mod.rs +++ b/src/database/redis/mod.rs @@ -4,7 +4,6 @@ use futures_util::StreamExt; use futures::stream; use lazy_static::lazy_static; use log::error; -use redis; use redis::AsyncCommands; use serde_json::json; @@ -37,7 +36,7 @@ pub struct RedisStorage { redis: redis::Client, } -fn filter_post<'a>(mut post: serde_json::Value, user: &'a Option) -> Option { +fn filter_post(mut post: serde_json::Value, user: &'_ Option) -> Option { if post["properties"]["deleted"][0].is_string() { return Some(json!({ "type": post["type"], @@ -50,10 +49,8 @@ fn filter_post<'a>(mut post: serde_json::Value, user: &'a Option) -> Opt let author = post["properties"]["author"].as_array().unwrap_or(&empty_vec).iter().map(|i| i.as_str().unwrap().to_string()); let visibility = post["properties"]["visibility"][0].as_str().unwrap_or("public"); let mut audience = author.chain(post["properties"]["audience"].as_array().unwrap_or(&empty_vec).iter().map(|i| i.as_str().unwrap().to_string())); - if visibility == "private" { - if !audience.any(|i| Some(i) == *user) { - return None - } + if (visibility == "private" && !audience.any(|i| Some(i) == *user)) || (visibility == "protected" && user.is_none()) { + return None } if post["properties"]["location"].is_array() { let location_visibility = post["properties"]["location-visibility"][0].as_str().unwrap_or("private"); @@ -147,27 +144,24 @@ impl Storage for RedisStorage { let key: &str; match post["properties"]["uid"][0].as_str() { Some(uid) => key = uid, - None => return Err(StorageError::new(ErrorKind::Other, "post doesn't have a UID")) + None => return Err(StorageError::new(ErrorKind::BadRequest, "post doesn't have a UID")) } - match conn.hset::<&str, &str, String, ()>(&"posts", key, post.to_string()).await { - Err(err) => return Err(err.into()), - _ => {} + if let Err(err) = conn.hset::<&str, &str, String, ()>(&"posts", key, post.to_string()).await { + return Err(err.into()) } if post["properties"]["url"].is_array() { for url in post["properties"]["url"].as_array().unwrap().iter().map(|i| i.as_str().unwrap().to_string()) { - if &url != key { - match conn.hset::<&str, &str, String, ()>(&"posts", &url, json!({"see_other": key}).to_string()).await { - Err(err) => return Err(err.into()), - _ => {} + if url != key { + if let Err(err) = conn.hset::<&str, &str, String, ()>(&"posts", &url, json!({"see_other": key}).to_string()).await { + return Err(err.into()) } } } } if post["type"].as_array().unwrap().iter().any(|i| i == "h-feed") { // This is a feed. Add it to the channels array if it's not already there. - match conn.sadd::("channels_".to_string() + post["properties"]["author"][0].as_str().unwrap(), key).await { - Err(err) => return Err(err.into()), - _ => {}, + if let Err(err) = conn.sadd::("channels_".to_string() + post["properties"]["author"][0].as_str().unwrap(), key).await { + return Err(err.into()) } } Ok(()) @@ -258,7 +252,7 @@ impl Storage for RedisStorage { // Broken links return None, and Stream::filter_map skips all Nones. .filter_map(|post: Option| async move { post }) .filter_map(|post| async move { - return filter_post(post, user) + filter_post(post, user) }) .take(limit); match std::panic::AssertUnwindSafe(posts.collect::>()).catch_unwind().await { -- cgit 1.4.1