artisan_keystore 2.1.1

A keystore server designed for AH
Documentation
//! Message types exchanged between the client and the keystore server.

use colored::*;
use serde::{Deserialize, Serialize}; // Add this crate to your Cargo.toml

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Request to add a new key to the store.
pub struct AddKeyCommand {
    pub id: String,
    pub value: Vec<u8>,
    pub ttl: Option<u64>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Request to fetch a key by id.
pub struct GetKeyCommand {
    pub id: String,
    pub version: Option<u32>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Request to rotate a key when it is near expiration.
pub struct MigrateKeyCommand {
    pub id: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Request to generate a key of a given length.
pub struct GenerateKeyCommand {
    pub length: Option<usize>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Generic success/failure response from the server.
pub struct CommandResponse {
    pub success: bool,
    pub message: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Wrapper around all possible commands exchanged with the server.
pub enum AppMessage {
    AddKey(AddKeyCommand),
    GetKey(GetKeyCommand),
    MigrateKey(MigrateKeyCommand),
    GenerateKey(GenerateKeyCommand),
    Response(CommandResponse),
}

// Implement Display for AddKeyCommand
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)
        )
    }
}

// Implement Display for GetKeyCommand
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)
        )
    }
}

// Implement Display for MigrateKeyCommand
impl std::fmt::Display for MigrateKeyCommand {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", "ID".green().bold(), self.id)
    }
}

// Implement Display for GenerateKeyCommand
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))
    }
}

// Implement Display for CommandResponse
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
        )
    }
}

// Implement Display for AppMessage
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),
        }
    }
}