use colored::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AddKeyCommand {
pub id: String,
pub value: Vec<u8>,
pub ttl: Option<u64>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GetKeyCommand {
pub id: String,
pub version: Option<u32>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MigrateKeyCommand {
pub id: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GenerateKeyCommand {
pub length: Option<usize>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CommandResponse {
pub success: bool,
pub message: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum AppMessage {
AddKey(AddKeyCommand),
GetKey(GetKeyCommand),
MigrateKey(MigrateKeyCommand),
GenerateKey(GenerateKeyCommand),
Response(CommandResponse),
}
impl std::fmt::Display for AddKeyCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}: {}\n{}: {}\n{}: {}",
"ID".green().bold(),
self.id,
"Value".blue(),
hex::encode(self.value.clone()),
"TTL".yellow(),
self.ttl.unwrap_or(0)
)
}
}
impl std::fmt::Display for GetKeyCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}: {}\n{}: {}",
"ID".green().bold(),
self.id,
"Version".blue(),
self.version.unwrap_or(0)
)
}
}
impl std::fmt::Display for MigrateKeyCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", "ID".green().bold(), self.id)
}
}
impl std::fmt::Display for GenerateKeyCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", "Length".yellow(), self.length.unwrap_or(32))
}
}
impl std::fmt::Display for CommandResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let status = if self.success {
"Success".green().bold()
} else {
"Failure".red().bold()
};
write!(
f,
"{}: {}\n{}: {}",
"Status".blue(),
status,
"Message".yellow(),
self.message
)
}
}
impl std::fmt::Display for AppMessage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AppMessage::AddKey(cmd) => write!(f, "{}\n{}", "Add Key Command".bold().magenta(), cmd),
AppMessage::GetKey(cmd) => write!(f, "{}\n{}", "Get Key Command".bold().magenta(), cmd),
AppMessage::MigrateKey(cmd) => {
write!(f, "{}\n{}", "Migrate Key Command".bold().magenta(), cmd)
}
AppMessage::GenerateKey(cmd) => {
write!(f, "{}\n{}", "Generate Key Command".bold().magenta(), cmd)
}
AppMessage::Response(res) => write!(f, "{}\n{}", "Command Response".bold().cyan(), res),
}
}
}