use crate::{request::Request, response::Response, serialize::serialize, status::Status};
use futures::Future;
use serde::{Deserialize, Serialize};
use std::{
collections::{HashMap, HashSet},
pin::Pin,
};
use zenoh::config::ZenohId;
pub(crate) type ServerTaskFuture =
Pin<Box<dyn Future<Output = Result<Vec<u8>, Status>> + Send + 'static>>;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct WireMessage {
pub(crate) payload: Option<Vec<u8>>,
pub(crate) status: Status,
}
#[derive(Debug)]
pub struct Message {
pub method: String,
pub body: Vec<u8>,
pub metadata: HashMap<String, String>,
pub status: Status,
}
impl Message {
pub fn from_parts(
method: String,
body: Vec<u8>,
metadata: HashMap<String, String>,
status: Status,
) -> Self {
Self {
method,
body,
metadata,
status,
}
}
}
impl From<Status> for Message {
fn from(value: Status) -> Self {
Self {
method: "".into(),
body: vec![],
metadata: HashMap::new(),
status: value,
}
}
}
impl<T> From<Response<T>> for Message
where
T: Serialize + Clone + std::fmt::Debug,
for<'de2> T: Deserialize<'de2>,
{
fn from(value: Response<T>) -> Self {
Self {
method: "".into(),
body: serialize(value.get_ref()).unwrap_or_default(),
metadata: value.get_metadata().clone(),
status: Status::ok(""),
}
}
}
impl<T> From<Request<T>> for Message
where
T: Serialize + Clone + std::fmt::Debug,
for<'de2> T: Deserialize<'de2>,
{
fn from(value: Request<T>) -> Self {
Self {
method: "".into(),
body: serialize(value.get_ref()).unwrap_or_default(),
metadata: value.get_metadata().clone(),
status: Status::ok(""),
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ServerMetadata {
pub labels: HashSet<String>,
pub id: ZenohId,
}