ais_keystore_lib/shared/
messages.rs1use colored::*;
4use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug, Clone)]
7pub struct AddKeyCommand {
9 pub id: String,
10 pub value: Vec<u8>,
11 pub ttl: Option<u64>,
12}
13
14#[derive(Serialize, Deserialize, Debug, Clone)]
15pub struct GetKeyCommand {
17 pub id: String,
18 pub version: Option<u32>,
19}
20
21#[derive(Serialize, Deserialize, Debug, Clone)]
22pub struct MigrateKeyCommand {
24 pub id: String,
25}
26
27#[derive(Serialize, Deserialize, Debug, Clone)]
28pub struct GenerateKeyCommand {
30 pub length: Option<usize>,
31}
32
33#[derive(Serialize, Deserialize, Debug, Clone)]
34pub struct CommandResponse {
36 pub success: bool,
37 pub message: String,
38}
39
40#[derive(Serialize, Deserialize, Debug, Clone)]
41pub enum AppMessage {
43 AddKey(AddKeyCommand),
44 GetKey(GetKeyCommand),
45 MigrateKey(MigrateKeyCommand),
46 GenerateKey(GenerateKeyCommand),
47 Response(CommandResponse),
48}
49
50impl 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
66impl 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
80impl 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
87impl 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
94impl 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
113impl 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}