improvement: misc refactorings (clippy)

This commit is contained in:
Greg Heartsfield 2022-09-02 12:26:00 -05:00
parent a843eaa939
commit a754477a02
4 changed files with 29 additions and 17 deletions

View File

@ -53,7 +53,7 @@ where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let opt = Option::deserialize(deserializer)?; 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. /// Attempt to form a single-char tag name.
@ -62,7 +62,7 @@ pub fn single_char_tagname(tagname: &str) -> Option<char> {
// of a single char. // of a single char.
let mut tagnamechars = tagname.chars(); let mut tagnamechars = tagname.chars();
let firstchar = tagnamechars.next(); let firstchar = tagnamechars.next();
return match firstchar { match firstchar {
Some(_) => { Some(_) => {
// check second char // check second char
if tagnamechars.next().is_none() { if tagnamechars.next().is_none() {
@ -72,7 +72,7 @@ pub fn single_char_tagname(tagname: &str) -> Option<char> {
} }
} }
None => None, None => None,
}; }
} }
/// Convert network event to parsed/validated event. /// Convert network event to parsed/validated event.
@ -129,9 +129,7 @@ impl Event {
let tagnamechar = tagnamechar_opt.unwrap(); let tagnamechar = tagnamechar_opt.unwrap();
let tagval = t.get(1).unwrap(); let tagval = t.get(1).unwrap();
// ensure a vector exists for this tag // ensure a vector exists for this tag
if !idx.contains_key(&tagnamechar) { idx.entry(tagnamechar).or_insert_with(HashSet::new);
idx.insert(tagnamechar.clone(), HashSet::new());
}
// get the tag vec and insert entry // get the tag vec and insert entry
let tidx = idx.get_mut(&tagnamechar).expect("could not get tag vector"); let tidx = idx.get_mut(&tagnamechar).expect("could not get tag vector");
tidx.insert(tagval.clone()); tidx.insert(tagval.clone());

View File

@ -229,17 +229,31 @@ fn main() -> Result<(), Error> {
.unwrap(); .unwrap();
// start tokio // start tokio
rt.block_on(async { rt.block_on(async {
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(); 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); info!("listening on: {}", socket_addr);
// all client-submitted valid events are broadcast to every // all client-submitted valid events are broadcast to every
// other client on this channel. This should be large enough // other client on this channel. This should be large enough
// to accomodate slower readers (messages are dropped if // to accomodate slower readers (messages are dropped if
// clients can not keep up). // clients can not keep up).
let (bcast_tx, _) = broadcast::channel::<Event>(settings.limits.broadcast_buffer); let (bcast_tx, _) = broadcast::channel::<Event>(broadcast_buffer_limit);
// validated events that need to be persisted are sent to the // validated events that need to be persisted are sent to the
// database on via this channel. // database on via this channel.
let (event_tx, event_rx) = let (event_tx, event_rx) =
mpsc::channel::<SubmittedEvent>(settings.limits.event_persist_buffer); mpsc::channel::<SubmittedEvent>(persist_buffer_limit);
// establish a channel for letting all threads now about a // establish a channel for letting all threads now about a
// requested server shutdown. // requested server shutdown.
let (invoke_shutdown, shutdown_listen) = broadcast::channel::<()>(1); let (invoke_shutdown, shutdown_listen) = broadcast::channel::<()>(1);
@ -267,7 +281,7 @@ fn main() -> Result<(), Error> {
// create a nip-05 verifier thread // create a nip-05 verifier thread
let verifier_opt = nip05::Verifier::new(metadata_rx, bcast_tx.clone()); let verifier_opt = nip05::Verifier::new(metadata_rx, bcast_tx.clone());
if let Ok(mut v) = verifier_opt { if let Ok(mut v) = verifier_opt {
if settings.verified_users.is_active() { if verified_users_active {
tokio::task::spawn(async move { tokio::task::spawn(async move {
info!("starting up NIP-05 verifier..."); info!("starting up NIP-05 verifier...");
v.run().await; v.run().await;
@ -286,8 +300,8 @@ fn main() -> Result<(), Error> {
"client query", "client query",
rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY
| rusqlite::OpenFlags::SQLITE_OPEN_SHARED_CACHE, | rusqlite::OpenFlags::SQLITE_OPEN_SHARED_CACHE,
settings.database.min_conn, db_min_conn,
settings.database.max_conn, db_max_conn,
true, true,
); );
// A `Service` is needed for every connection, so this // A `Service` is needed for every connection, so this
@ -428,7 +442,7 @@ async fn nostr_server(
}, },
Some(query_result) = query_rx.recv() => { Some(query_result) = query_rx.recv() => {
// database informed us of a query result we asked for // 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" { if query_result.event == "EOSE" {
let send_str = format!("[\"EOSE\",\"{}\"]", subesc); let send_str = format!("[\"EOSE\",\"{}\"]", subesc);
ws_stream.send(Message::Text(send_str)).await.ok(); ws_stream.send(Message::Text(send_str)).await.ok();
@ -452,7 +466,7 @@ async fn nostr_server(
cid, s, cid, s,
global_event.get_event_id_prefix()); global_event.get_event_id_prefix());
// create an event response and send it // 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(); ws_stream.send(Message::Text(format!("[\"EVENT\",\"{}\",{}]", subesc, event_str))).await.ok();
//nostr_stream.send(res).await.ok(); //nostr_stream.send(res).await.ok();
} else { } else {

View File

@ -252,7 +252,7 @@ PRAGMA user_version=5;
let tagval = t.get(1).unwrap(); let tagval = t.get(1).unwrap();
// insert as BLOB if we can restore it losslessly. // insert as BLOB if we can restore it losslessly.
// this means it needs to be even length and lowercase. // 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( tx.execute(
"INSERT INTO tag (event_id, name, value_hex) VALUES (?1, ?2, ?3);", "INSERT INTO tag (event_id, name, value_hex) VALUES (?1, ?2, ?3);",
params![event_id, tagname, hex::decode(&tagval).ok()], params![event_id, tagname, hex::decode(&tagval).ok()],

View File

@ -110,7 +110,7 @@ fn tag_search_char_from_filter(tagname: &str) -> Option<char> {
// of a single char. // of a single char.
let mut tagnamechars = tagname_nohash.chars(); let mut tagnamechars = tagname_nohash.chars();
let firstchar = tagnamechars.next(); let firstchar = tagnamechars.next();
return match firstchar { match firstchar {
Some(_) => { Some(_) => {
// check second char // check second char
if tagnamechars.next().is_none() { if tagnamechars.next().is_none() {
@ -120,7 +120,7 @@ fn tag_search_char_from_filter(tagname: &str) -> Option<char> {
} }
} }
None => None, None => None,
}; }
} }
impl<'de> Deserialize<'de> for Subscription { impl<'de> Deserialize<'de> for Subscription {