use crate::command::Response;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum RPC {
Command(CommandInvocation),
CommandResponse(CommandResponse),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CommandInvocation {
pub name: String,
pub args: Vec<String>,
pub reply_id: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CommandResponse {
pub success: bool,
pub response: Response,
pub reply_id: u32,
pub num_attachments: u32,
}
impl CommandResponse {
pub fn new(success: bool, response: Response, reply_id: u32) -> Self {
CommandResponse {
success,
response,
reply_id,
num_attachments: 0,
}
}
}
impl CommandInvocation {
pub fn new(name: String, args: Vec<String>, reply_id: u32) -> Self {
CommandInvocation {
name,
args,
reply_id,
}
}
}
impl Display for RPC {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RPC::Command(cmd) => write!(f, "Command({})", cmd),
RPC::CommandResponse(resp) => write!(f, "CommandResponse({})", resp),
}
}
}
impl Display for CommandInvocation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.args.is_empty() {
write!(f, "{} [id={}]", self.name, self.reply_id)
} else {
write!(f, "{} {:?} [id={}]", self.name, self.args, self.reply_id)
}
}
}
impl Display for CommandResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let status = if self.success { "ok" } else { "err" };
write!(f, "[id={}] {}: {}", self.reply_id, status, self.response)
}
}