From 1032a51220753bf4fa8e023f6ad1dd83dae802ee Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Wed, 9 Aug 2023 14:59:39 -0700 Subject: [PATCH] refactor: clippy suggestions --- src/conn.rs | 10 +++++----- src/nauthz.rs | 2 +- src/repo/sqlite.rs | 14 ++++++-------- src/server.rs | 16 +++++++--------- src/utils.rs | 2 +- tests/integration_test.rs | 2 +- 6 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/conn.rs b/src/conn.rs index 28e6a65..2640d53 100644 --- a/src/conn.rs +++ b/src/conn.rs @@ -156,7 +156,7 @@ impl ClientConn { self.auth = Challenge(Uuid::new_v4().to_string()); } - pub fn authenticate(&mut self, event: &Event, relay_url: &String) -> Result<()> { + pub fn authenticate(&mut self, event: &Event, relay_url: &str) -> Result<()> { match &self.auth { Challenge(_) => (), AuthPubkey(_) => { @@ -181,15 +181,15 @@ impl ClientConn { return Err(Error::AuthFailure); } - let mut challenge: Option<&String> = None; - let mut relay: Option<&String> = None; + let mut challenge: Option<&str> = None; + let mut relay: Option<&str> = None; for tag in &event.tags { if tag.len() == 2 && tag.get(0) == Some(&"challenge".into()) { - challenge = tag.get(1); + challenge = tag.get(1).map(|x| x.as_str()); } if tag.len() == 2 && tag.get(0) == Some(&"relay".into()) { - relay = tag.get(1); + relay = tag.get(1).map(|x| x.as_str()); } } diff --git a/src/nauthz.rs b/src/nauthz.rs index d16ca80..2413a2b 100644 --- a/src/nauthz.rs +++ b/src/nauthz.rs @@ -41,7 +41,7 @@ impl std::convert::From for nauthz_grpc::event_request::Nip05Name { } // conversion of event tags into gprc struct -fn tags_to_protobuf(tags: &Vec>) -> Vec { +fn tags_to_protobuf(tags: &[Vec]) -> Vec { tags.iter() .map(|x| TagEntry { values: x.clone() }) .collect() diff --git a/src/repo/sqlite.rs b/src/repo/sqlite.rs index 7f3166f..c297847 100644 --- a/src/repo/sqlite.rs +++ b/src/repo/sqlite.rs @@ -1072,8 +1072,6 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec>, Option = @@ -1082,16 +1080,16 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec>, Option= {}", f.since.unwrap()); + let since_clause = if f.since.is_some() { + format!("AND created_at >= {}", f.since.unwrap()) } else { - since_clause = String::new(); + String::new() }; // Query for timestamp - if f.until.is_some() { - until_clause = format!("AND created_at <= {}", f.until.unwrap()); + let until_clause = if f.until.is_some() { + format!("AND created_at <= {}", f.until.unwrap()) } else { - until_clause = String::new(); + String::new() }; let tag_clause = format!( diff --git a/src/server.rs b/src/server.rs index e3318af..3c2b9f2 100644 --- a/src/server.rs +++ b/src/server.rs @@ -30,6 +30,8 @@ use hyper::upgrade::Upgraded; use hyper::{ header, server::conn::AddrStream, upgrade, Body, Request, Response, Server, StatusCode, }; +use nostr::key::FromPkStr; +use nostr::key::Keys; use prometheus::IntCounterVec; use prometheus::IntGauge; use prometheus::{Encoder, Histogram, HistogramOpts, IntCounter, Opts, Registry, TextEncoder}; @@ -60,8 +62,6 @@ use tungstenite::error::Error as WsError; use tungstenite::handshake; use tungstenite::protocol::Message; use tungstenite::protocol::WebSocketConfig; -use nostr::key::FromPkStr; -use nostr::key::Keys; /// Handle arbitrary HTTP requests, including for `WebSocket` upgrades. #[allow(clippy::too_many_arguments)] @@ -1029,7 +1029,7 @@ fn make_notice_message(notice: &Notice) -> Message { Message::text(json.to_string()) } -fn allowed_to_send(event_str: &String, conn: &conn::ClientConn, settings: &Settings) -> bool { +fn allowed_to_send(event_str: &str, conn: &conn::ClientConn, settings: &Settings) -> bool { // TODO: pass in kind so that we can avoid deserialization for most events if settings.authorization.nip42_dms { match serde_json::from_str::(event_str) { @@ -1038,16 +1038,14 @@ fn allowed_to_send(event_str: &String, conn: &conn::ClientConn, settings: &Setti match (conn.auth_pubkey(), event.tag_values_by_name("p").first()) { (Some(auth_pubkey), Some(recipient_pubkey)) => { recipient_pubkey == auth_pubkey || &event.pubkey == auth_pubkey - }, - (_, _) => { - false - }, + } + (_, _) => false, } } else { true } - }, - Err(_) => false + } + Err(_) => false, } } else { true diff --git a/src/utils.rs b/src/utils.rs index b70403e..eae6846 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -37,7 +37,7 @@ pub fn is_lower_hex(s: &str) -> bool { }) } -pub fn host_str(url: &String) -> Option { +pub fn host_str(url: &str) -> Option { Url::parse(url) .ok() .and_then(|u| u.host_str().map(|s| s.to_string())) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index ebe55c3..f65f4e8 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -73,7 +73,7 @@ async fn publish_test() -> Result<()> { let event_sub = r#"["REQ", "simple", {}]"#; sub_ws.send(event_sub.into()).await?; // read from subscription - let ws_next = sub_ws.next().await; + let _ws_next = sub_ws.next().await; let _res = relay.shutdown_tx.send(()); Ok(()) }