use serde::{ser::Error as _, Deserialize, Deserializer, Serialize, Serializer};
use crate::session::ModuleControlRequest;
pub const OBSERVED_PEER_CAPABILITY: &str = "observed-peer/v1";
pub const OPERATOR_APP_CAPABILITY: &str = "operator-app/v1";
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ObservedPeerStatus {
Observed,
Unobserved,
Unknown(String),
}
impl Serialize for ObservedPeerStatus {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(match self {
Self::Observed => "observed",
Self::Unobserved => "unobserved",
Self::Unknown(value) => value,
})
}
}
impl<'de> Deserialize<'de> for ObservedPeerStatus {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Ok(match String::deserialize(deserializer)?.as_str() {
"observed" => Self::Observed,
"unobserved" => Self::Unobserved,
value => Self::Unknown(value.to_owned()),
})
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ObservedPeer {
pub status: ObservedPeerStatus,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub uid: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sid: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub exe: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub signing_id: Option<String>,
}
impl ObservedPeer {
pub fn unobserved() -> Self {
Self {
status: ObservedPeerStatus::Unobserved,
uid: None,
sid: None,
exe: None,
signing_id: None,
}
}
pub fn observed_uid(uid: u32) -> Self {
Self {
status: ObservedPeerStatus::Observed,
uid: Some(uid),
..Self::unobserved()
}
}
pub fn observed_sid(sid: impl Into<String>) -> Self {
Self {
status: ObservedPeerStatus::Observed,
sid: Some(sid.into()),
..Self::unobserved()
}
}
#[must_use]
pub fn with_exe(mut self, exe: Option<String>) -> Self {
self.exe = exe;
self
}
#[must_use]
pub fn with_signing_id(mut self, signing_id: Option<String>) -> Self {
self.signing_id = signing_id;
self
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct OperatorApp {
pub app: String,
pub pid: u32,
}
impl OperatorApp {
pub fn new(app: impl Into<String>, pid: u32) -> Self {
Self {
app: app.into(),
pid,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct RouteBindPeer {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub observed_peer: Option<ObservedPeer>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub operator_app: Option<OperatorApp>,
}
impl RouteBindPeer {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_observed_peer(mut self, observed_peer: Option<ObservedPeer>) -> Self {
self.observed_peer = observed_peer;
self
}
#[must_use]
pub fn with_operator_app(mut self, operator_app: Option<OperatorApp>) -> Self {
self.operator_app = operator_app;
self
}
pub fn is_empty(&self) -> bool {
self.observed_peer.is_none() && self.operator_app.is_none()
}
}
pub fn encode_route_bind(
bind: &ModuleControlRequest,
peer: &RouteBindPeer,
) -> serde_json::Result<Vec<u8>> {
if !matches!(bind, ModuleControlRequest::RouteBind { .. }) {
return Err(serde_json::Error::custom(
"peer keys can only be added to a route.bind request",
));
}
if peer.is_empty() {
return serde_json::to_vec(bind);
}
#[derive(Serialize)]
struct WithPeer<'a> {
#[serde(flatten)]
bind: &'a ModuleControlRequest,
#[serde(flatten)]
peer: &'a RouteBindPeer,
}
serde_json::to_vec(&WithPeer { bind, peer })
}