mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 10:15:31 +00:00
improve ws protocol struct naming and documentation
This commit is contained in:
parent
b473bb6fba
commit
0789f7ec3b
17 changed files with 193 additions and 137 deletions
40
crates/ws_protocol/src/incoming/mod.rs
Normal file
40
crates/ws_protocol/src/incoming/mod.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
use anyhow::Context;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod announce;
|
||||
pub mod scrape;
|
||||
|
||||
pub use announce::*;
|
||||
pub use scrape::*;
|
||||
|
||||
/// Message received by tracker
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum InMessage {
|
||||
AnnounceRequest(AnnounceRequest),
|
||||
ScrapeRequest(ScrapeRequest),
|
||||
}
|
||||
|
||||
impl InMessage {
|
||||
#[inline]
|
||||
pub fn to_ws_message(&self) -> ::tungstenite::Message {
|
||||
::tungstenite::Message::from(::serde_json::to_string(&self).unwrap())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_ws_message(ws_message: tungstenite::Message) -> ::anyhow::Result<Self> {
|
||||
use tungstenite::Message;
|
||||
|
||||
match ws_message {
|
||||
Message::Text(text) => {
|
||||
let mut text: Vec<u8> = text.into();
|
||||
|
||||
::simd_json::serde::from_slice(&mut text).context("deserialize with serde")
|
||||
}
|
||||
Message::Binary(mut bytes) => {
|
||||
::simd_json::serde::from_slice(&mut bytes[..]).context("deserialize with serde")
|
||||
}
|
||||
_ => Err(anyhow::anyhow!("Message is neither text nor binary")),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue