use super::endpoint::{Role, SchemaId};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Hello {
pub magic: String,
pub protocol_version: u16,
pub role: Role,
pub schema_id: SchemaId,
pub msg_version: u16,
pub endpoint: String,
}
impl Hello {
pub const MAGIC: &'static str = "roplat-ipc";
pub const PROTOCOL_VERSION: u16 = 1;
pub fn new(
role: Role,
schema_id: SchemaId,
msg_version: u16,
endpoint: impl Into<String>,
) -> Self {
Self {
magic: Self::MAGIC.to_string(),
protocol_version: Self::PROTOCOL_VERSION,
role,
schema_id,
msg_version,
endpoint: endpoint.into(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HelloAck {
pub magic: String,
pub protocol_version: u16,
pub accepted: bool,
pub reason: Option<String>,
}
impl HelloAck {
pub fn ok() -> Self {
Self {
magic: Hello::MAGIC.to_string(),
protocol_version: Hello::PROTOCOL_VERSION,
accepted: true,
reason: None,
}
}
pub fn reject(reason: impl Into<String>) -> Self {
Self {
magic: Hello::MAGIC.to_string(),
protocol_version: Hello::PROTOCOL_VERSION,
accepted: false,
reason: Some(reason.into()),
}
}
}