use rand::{rngs::OsRng, Rng};
impl_bytes!(
SessionID,
32,
"Session identifier for multiplexing multiple sessions over the \
same transport"
);
impl SessionID {
pub fn random() -> Self {
let mut session_id = [0u8; 32];
OsRng.fill(&mut session_id);
SessionID(session_id)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
#[cbor(with_default)]
#[repr(u8)]
pub enum Kind {
NoiseSession = 0,
InsecureQuery = 1,
LocalQuery = 2,
}
impl Default for Kind {
fn default() -> Self {
Self::NoiseSession
}
}
#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
pub struct Frame {
pub session: SessionID,
pub untrusted_plaintext: String,
pub payload: Vec<u8>,
}
#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
#[cbor(no_default)]
pub struct Request {
pub method: String,
pub args: cbor::Value,
}
#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
pub struct Error {
pub message: String,
}
#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
pub enum Body {
Success(cbor::Value),
Error(String),
}
#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
#[cbor(no_default)]
pub struct Response {
pub body: Body,
}
#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
pub enum Message {
Request(Request),
Response(Response),
Close,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, cbor::Encode, cbor::Decode)]
pub enum PeerFeedback {
Success = 0,
Failure = 1,
BadPeer = 2,
}