2019-10-16 15:47:56 +00:00
|
|
|
use super::V;
|
|
|
|
|
2020-05-24 19:18:48 +00:00
|
|
|
use bytes::{Buf, buf::BufMutExt, BytesMut};
|
2019-10-22 21:37:37 +00:00
|
|
|
use std::sync::Arc;
|
2020-05-11 20:02:43 +00:00
|
|
|
use std::marker::PhantomData;
|
2019-10-16 15:47:56 +00:00
|
|
|
|
2020-05-28 21:32:33 +00:00
|
|
|
use preserves::{
|
|
|
|
de::Deserializer,
|
|
|
|
error,
|
|
|
|
ser::to_writer,
|
|
|
|
value::reader::from_bytes,
|
|
|
|
};
|
|
|
|
|
2019-10-16 15:47:56 +00:00
|
|
|
pub type EndpointName = V;
|
|
|
|
pub type Assertion = V;
|
2019-10-22 21:37:37 +00:00
|
|
|
pub type Captures = Arc<Vec<Assertion>>;
|
2019-10-16 15:47:56 +00:00
|
|
|
|
2020-05-11 20:02:43 +00:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
2019-10-16 15:47:56 +00:00
|
|
|
pub enum Action {
|
|
|
|
Assert(EndpointName, Assertion),
|
|
|
|
Clear(EndpointName),
|
|
|
|
Message(Assertion),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
|
|
|
pub enum Event {
|
|
|
|
Add(EndpointName, Captures),
|
|
|
|
Del(EndpointName, Captures),
|
|
|
|
Msg(EndpointName, Captures),
|
|
|
|
End(EndpointName),
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:02:43 +00:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
2020-05-11 20:08:27 +00:00
|
|
|
pub enum C2S {
|
2019-10-16 15:47:56 +00:00
|
|
|
Connect(V),
|
|
|
|
Turn(Vec<Action>),
|
|
|
|
Ping(),
|
|
|
|
Pong(),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
2020-05-11 20:08:27 +00:00
|
|
|
pub enum S2C {
|
2019-10-24 19:05:39 +00:00
|
|
|
Err(String, V),
|
2019-10-16 15:47:56 +00:00
|
|
|
Turn(Vec<Event>),
|
|
|
|
Ping(),
|
|
|
|
Pong(),
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2020-05-28 21:32:33 +00:00
|
|
|
pub type Error = error::Error;
|
2019-10-16 15:47:56 +00:00
|
|
|
|
2020-05-11 20:02:43 +00:00
|
|
|
pub struct Codec<InT, OutT> {
|
|
|
|
ph_in: PhantomData<InT>,
|
|
|
|
ph_out: PhantomData<OutT>,
|
2019-10-16 15:47:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 20:10:34 +00:00
|
|
|
pub type ServerCodec = Codec<C2S, S2C>;
|
|
|
|
pub type ClientCodec = Codec<S2C, C2S>;
|
|
|
|
|
2020-05-11 20:02:43 +00:00
|
|
|
impl<InT, OutT> Codec<InT, OutT> {
|
2020-05-28 21:32:33 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Codec { ph_in: PhantomData, ph_out: PhantomData }
|
2019-10-16 15:47:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:02:43 +00:00
|
|
|
impl<InT: serde::de::DeserializeOwned, OutT> tokio_util::codec::Decoder for Codec<InT, OutT> {
|
|
|
|
type Item = InT;
|
2020-05-28 21:32:33 +00:00
|
|
|
type Error = Error;
|
2019-10-16 15:47:56 +00:00
|
|
|
fn decode(&mut self, bs: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
2020-05-28 21:32:33 +00:00
|
|
|
let mut r = from_bytes(bs);
|
|
|
|
let mut d = Deserializer::from_reader(&mut r);
|
|
|
|
match Self::Item::deserialize(&mut d) {
|
|
|
|
Err(e) if error::is_eof_error(&e) => Ok(None),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
Ok(item) => {
|
|
|
|
let count = d.read.source.index;
|
|
|
|
bs.advance(count);
|
|
|
|
Ok(Some(item))
|
2019-10-17 14:41:42 +00:00
|
|
|
}
|
2019-10-16 15:47:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:02:43 +00:00
|
|
|
impl<InT, OutT: serde::Serialize> tokio_util::codec::Encoder<OutT> for Codec<InT, OutT>
|
2020-05-06 15:14:05 +00:00
|
|
|
{
|
2020-05-28 21:32:33 +00:00
|
|
|
type Error = Error;
|
2020-05-11 20:02:43 +00:00
|
|
|
fn encode(&mut self, item: OutT, bs: &mut BytesMut) -> Result<(), Self::Error> {
|
2020-05-28 21:32:33 +00:00
|
|
|
to_writer(&mut bs.writer(), &item)
|
2019-10-16 15:47:56 +00:00
|
|
|
}
|
|
|
|
}
|