mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2024-11-24 17:49:07 -05:00
refactor: clippy suggestions
This commit is contained in:
parent
ca0f01c94b
commit
e732f918f9
|
@ -22,9 +22,9 @@ impl From<CloseCmd> for Result<Close> {
|
|||
fn from(cc: CloseCmd) -> Result<Close> {
|
||||
// ensure command is correct
|
||||
if cc.cmd != "CLOSE" {
|
||||
return Err(Error::CommandUnknownError);
|
||||
Err(Error::CommandUnknownError)
|
||||
} else {
|
||||
return Ok(Close { id: cc.id });
|
||||
Ok(Close { id: cc.id })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,12 @@ pub struct ClientConn {
|
|||
max_subs: usize,
|
||||
}
|
||||
|
||||
impl Default for ClientConn {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientConn {
|
||||
/// Create a new, empty connection state.
|
||||
pub fn new() -> Self {
|
||||
|
@ -77,7 +83,7 @@ impl ClientConn {
|
|||
"Registered new subscription, currently have {} active subs",
|
||||
self.subscriptions.len()
|
||||
);
|
||||
return Ok(());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove the subscription for this connection.
|
||||
|
|
|
@ -130,7 +130,7 @@ pub fn write_event(conn: &mut Connection, e: &Event) -> Result<usize> {
|
|||
let ev_id = tx.last_insert_rowid();
|
||||
// add all event tags into the event_ref table
|
||||
let etags = e.get_event_tags();
|
||||
if etags.len() > 0 {
|
||||
if !etags.is_empty() {
|
||||
for etag in etags.iter() {
|
||||
tx.execute(
|
||||
"INSERT OR IGNORE INTO event_ref (event_id, referenced_event) VALUES (?1, ?2)",
|
||||
|
@ -140,7 +140,7 @@ pub fn write_event(conn: &mut Connection, e: &Event) -> Result<usize> {
|
|||
}
|
||||
// add all event tags into the pubkey_ref table
|
||||
let ptags = e.get_pubkey_tags();
|
||||
if ptags.len() > 0 {
|
||||
if !ptags.is_empty() {
|
||||
for ptag in ptags.iter() {
|
||||
tx.execute(
|
||||
"INSERT OR IGNORE INTO event_ref (event_id, referenced_pubkey) VALUES (?1, ?2)",
|
||||
|
@ -238,7 +238,7 @@ fn query_from_sub(sub: &Subscription) -> String {
|
|||
filter_components.push(created_clause);
|
||||
}
|
||||
// combine all clauses, and add to filter_clauses
|
||||
if filter_components.len() > 0 {
|
||||
if !filter_components.is_empty() {
|
||||
let mut fc = "( ".to_owned();
|
||||
fc.push_str(&filter_components.join(" AND "));
|
||||
fc.push_str(" )");
|
||||
|
@ -247,7 +247,7 @@ fn query_from_sub(sub: &Subscription) -> String {
|
|||
}
|
||||
|
||||
// combine all filters with OR clauses, if any exist
|
||||
if filter_clauses.len() > 0 {
|
||||
if !filter_clauses.is_empty() {
|
||||
query.push_str(" WHERE ");
|
||||
query.push_str(&filter_clauses.join(" OR "));
|
||||
}
|
||||
|
|
29
src/event.rs
29
src/event.rs
|
@ -39,7 +39,7 @@ where
|
|||
D: Deserializer<'de>,
|
||||
{
|
||||
let opt = Option::deserialize(deserializer)?;
|
||||
Ok(opt.unwrap_or_else(|| vec![]))
|
||||
Ok(opt.unwrap_or_else(Vec::new))
|
||||
}
|
||||
|
||||
/// Convert network event to parsed/validated event.
|
||||
|
@ -47,11 +47,11 @@ impl From<EventCmd> for Result<Event> {
|
|||
fn from(ec: EventCmd) -> Result<Event> {
|
||||
// ensure command is correct
|
||||
if ec.cmd != "EVENT" {
|
||||
return Err(CommandUnknownError);
|
||||
Err(CommandUnknownError)
|
||||
} else if ec.event.is_valid() {
|
||||
return Ok(ec.event);
|
||||
Ok(ec.event)
|
||||
} else {
|
||||
return Err(EventInvalid);
|
||||
Err(EventInvalid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ impl Event {
|
|||
}
|
||||
let c = c_opt.unwrap();
|
||||
// * compute the sha256sum.
|
||||
let digest: sha256::Hash = sha256::Hash::hash(&c.as_bytes());
|
||||
let digest: sha256::Hash = sha256::Hash::hash(c.as_bytes());
|
||||
let hex_digest = format!("{:x}", digest);
|
||||
// * ensure the id matches the computed sha256sum.
|
||||
if self.id != hex_digest {
|
||||
|
@ -88,10 +88,7 @@ impl Event {
|
|||
let message = secp256k1::Message::from(digest);
|
||||
let pubkey = schnorrsig::PublicKey::from_str(&self.pubkey).unwrap();
|
||||
let verify = secp.schnorrsig_verify(&sig, &message, &pubkey);
|
||||
match verify {
|
||||
Ok(()) => true,
|
||||
_ => false,
|
||||
}
|
||||
matches!(verify, Ok(()))
|
||||
}
|
||||
|
||||
/// Convert event to canonical representation for signing.
|
||||
|
@ -99,7 +96,7 @@ impl Event {
|
|||
// create a JsonValue for each event element
|
||||
let mut c: Vec<Value> = vec![];
|
||||
// id must be set to 0
|
||||
let id = Number::from(0 as u64);
|
||||
let id = Number::from(0_u64);
|
||||
c.push(serde_json::Value::Number(id));
|
||||
// public key
|
||||
c.push(Value::String(self.pubkey.to_owned()));
|
||||
|
@ -135,10 +132,8 @@ impl Event {
|
|||
pub fn get_event_tags(&self) -> Vec<&str> {
|
||||
let mut etags = vec![];
|
||||
for t in self.tags.iter() {
|
||||
if t.len() >= 2 {
|
||||
if t.get(0).unwrap() == "e" {
|
||||
etags.push(&t.get(1).unwrap()[..]);
|
||||
}
|
||||
if t.len() >= 2 && t.get(0).unwrap() == "e" {
|
||||
etags.push(&t.get(1).unwrap()[..]);
|
||||
}
|
||||
}
|
||||
etags
|
||||
|
@ -148,10 +143,8 @@ impl Event {
|
|||
pub fn get_pubkey_tags(&self) -> Vec<&str> {
|
||||
let mut ptags = vec![];
|
||||
for t in self.tags.iter() {
|
||||
if t.len() >= 2 {
|
||||
if t.get(0).unwrap() == "p" {
|
||||
ptags.push(&t.get(1).unwrap()[..]);
|
||||
}
|
||||
if t.len() >= 2 && t.get(0).unwrap() == "p" {
|
||||
ptags.push(&t.get(1).unwrap()[..]);
|
||||
}
|
||||
}
|
||||
ptags
|
||||
|
|
|
@ -177,12 +177,9 @@ async fn nostr_server(
|
|||
match parsed {
|
||||
Ok(c) => {
|
||||
let stop_tx = running_queries.remove(&c.id);
|
||||
match stop_tx {
|
||||
Some(tx) => {
|
||||
info!("Removing query, telling DB to abandon query");
|
||||
tx.send(()).ok();
|
||||
},
|
||||
None => {}
|
||||
if let Some(tx) = stop_tx {
|
||||
info!("Removing query, telling DB to abandon query");
|
||||
tx.send(()).ok();
|
||||
}
|
||||
conn.unsubscribe(c);
|
||||
},
|
||||
|
|
|
@ -44,7 +44,7 @@ pub struct NostrStream {
|
|||
|
||||
/// Given a websocket, return a protocol stream wrapper.
|
||||
pub fn wrap_ws_in_nostr(ws: WebSocketStream<TcpStream>) -> NostrStream {
|
||||
return NostrStream { ws_stream: ws };
|
||||
NostrStream { ws_stream: ws }
|
||||
}
|
||||
|
||||
/// Implement the [`Stream`] interface to produce Nostr messages.
|
||||
|
|
|
@ -52,18 +52,18 @@ impl<'de> Deserialize<'de> for Subscription {
|
|||
// check for array
|
||||
let va = v
|
||||
.as_array_mut()
|
||||
.ok_or(serde::de::Error::custom("not array"))?;
|
||||
.ok_or_else(|| serde::de::Error::custom("not array"))?;
|
||||
|
||||
// check length
|
||||
if va.len() < 3 {
|
||||
return Err(serde::de::Error::custom("not enough fields"));
|
||||
}
|
||||
let mut i = va.into_iter();
|
||||
let mut i = va.iter_mut();
|
||||
// get command ("REQ") and ensure it is a string
|
||||
let req_cmd_str: serde_json::Value = i.next().unwrap().take();
|
||||
let req = req_cmd_str.as_str().ok_or(serde::de::Error::custom(
|
||||
"first element of request was not a string",
|
||||
))?;
|
||||
let req = req_cmd_str
|
||||
.as_str()
|
||||
.ok_or_else(|| serde::de::Error::custom("first element of request was not a string"))?;
|
||||
if req != "REQ" {
|
||||
return Err(serde::de::Error::custom("missing REQ command"));
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ impl<'de> Deserialize<'de> for Subscription {
|
|||
let sub_id_str: serde_json::Value = i.next().unwrap().take();
|
||||
let sub_id = sub_id_str
|
||||
.as_str()
|
||||
.ok_or(serde::de::Error::custom("missing subscription id"))?;
|
||||
.ok_or_else(|| serde::de::Error::custom("missing subscription id"))?;
|
||||
|
||||
let mut filters = vec![];
|
||||
for fv in i {
|
||||
|
@ -100,7 +100,7 @@ impl Subscription {
|
|||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -135,9 +135,8 @@ impl ReqFilter {
|
|||
self.id.as_ref().map(|v| v == &event.id).unwrap_or(true)
|
||||
&& self.since.map(|t| event.created_at > t).unwrap_or(true)
|
||||
&& self.kind_match(event.kind)
|
||||
&& self.author_match(&event)
|
||||
&& self.event_match(&event)
|
||||
&& true // match if all other fields are absent
|
||||
&& self.author_match(event)
|
||||
&& self.event_match(event)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user