refactor: fix clippy errors for relay info response

This commit is contained in:
Greg Heartsfield 2022-01-05 10:10:44 -05:00
parent d78bbfc290
commit 19ed990c57
3 changed files with 12 additions and 29 deletions

View File

@ -8,7 +8,7 @@ lazy_static! {
pub static ref SETTINGS: RwLock<Settings> = RwLock::new(Settings::default()); pub static ref SETTINGS: RwLock<Settings> = RwLock::new(Settings::default());
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(unused)] #[allow(unused)]
pub struct Info { pub struct Info {
pub relay_url: Option<String>, pub relay_url: Option<String>,

View File

@ -25,36 +25,18 @@ pub struct RelayInfo {
pub version: Option<String>, pub version: Option<String>,
} }
impl Default for RelayInfo { /// Convert an Info configuration into public Relay Info
fn default() -> Self { impl From<config::Info> for RelayInfo {
fn from(i: config::Info) -> Self {
RelayInfo { RelayInfo {
id: None, id: i.relay_url,
name: None, name: i.name,
description: None, description: i.description,
pubkey: None, pubkey: i.pubkey,
email: None, email: i.email,
supported_nips: Some(vec![1]), supported_nips: Some(vec![1]),
software: Some("https://git.sr.ht/~gheartsfield/nostr-rs-relay".to_owned()), software: Some("https://git.sr.ht/~gheartsfield/nostr-rs-relay".to_owned()),
version: CARGO_PKG_VERSION.map(|x| x.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()
}
}

View File

@ -14,7 +14,7 @@ use nostr_rs_relay::conn;
use nostr_rs_relay::db; use nostr_rs_relay::db;
use nostr_rs_relay::error::{Error, Result}; use nostr_rs_relay::error::{Error, Result};
use nostr_rs_relay::event::Event; 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;
use nostr_rs_relay::protostream::NostrMessage::*; use nostr_rs_relay::protostream::NostrMessage::*;
use nostr_rs_relay::protostream::NostrResponse::*; use nostr_rs_relay::protostream::NostrResponse::*;
@ -110,7 +110,8 @@ async fn handle_web_request(
let config = config::SETTINGS.read().unwrap(); let config = config::SETTINGS.read().unwrap();
// build a relay info response // build a relay info response
debug!("Responding to server info request"); 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() return Ok(Response::builder()
.status(200) .status(200)
.header("Content-Type", "application/nostr+json") .header("Content-Type", "application/nostr+json")