initial nostr code

This commit is contained in:
William Casarin
2022-12-11 15:46:18 -08:00
parent b6f5d8dc03
commit e6571d8847
16 changed files with 1434 additions and 41 deletions

60
enostr/src/relay/mod.rs Normal file
View File

@@ -0,0 +1,60 @@
use ewebsock::{WsReceiver, WsSender};
use crate::Result;
use std::fmt;
use std::hash::{Hash, Hasher};
pub mod message;
pub mod pool;
#[derive(Debug)]
pub enum RelayStatus {
Connected,
Connecting,
Disconnected,
}
pub struct Relay {
pub url: String,
pub status: RelayStatus,
pub sender: WsSender,
pub receiver: WsReceiver,
}
impl fmt::Debug for Relay {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Relay")
.field("url", &self.url)
.field("status", &self.status)
.finish()
}
}
impl Hash for Relay {
fn hash<H: Hasher>(&self, state: &mut H) {
// Hashes the Relay by hashing the URL
self.url.hash(state);
}
}
impl PartialEq for Relay {
fn eq(&self, other: &Self) -> bool {
self.url == other.url
}
}
impl Eq for Relay {}
impl Relay {
pub fn new(url: String) -> Result<Self> {
let status = RelayStatus::Connecting;
let (sender, receiver) = ewebsock::connect(&url)?;
Ok(Self {
url,
sender,
receiver,
status,
})
}
}