Skip to content

Commit

Permalink
fix clippy warnings
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Aug 20, 2024
1 parent 769baf9 commit 5773c0b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 34 deletions.
2 changes: 1 addition & 1 deletion enostr/src/client/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl ClientMessage {
format!("[\"REQ\",\"{}\",{}]", sub_id, filters_json_str)
} else {
let filters_json_str: Result<Vec<String>, Error> = filters
.into_iter()
.iter()
.map(|f| f.json().map_err(Into::<Error>::into))
.collect();
format!("[\"REQ\",\"{}\",{}]", sub_id, filters_json_str?.join(","))
Expand Down
1 change: 0 additions & 1 deletion enostr/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//use nostr::prelude::secp256k1;
use serde_json;
use std::array::TryFromSliceError;
use std::fmt;

Expand Down
22 changes: 6 additions & 16 deletions enostr/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl FullKeypair {
let secret_key = nostr::SecretKey::from(*secret_key);
FullKeypair {
pubkey: Pubkey::new(&xopk.serialize()),
secret_key: SecretKey::from(secret_key),
secret_key,
}
}

Expand Down Expand Up @@ -90,11 +90,7 @@ impl std::fmt::Display for Keypair {

impl std::fmt::Display for FullKeypair {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Keypair:\n\tpublic: {}\n\tsecret: {}",
self.pubkey, "<hidden>"
)
write!(f, "Keypair:\n\tpublic: {}\n\tsecret: <hidden>", self.pubkey)
}
}

Expand All @@ -108,23 +104,17 @@ impl SerializableKeypair {
pub fn from_keypair(kp: &Keypair, pass: &str, log_n: u8) -> Self {
Self {
pubkey: kp.pubkey.clone(),
encrypted_secret_key: kp
.secret_key
.clone()
.map(|s| {
EncryptedSecretKey::new(&s, pass, log_n, nostr::nips::nip49::KeySecurity::Weak)
.ok()
})
.flatten(),
encrypted_secret_key: kp.secret_key.clone().and_then(|s| {
EncryptedSecretKey::new(&s, pass, log_n, nostr::nips::nip49::KeySecurity::Weak).ok()
}),
}
}

pub fn to_keypair(&self, pass: &str) -> Keypair {
Keypair::new(
self.pubkey.clone(),
self.encrypted_secret_key
.map(|e| e.to_secret_key(pass).ok())
.flatten(),
.and_then(|e| e.to_secret_key(pass).ok()),
)
}
}
2 changes: 1 addition & 1 deletion enostr/src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Note {
}

pub fn verify(&self) -> Result<Self, Error> {
return Err(Error::InvalidSignature);
Err(Error::InvalidSignature)
}

/// This is just for serde sanity checking
Expand Down
10 changes: 5 additions & 5 deletions enostr/src/relay/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<'a> From<&'a WsEvent> for RelayEvent<'a> {
impl<'a> From<&'a WsMessage> for RelayEvent<'a> {
fn from(wsmsg: &'a WsMessage) -> RelayEvent<'a> {
match wsmsg {
WsMessage::Text(ref s) => match RelayMessage::from_json(&s).map(RelayEvent::Message) {
WsMessage::Text(s) => match RelayMessage::from_json(s).map(RelayEvent::Message) {
Ok(msg) => msg,
Err(err) => RelayEvent::Error(err),
},
Expand All @@ -59,9 +59,9 @@ impl<'a> RelayMessage<'a> {

pub fn ok(event_id: &'a str, status: bool, message: &'a str) -> Self {
RelayMessage::OK(CommandResult {
event_id: event_id,
event_id,
status,
message: message,
message,
})
}

Expand All @@ -78,7 +78,7 @@ impl<'a> RelayMessage<'a> {
// Relay response format: ["NOTICE", <message>]
if &msg[0..=9] == "[\"NOTICE\"," {
// TODO: there could be more than one space, whatever
let start = if msg.bytes().nth(10) == Some(b' ') {
let start = if msg.as_bytes().get(10).copied() == Some(b' ') {
12
} else {
11
Expand All @@ -96,7 +96,7 @@ impl<'a> RelayMessage<'a> {
// EOSE (NIP-15)
// Relay response format: ["EOSE", <subscription_id>]
if &msg[0..=7] == "[\"EOSE\"," {
let start = if msg.bytes().nth(8) == Some(b' ') {
let start = if msg.as_bytes().get(8).copied() == Some(b' ') {
10
} else {
9
Expand Down
24 changes: 14 additions & 10 deletions enostr/src/relay/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct PoolRelay {
impl PoolRelay {
pub fn new(relay: Relay) -> PoolRelay {
PoolRelay {
relay: relay,
relay,
last_ping: Instant::now(),
last_connect_attempt: Instant::now(),
retry_connect_after: Self::initial_reconnect_duration(),
Expand All @@ -43,6 +43,12 @@ pub struct RelayPool {
pub ping_rate: Duration,
}

impl Default for RelayPool {
fn default() -> Self {
RelayPool::new()
}
}

impl RelayPool {
// Constructs a new, empty RelayPool.
pub fn new() -> RelayPool {
Expand All @@ -59,11 +65,12 @@ impl RelayPool {

pub fn has(&self, url: &str) -> bool {
for relay in &self.relays {
if &relay.relay.url == url {
if relay.relay.url == url {
return true;
}
}
return false;

false
}

pub fn send(&mut self, cmd: &ClientMessage) {
Expand Down Expand Up @@ -157,7 +164,7 @@ impl RelayPool {
/// function searches each relay in the list in order, attempting to
/// receive a message from each. If a message is received, return it.
/// If no message is received from any relays, None is returned.
pub fn try_recv<'a>(&'a mut self) -> Option<PoolEvent<'a>> {
pub fn try_recv(&mut self) -> Option<PoolEvent<'_>> {
for relay in &mut self.relays {
let relay = &mut relay.relay;
if let Some(event) = relay.receiver.try_recv() {
Expand All @@ -176,12 +183,9 @@ impl RelayPool {
// let's just handle pongs here.
// We only need to do this natively.
#[cfg(not(target_arch = "wasm32"))]
match &ev {
WsMessage::Ping(ref bs) => {
debug!("pong {}", &relay.url);
relay.sender.send(WsMessage::Pong(bs.to_owned()));
}
_ => {}
if let WsMessage::Ping(ref bs) = ev {
debug!("pong {}", &relay.url);
relay.sender.send(WsMessage::Pong(bs.to_owned()));
}
}
}
Expand Down

0 comments on commit 5773c0b

Please sign in to comment.