From a754477a02f6c163c02c9634d8c9d5cd912d572c Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Fri, 2 Sep 2022 12:26:00 -0500 Subject: [PATCH] improvement: misc refactorings (clippy) --- src/event.rs | 10 ++++------ src/main.rs | 30 ++++++++++++++++++++++-------- src/schema.rs | 2 +- src/subscription.rs | 4 ++-- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/event.rs b/src/event.rs index 34e4c08..58dfb5b 100644 --- a/src/event.rs +++ b/src/event.rs @@ -53,7 +53,7 @@ where D: Deserializer<'de>, { let opt = Option::deserialize(deserializer)?; - Ok(opt.unwrap_or_else(Vec::new)) + Ok(opt.unwrap_or_default()) } /// Attempt to form a single-char tag name. @@ -62,7 +62,7 @@ pub fn single_char_tagname(tagname: &str) -> Option { // of a single char. let mut tagnamechars = tagname.chars(); let firstchar = tagnamechars.next(); - return match firstchar { + match firstchar { Some(_) => { // check second char if tagnamechars.next().is_none() { @@ -72,7 +72,7 @@ pub fn single_char_tagname(tagname: &str) -> Option { } } None => None, - }; + } } /// Convert network event to parsed/validated event. @@ -129,9 +129,7 @@ impl Event { let tagnamechar = tagnamechar_opt.unwrap(); let tagval = t.get(1).unwrap(); // ensure a vector exists for this tag - if !idx.contains_key(&tagnamechar) { - idx.insert(tagnamechar.clone(), HashSet::new()); - } + idx.entry(tagnamechar).or_insert_with(HashSet::new); // get the tag vec and insert entry let tidx = idx.get_mut(&tagnamechar).expect("could not get tag vector"); tidx.insert(tagval.clone()); diff --git a/src/main.rs b/src/main.rs index fcb6b9c..6a47dc1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -229,17 +229,31 @@ fn main() -> Result<(), Error> { .unwrap(); // start tokio rt.block_on(async { - let settings = config::SETTINGS.read().unwrap(); + let broadcast_buffer_limit; + let persist_buffer_limit; + let verified_users_active; + let db_min_conn; + let db_max_conn; + // hack to prove we drop the mutexguard prior to any await points + // (https://github.com/rust-lang/rust-clippy/issues/6446) + { + let settings = config::SETTINGS.read().unwrap(); + broadcast_buffer_limit = settings.limits.broadcast_buffer; + persist_buffer_limit = settings.limits.event_persist_buffer; + verified_users_active = settings.verified_users.is_active(); + db_min_conn = settings.database.min_conn; + db_max_conn = settings.database.max_conn; + } info!("listening on: {}", socket_addr); // all client-submitted valid events are broadcast to every // other client on this channel. This should be large enough // to accomodate slower readers (messages are dropped if // clients can not keep up). - let (bcast_tx, _) = broadcast::channel::(settings.limits.broadcast_buffer); + let (bcast_tx, _) = broadcast::channel::(broadcast_buffer_limit); // validated events that need to be persisted are sent to the // database on via this channel. let (event_tx, event_rx) = - mpsc::channel::(settings.limits.event_persist_buffer); + mpsc::channel::(persist_buffer_limit); // establish a channel for letting all threads now about a // requested server shutdown. let (invoke_shutdown, shutdown_listen) = broadcast::channel::<()>(1); @@ -267,7 +281,7 @@ fn main() -> Result<(), Error> { // create a nip-05 verifier thread let verifier_opt = nip05::Verifier::new(metadata_rx, bcast_tx.clone()); if let Ok(mut v) = verifier_opt { - if settings.verified_users.is_active() { + if verified_users_active { tokio::task::spawn(async move { info!("starting up NIP-05 verifier..."); v.run().await; @@ -286,8 +300,8 @@ fn main() -> Result<(), Error> { "client query", rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY | rusqlite::OpenFlags::SQLITE_OPEN_SHARED_CACHE, - settings.database.min_conn, - settings.database.max_conn, + db_min_conn, + db_max_conn, true, ); // A `Service` is needed for every connection, so this @@ -428,7 +442,7 @@ async fn nostr_server( }, Some(query_result) = query_rx.recv() => { // database informed us of a query result we asked for - let subesc = query_result.sub_id.replace("\"", ""); + let subesc = query_result.sub_id.replace('"', ""); if query_result.event == "EOSE" { let send_str = format!("[\"EOSE\",\"{}\"]", subesc); ws_stream.send(Message::Text(send_str)).await.ok(); @@ -452,7 +466,7 @@ async fn nostr_server( cid, s, global_event.get_event_id_prefix()); // create an event response and send it - let subesc = s.replace("\"", ""); + let subesc = s.replace('"', ""); ws_stream.send(Message::Text(format!("[\"EVENT\",\"{}\",{}]", subesc, event_str))).await.ok(); //nostr_stream.send(res).await.ok(); } else { diff --git a/src/schema.rs b/src/schema.rs index 51e54ea..987b6dd 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -252,7 +252,7 @@ PRAGMA user_version=5; let tagval = t.get(1).unwrap(); // insert as BLOB if we can restore it losslessly. // this means it needs to be even length and lowercase. - if (tagval.len() % 2 == 0) && is_lower_hex(&tagval) { + if (tagval.len() % 2 == 0) && is_lower_hex(tagval) { tx.execute( "INSERT INTO tag (event_id, name, value_hex) VALUES (?1, ?2, ?3);", params![event_id, tagname, hex::decode(&tagval).ok()], diff --git a/src/subscription.rs b/src/subscription.rs index 3b64b0b..4415097 100644 --- a/src/subscription.rs +++ b/src/subscription.rs @@ -110,7 +110,7 @@ fn tag_search_char_from_filter(tagname: &str) -> Option { // of a single char. let mut tagnamechars = tagname_nohash.chars(); let firstchar = tagnamechars.next(); - return match firstchar { + match firstchar { Some(_) => { // check second char if tagnamechars.next().is_none() { @@ -120,7 +120,7 @@ fn tag_search_char_from_filter(tagname: &str) -> Option { } } None => None, - }; + } } impl<'de> Deserialize<'de> for Subscription {