mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2024-11-14 23:19:07 -05:00
Request parsing
This commit is contained in:
parent
68a69505ac
commit
bba66a1bce
|
@ -1,3 +1,4 @@
|
|||
pub mod error;
|
||||
pub mod event;
|
||||
pub mod proto;
|
||||
pub mod request;
|
||||
|
|
102
src/request.rs
102
src/request.rs
|
@ -3,17 +3,17 @@ use serde::{Deserialize, Deserializer, Serialize};
|
|||
//use serde_json::json;
|
||||
//use serde_json::Result;
|
||||
|
||||
|
||||
// Container for a request filter
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
|
||||
#[serde(transparent)]
|
||||
pub struct ReqCmd {
|
||||
cmds: Vec<String>
|
||||
cmds: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
|
||||
#[derive(PartialEq, Debug, Clone)]
|
||||
pub struct Subscription {
|
||||
id: String,
|
||||
Vec<ReqFilter>
|
||||
filters: Vec<ReqFilter>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
|
||||
|
@ -28,3 +28,97 @@ pub struct ReqFilter {
|
|||
since: Option<u64>,
|
||||
authors: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Subscription {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Subscription, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let r: ReqCmd = Deserialize::deserialize(deserializer)?;
|
||||
let elems = r.cmds;
|
||||
// ensure we have at least 3 fields
|
||||
if elems.len() < 3 {
|
||||
Err(serde::de::Error::custom("not enough fields"))
|
||||
} else {
|
||||
// divide into req/sub-id vector, and filter vector
|
||||
let (header, filter_strs) = elems.split_at(2);
|
||||
let req_cmd = header.get(0).unwrap();
|
||||
let sub_id = header.get(1).unwrap();
|
||||
if req_cmd != "REQ" {
|
||||
return Err(serde::de::Error::custom("missing REQ command"));
|
||||
}
|
||||
let mut filters = vec![];
|
||||
for e in filter_strs.iter() {
|
||||
let des_res = serde_json::from_str::<ReqFilter>(e);
|
||||
match des_res {
|
||||
Ok(f) => filters.push(f),
|
||||
Err(_) => return Err(serde::de::Error::custom("could not parse filter")),
|
||||
}
|
||||
}
|
||||
Ok(Subscription {
|
||||
id: sub_id.to_owned(),
|
||||
filters,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// impl Subscription {
|
||||
// pub fn parse(json: &str) -> Result<Subscription> {
|
||||
// use serde to parse the ReqCmd, and then extract elements
|
||||
// }
|
||||
// }
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
// fn simple_req() -> Event {
|
||||
// super::Event {
|
||||
// id: 0,
|
||||
// pubkey: 0,
|
||||
// created_at: 0,
|
||||
// kind: 0,
|
||||
// tags: vec![],
|
||||
// content: "".to_owned(),
|
||||
// sig: 0,
|
||||
// }
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn empty_request_parse() -> Result<()> {
|
||||
let raw_json = "[\"REQ\",\"some-id\",\"{}\"]";
|
||||
let s: Subscription = serde_json::from_str(raw_json)?;
|
||||
assert_eq!(s.id, "some-id");
|
||||
assert_eq!(s.filters.len(), 1);
|
||||
assert_eq!(s.filters.get(0).unwrap().author, None);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incorrect_header() {
|
||||
let raw_json = "[\"REQUEST\",\"some-id\",\"{}\"]";
|
||||
assert!(serde_json::from_str::<Subscription>(raw_json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn req_missing_filters() {
|
||||
let raw_json = "[\"REQ\",\"some-id\"]";
|
||||
assert!(serde_json::from_str::<Subscription>(raw_json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_filter() {
|
||||
// unrecognized field in filter
|
||||
let raw_json = "[\"REQ\",\"some-id\",\"{\"foo\": 3}\"]";
|
||||
assert!(serde_json::from_str::<Subscription>(raw_json).is_err());
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn author_filter() -> Result<()> {
|
||||
// let raw_json = "[\"REQ\",\"some-id\",\"{\"author\": \"test-author-id\"}\"]";
|
||||
// let s: Subscription = serde_json::from_str(raw_json)?;
|
||||
// assert_eq!(s.id, "some-id");
|
||||
// assert_eq!(s.filters.len(), 1);
|
||||
// Ok(())
|
||||
// }
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user