nostr-rs-relay

My dev fork of nostr-rs-relay
git clone git://jb55.com/nostr-rs-relay
Log | Files | Refs | README | LICENSE

commit 19ed990c5782af2c6668ed788d2b140e6721a253
parent d78bbfc2906caa53cc40fe2fc87a63736b2e1b5d
Author: Greg Heartsfield <scsibug@imap.cc>
Date:   Wed,  5 Jan 2022 10:10:44 -0500

refactor: fix clippy errors for relay info response

Diffstat:
Msrc/config.rs | 2+-
Msrc/info.rs | 34++++++++--------------------------
Msrc/main.rs | 5+++--
3 files changed, 12 insertions(+), 29 deletions(-)

diff --git a/src/config.rs b/src/config.rs @@ -8,7 +8,7 @@ lazy_static! { pub static ref SETTINGS: RwLock<Settings> = RwLock::new(Settings::default()); } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone)] #[allow(unused)] pub struct Info { pub relay_url: Option<String>, diff --git a/src/info.rs b/src/info.rs @@ -25,36 +25,18 @@ pub struct RelayInfo { pub version: Option<String>, } -impl Default for RelayInfo { - fn default() -> Self { +/// Convert an Info configuration into public Relay Info +impl From<config::Info> for RelayInfo { + fn from(i: config::Info) -> Self { RelayInfo { - id: None, - name: None, - description: None, - pubkey: None, - email: None, + id: i.relay_url, + name: i.name, + description: i.description, + pubkey: i.pubkey, + email: i.email, supported_nips: Some(vec![1]), software: Some("https://git.sr.ht/~gheartsfield/nostr-rs-relay".to_owned()), version: CARGO_PKG_VERSION.map(|x| x.to_owned()), } } } - -/// Convert an Info struct into Relay Info json string -pub fn relay_info_json(info: &config::Info) -> String { - // get a default RelayInfo - let mut r = RelayInfo::default(); - // update fields from Info, if present - r.id = info.relay_url.clone(); - r.name = info.name.clone(); - r.description = info.description.clone(); - r.pubkey = info.pubkey.clone(); - r.email = info.email.clone(); - r.to_json() -} - -impl RelayInfo { - pub fn to_json(self) -> String { - serde_json::to_string_pretty(&self).unwrap() - } -} diff --git a/src/main.rs b/src/main.rs @@ -14,7 +14,7 @@ use nostr_rs_relay::conn; use nostr_rs_relay::db; use nostr_rs_relay::error::{Error, Result}; use nostr_rs_relay::event::Event; -use nostr_rs_relay::info::relay_info_json; +use nostr_rs_relay::info::RelayInfo; use nostr_rs_relay::protostream; use nostr_rs_relay::protostream::NostrMessage::*; use nostr_rs_relay::protostream::NostrResponse::*; @@ -110,7 +110,8 @@ async fn handle_web_request( let config = config::SETTINGS.read().unwrap(); // build a relay info response debug!("Responding to server info request"); - let b = Body::from(relay_info_json(&config.info)); + let rinfo = RelayInfo::from(config.info.clone()); + let b = Body::from(serde_json::to_string_pretty(&rinfo).unwrap()); return Ok(Response::builder() .status(200) .header("Content-Type", "application/nostr+json")