From f249fae274241c86c6f9653ac9e574186a73e025 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Wed, 14 Feb 2024 14:31:17 +1300 Subject: [PATCH] NostrReply --- src/main.rs | 1 + src/reply.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/reply.rs diff --git a/src/main.rs b/src/main.rs index b992bf0..e6da598 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ include!("macros.rs"); pub mod config; pub mod error; pub mod globals; +pub mod reply; pub mod store; pub mod tls; pub mod types; diff --git a/src/reply.rs b/src/reply.rs new file mode 100644 index 0000000..45dda57 --- /dev/null +++ b/src/reply.rs @@ -0,0 +1,28 @@ +use crate::types::{Event, Id}; +use std::fmt; + +pub enum NostrReply<'a> { + Event(&'a str, Event<'a>), + Ok(Id, bool, String), + Eose(&'a str), + Closed(&'a str, String), + Notice(String), +} + +impl NostrReply<'_> { + pub fn as_json(&self) -> String { + match self { + NostrReply::Event(subid, event) => format!(r#"["EVENT", "{subid}", {}]"#, event), + NostrReply::Ok(id, ok, msg) => format!(r#"["OK","{id}",{ok},"{msg}"]"#), + NostrReply::Eose(subid) => format!(r#"["EOSE","{subid}"]"#), + NostrReply::Closed(subid, msg) => format!(r#"["CLOSED","{subid}","{msg}"]"#), + NostrReply::Notice(msg) => format!(r#"["NOTICE","{msg}"]"#), + } + } +} + +impl fmt::Display for NostrReply<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.as_json()) + } +}