Simple event struct with serialization

This commit is contained in:
Greg Heartsfield 2021-11-21 18:14:38 -06:00
parent 0fe2423b22
commit c2704af0d6
3 changed files with 126 additions and 1 deletions

51
Cargo.lock generated
View File

@ -307,6 +307,9 @@ dependencies = [
"env_logger", "env_logger",
"futures-util", "futures-util",
"log", "log",
"serde",
"serde_json",
"serde_repr",
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-tungstenite", "tokio-tungstenite",
@ -489,12 +492,60 @@ version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.1.0" version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "serde"
version = "1.0.130"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.130"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "063bf466a64011ac24040a49009724ee60a57da1b437617ceb32e53ad61bfb19"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "serde_repr"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98d0516900518c29efa217c298fa1f4e6c6ffc85ae29fd7f4ee48f176e1a9ed5"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "sha-1" name = "sha-1"
version = "0.9.8" version = "0.9.8"

View File

@ -13,3 +13,6 @@ futures-util = "0.3.17"
tokio-tungstenite = "0.16.0" tokio-tungstenite = "0.16.0"
tungstenite = "0.16.0" tungstenite = "0.16.0"
thiserror = "1.0.30" thiserror = "1.0.30"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.71"
serde_repr = "0.1.7"

View File

@ -1 +1,72 @@
pub struct Event {} use serde::{Deserialize, Deserializer, Serialize};
use serde_json::json;
use serde_json::Result;
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Event {
id: u32,
pubkey: u32,
created_at: u64,
kind: u8,
tags: Vec<Vec<String>>,
content: String,
sig: u64,
}
// Goals:
// Roundtrip from JSON-string to Event, and back to string.
// Perform validation on an Event to ensure the id and signature are correct.
#[cfg(test)]
mod tests {
use crate::event::Event;
use serde_json::Result;
fn simple_event() -> Event {
super::Event {
id: 0,
pubkey: 0,
created_at: 0,
kind: 0,
tags: vec![],
content: "".to_owned(),
sig: 0,
}
}
#[test]
fn event_creation() {
// create an event
let event = simple_event();
assert_eq!(event.id, 0);
}
#[test]
fn event_serialize() -> Result<()> {
// serialize an event to JSON string
let event = simple_event();
let j = serde_json::to_string(&event)?;
assert_eq!(j, "{\"id\":0,\"pubkey\":0,\"created_at\":0,\"kind\":0,\"tags\":[],\"content\":\"\",\"sig\":0}");
Ok(())
}
#[test]
fn event_tags_serialize() -> Result<()> {
// serialize an event with tags to JSON string
let mut event = simple_event();
event.tags = vec![
vec![
"e".to_owned(),
"xxxx".to_owned(),
"wss://example.com".to_owned(),
],
vec![
"p".to_owned(),
"yyyyy".to_owned(),
"wss://example.com:3033".to_owned(),
],
];
let j = serde_json::to_string(&event)?;
assert_eq!(j, "{\"id\":0,\"pubkey\":0,\"created_at\":0,\"kind\":0,\"tags\":[[\"e\",\"xxxx\",\"wss://example.com\"],[\"p\",\"yyyyy\",\"wss://example.com:3033\"]],\"content\":\"\",\"sig\":0}");
Ok(())
}
}