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 ++++++++++++------------------ src/lib.rs | 4 ++-- src/main.rs | 3 +-- src/micropub/post.rs | 16 ++++++++-------- 5 files changed, 27 insertions(+), 34 deletions(-) (limited to 'src') 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 { diff --git a/src/lib.rs b/src/lib.rs index 219aec6..99d74b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,7 +57,7 @@ where }, Err(err) => match err.kind() { database::ErrorKind::PermissionDenied => { - if let Some(_) = query.user { + if query.user.is_some() { Ok(Response::builder(403).build()) } else { Ok(Response::builder(401).build()) @@ -68,7 +68,7 @@ where } }); - return app + app } pub async fn get_app_with_redis(token_endpoint: surf::Url, redis_uri: String, media_endpoint: Option) -> App { diff --git a/src/main.rs b/src/main.rs index 3d0831e..778aa4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ use std::env; use log::{error,info,debug}; -use env_logger; use surf::Url; use kittybox_micropub as micropub; @@ -42,7 +41,7 @@ async fn main() -> Result<(), std::io::Error> { } let media_endpoint: Option = env::var("MEDIA_ENDPOINT").ok(); - let host = env::var("SERVE_AT").ok().unwrap_or("0.0.0.0:8080".to_string()); + let host = env::var("SERVE_AT").ok().unwrap_or_else(|| "0.0.0.0:8080".to_string()); let app = micropub::get_app_with_redis(token_endpoint, redis_uri, media_endpoint).await; app.listen(host).await } \ No newline at end of file diff --git a/src/micropub/post.rs b/src/micropub/post.rs index 7ec3566..37cbe26 100644 --- a/src/micropub/post.rs +++ b/src/micropub/post.rs @@ -40,7 +40,7 @@ fn get_folder_from_type(post_type: &str) -> String { }).to_string() } -fn normalize_mf2<'a>(mut body: serde_json::Value, user: &User) -> (String, serde_json::Value) { +fn normalize_mf2(mut body: serde_json::Value, user: &User) -> (String, serde_json::Value) { // Normalize the MF2 object here. let me = &user.me; let published: DateTime; @@ -91,7 +91,7 @@ fn normalize_mf2<'a>(mut body: serde_json::Value, user: &User) -> (String, serde match body["properties"]["url"].as_array_mut() { Some(array) => { if !array.iter().any(|i| i.as_str().unwrap_or("") == uid) { - array.push(serde_json::Value::String(uid.to_string())) + array.push(serde_json::Value::String(uid)) } } None => { @@ -126,7 +126,7 @@ fn normalize_mf2<'a>(mut body: serde_json::Value, user: &User) -> (String, serde body["properties"]["channel"] = json!([default_channel]); } body["properties"]["posted-with"] = json!([user.client_id]); - if let None = body["properties"]["author"][0].as_str() { + if body["properties"]["author"][0].as_str().is_none() { body["properties"]["author"] = json!([me.as_str()]) } // TODO: maybe highlight #hashtags? @@ -168,7 +168,7 @@ async fn new_post(req: Request>, body: serde_jso for channel in post["properties"]["channel"] .as_array().unwrap().iter() .map(|i| i.as_str().unwrap_or("").to_string()) - .filter(|i| i != "") + .filter(|i| !i.is_empty()) .collect::>() { let default_channel = user.me.join(DEFAULT_CHANNEL_PATH).unwrap().to_string(); @@ -243,9 +243,9 @@ async fn process_json(req: Request>, body: serde return error_json!(400, "invalid_request", "This action is not supported.") } } - } else if let Some(_) = body["type"][0].as_str() { + } else if body["type"][0].is_string() { // This is definitely an h-entry or something similar. Check if it has properties? - if let Some(_) = body["properties"].as_object() { + if body["properties"].is_object() { // Ok, this is definitely a new h-entry. Let's save it. return new_post(req, body).await } else { @@ -269,10 +269,10 @@ fn convert_form_to_mf2_json(form: Vec<(String, String)>) -> serde_json::Value { } } } - if mf2["type"].as_array().unwrap().len() == 0 { + if mf2["type"].as_array().unwrap().is_empty() { mf2["type"].as_array_mut().unwrap().push(json!("h-entry")); } - return mf2 + mf2 } async fn process_form(req: Request>, form: Vec<(String, String)>) -> Result { -- cgit 1.4.1