Successful parsing of requests from stream

This commit is contained in:
Greg Heartsfield 2021-11-24 14:19:50 -06:00
parent c001726402
commit 4e2ec93584
2 changed files with 13 additions and 8 deletions

View File

@ -1,5 +1,5 @@
use crate::error::{Error, Result};
use crate::event;
use crate::{event, request};
use log::{debug, info};
use uuid::Uuid;
@ -39,6 +39,7 @@ pub enum NostrRawMessage {
#[derive(PartialEq, Debug)]
pub enum NostrRequest {
Event(event::Event),
Subscription(request::Subscription),
}
// Wrap the message in the expected request type
@ -60,7 +61,10 @@ pub fn parse_type(msg: String) -> Result<NostrRequest> {
let typ = msg_type_wrapper(msg)?;
match typ {
NostrRawMessage::Event(_) => Err(Error::EventParseFailed),
NostrRawMessage::Req(_) => Err(Error::ReqParseFailed),
NostrRawMessage::Req(m) => {
let s = request::Subscription::parse(&m)?;
Ok(NostrRequest::Subscription(s))
}
NostrRawMessage::Close(_) => Err(Error::CloseParseFailed),
}
}

View File

@ -1,4 +1,4 @@
use crate::error::Result;
use crate::error::{Error, Result};
use serde::{Deserialize, Deserializer, Serialize};
//use serde_json::json;
//use serde_json::Result;
@ -78,11 +78,12 @@ impl<'de> Deserialize<'de> for Subscription {
})
}
}
// impl Subscription {
// pub fn parse(json: &str) -> Result<Subscription> {
// use serde to parse the ReqCmd, and then extract elements
// }
// }
impl Subscription {
pub fn parse(json: &str) -> Result<Subscription> {
serde_json::from_str(json).map_err(|e| Error::JsonParseFailed(e))
}
}
#[cfg(test)]
mod tests {