ais_keystore_lib/shared/
messages.rs

1//! Message types exchanged between the client and the keystore server.
2
3use colored::*;
4use serde::{Deserialize, Serialize}; // Add this crate to your Cargo.toml
5
6#[derive(Serialize, Deserialize, Debug, Clone)]
7/// Request to add a new key to the store.
8pub struct AddKeyCommand {
9    pub id: String,
10    pub value: Vec<u8>,
11    pub ttl: Option<u64>,
12}
13
14#[derive(Serialize, Deserialize, Debug, Clone)]
15/// Request to fetch a key by id.
16pub struct GetKeyCommand {
17    pub id: String,
18    pub version: Option<u32>,
19}
20
21#[derive(Serialize, Deserialize, Debug, Clone)]
22/// Request to rotate a key when it is near expiration.
23pub struct MigrateKeyCommand {
24    pub id: String,
25}
26
27#[derive(Serialize, Deserialize, Debug, Clone)]
28/// Request to generate a key of a given length.
29pub struct GenerateKeyCommand {
30    pub length: Option<usize>,
31}
32
33#[derive(Serialize, Deserialize, Debug, Clone)]
34/// Generic success/failure response from the server.
35pub struct CommandResponse {
36    pub success: bool,
37    pub message: String,
38}
39
40#[derive(Serialize, Deserialize, Debug, Clone)]
41/// Wrapper around all possible commands exchanged with the server.
42pub enum AppMessage {
43    AddKey(AddKeyCommand),
44    GetKey(GetKeyCommand),
45    MigrateKey(MigrateKeyCommand),
46    GenerateKey(GenerateKeyCommand),
47    Response(CommandResponse),
48}
49
50// Implement Display for AddKeyCommand
51impl std::fmt::Display for AddKeyCommand {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        write!(
54            f,
55            "{}: {}\n{}: {}\n{}: {}",
56            "ID".green().bold(),
57            self.id,
58            "Value".blue(),
59            hex::encode(self.value.clone()),
60            "TTL".yellow(),
61            self.ttl.unwrap_or(0)
62        )
63    }
64}
65
66// Implement Display for GetKeyCommand
67impl std::fmt::Display for GetKeyCommand {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        write!(
70            f,
71            "{}: {}\n{}: {}",
72            "ID".green().bold(),
73            self.id,
74            "Version".blue(),
75            self.version.unwrap_or(0)
76        )
77    }
78}
79
80// Implement Display for MigrateKeyCommand
81impl std::fmt::Display for MigrateKeyCommand {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        write!(f, "{}: {}", "ID".green().bold(), self.id)
84    }
85}
86
87// Implement Display for GenerateKeyCommand
88impl std::fmt::Display for GenerateKeyCommand {
89    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90        write!(f, "{}: {}", "Length".yellow(), self.length.unwrap_or(32))
91    }
92}
93
94// Implement Display for CommandResponse
95impl std::fmt::Display for CommandResponse {
96    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97        let status = if self.success {
98            "Success".green().bold()
99        } else {
100            "Failure".red().bold()
101        };
102        write!(
103            f,
104            "{}: {}\n{}: {}",
105            "Status".blue(),
106            status,
107            "Message".yellow(),
108            self.message
109        )
110    }
111}
112
113// Implement Display for AppMessage
114impl std::fmt::Display for AppMessage {
115    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116        match self {
117            AppMessage::AddKey(cmd) => write!(f, "{}\n{}", "Add Key Command".bold().magenta(), cmd),
118            AppMessage::GetKey(cmd) => write!(f, "{}\n{}", "Get Key Command".bold().magenta(), cmd),
119            AppMessage::MigrateKey(cmd) => {
120                write!(f, "{}\n{}", "Migrate Key Command".bold().magenta(), cmd)
121            }
122            AppMessage::GenerateKey(cmd) => {
123                write!(f, "{}\n{}", "Generate Key Command".bold().magenta(), cmd)
124            }
125            AppMessage::Response(res) => write!(f, "{}\n{}", "Command Response".bold().cyan(), res),
126        }
127    }
128}