t_rust_less_lib/api/
command.rs

1use crate::memguard::SecretBytes;
2use crate::secrets_store::{SecretStoreError, SecretStoreResult};
3use crate::service::{ServiceError, ServiceResult};
4use serde::{Deserialize, Serialize};
5use zeroize::Zeroize;
6
7use super::{
8  ClipboardProviding, Event, Identity, PasswordGeneratorParam, Secret, SecretList, SecretListFilter, SecretVersion,
9  Status, StoreConfig,
10};
11
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Zeroize)]
13#[allow(clippy::large_enum_variant)]
14#[zeroize(drop)]
15pub enum Command {
16  ListStores,
17  UpsertStoreConfig(StoreConfig),
18  DeleteStoreConfig(String),
19  GetDefaultStore,
20  SetDefaultStore(String),
21  GenerateId,
22  GeneratePassword(PasswordGeneratorParam),
23  PollEvents(u64),
24
25  Status(String),
26  Lock(String),
27  Unlock {
28    store_name: String,
29    identity_id: String,
30    passphrase: SecretBytes,
31  },
32  Identities(String),
33  AddIdentity {
34    store_name: String,
35    identity: Identity,
36    passphrase: SecretBytes,
37  },
38  ChangePassphrase {
39    store_name: String,
40    passphrase: SecretBytes,
41  },
42  List {
43    store_name: String,
44    filter: SecretListFilter,
45  },
46  UpdateIndex(String),
47  Add {
48    store_name: String,
49    secret_version: SecretVersion,
50  },
51  Get {
52    store_name: String,
53    secret_id: String,
54  },
55  GetVersion {
56    store_name: String,
57    block_id: String,
58  },
59
60  SecretToClipboard {
61    store_name: String,
62    block_id: String,
63    properties: Vec<String>,
64  },
65  ClipboardIsDone,
66  ClipboardCurrentlyProviding,
67  ClipboardProvideNext,
68  ClipboardDestroy,
69}
70
71#[derive(Debug, Serialize, Deserialize, Zeroize)]
72#[allow(clippy::large_enum_variant)]
73#[zeroize(drop)]
74pub enum CommandResult {
75  Void,
76  Bool(bool),
77  String(String),
78  Configs(Vec<StoreConfig>),
79  Events(Vec<Event>),
80  Status(Status),
81  SecretList(SecretList),
82  Identities(Vec<Identity>),
83  Secret(Secret),
84  SecretVersion(SecretVersion),
85  ClipboardProviding(ClipboardProviding),
86  SecretStoreError(SecretStoreError),
87  ServiceError(ServiceError),
88}
89
90impl From<CommandResult> for ServiceResult<()> {
91  fn from(result: CommandResult) -> Self {
92    match result {
93      CommandResult::Void => Ok(()),
94      CommandResult::ServiceError(ref error) => Err(error.clone()),
95      CommandResult::SecretStoreError(ref error) => Err(ServiceError::SecretsStore(error.clone())),
96      _ => Err(ServiceError::IO("Invalid command result".to_string())),
97    }
98  }
99}
100
101impl From<ServiceResult<()>> for CommandResult {
102  fn from(result: ServiceResult<()>) -> Self {
103    match result {
104      Ok(_) => CommandResult::Void,
105      Err(error) => CommandResult::ServiceError(error),
106    }
107  }
108}
109
110impl From<CommandResult> for ServiceResult<bool> {
111  fn from(result: CommandResult) -> Self {
112    match result {
113      CommandResult::Bool(value) => Ok(value),
114      CommandResult::ServiceError(ref error) => Err(error.clone()),
115      CommandResult::SecretStoreError(ref error) => Err(ServiceError::SecretsStore(error.clone())),
116      _ => Err(ServiceError::IO("Invalid command result".to_string())),
117    }
118  }
119}
120
121impl From<ServiceResult<bool>> for CommandResult {
122  fn from(result: ServiceResult<bool>) -> Self {
123    match result {
124      Ok(value) => CommandResult::Bool(value),
125      Err(error) => CommandResult::ServiceError(error),
126    }
127  }
128}
129
130impl From<CommandResult> for ServiceResult<String> {
131  fn from(result: CommandResult) -> Self {
132    match &result {
133      CommandResult::String(value) => Ok(value.clone()),
134      CommandResult::ServiceError(ref error) => Err(error.clone()),
135      CommandResult::SecretStoreError(error) => Err(ServiceError::SecretsStore(error.clone())),
136      _ => Err(ServiceError::IO("Invalid command result".to_string())),
137    }
138  }
139}
140
141impl From<ServiceResult<String>> for CommandResult {
142  fn from(result: ServiceResult<String>) -> Self {
143    match result {
144      Ok(value) => CommandResult::String(value),
145      Err(error) => CommandResult::ServiceError(error),
146    }
147  }
148}
149
150impl From<CommandResult> for ServiceResult<Option<String>> {
151  fn from(result: CommandResult) -> Self {
152    match &result {
153      CommandResult::Void => Ok(None),
154      CommandResult::String(value) => Ok(Some(value.clone())),
155      CommandResult::ServiceError(error) => Err(error.clone()),
156      CommandResult::SecretStoreError(error) => Err(ServiceError::SecretsStore(error.clone())),
157      _ => Err(ServiceError::IO("Invalid command result".to_string())),
158    }
159  }
160}
161
162impl From<ServiceResult<Option<String>>> for CommandResult {
163  fn from(result: ServiceResult<Option<String>>) -> Self {
164    match result {
165      Ok(Some(value)) => CommandResult::String(value),
166      Ok(None) => CommandResult::Void,
167      Err(error) => CommandResult::ServiceError(error),
168    }
169  }
170}
171
172impl From<CommandResult> for ServiceResult<Vec<StoreConfig>> {
173  fn from(result: CommandResult) -> Self {
174    match &result {
175      CommandResult::Configs(value) => Ok(value.clone()),
176      CommandResult::ServiceError(error) => Err(error.clone()),
177      CommandResult::SecretStoreError(error) => Err(ServiceError::SecretsStore(error.clone())),
178      _ => Err(ServiceError::IO("Invalid command result".to_string())),
179    }
180  }
181}
182
183impl From<ServiceResult<Vec<StoreConfig>>> for CommandResult {
184  fn from(result: ServiceResult<Vec<StoreConfig>>) -> Self {
185    match result {
186      Ok(value) => CommandResult::Configs(value),
187      Err(error) => CommandResult::ServiceError(error),
188    }
189  }
190}
191
192impl From<CommandResult> for ServiceResult<Vec<Event>> {
193  fn from(result: CommandResult) -> Self {
194    match &result {
195      CommandResult::Events(value) => Ok(value.clone()),
196      CommandResult::ServiceError(error) => Err(error.clone()),
197      CommandResult::SecretStoreError(error) => Err(ServiceError::SecretsStore(error.clone())),
198      _ => Err(ServiceError::IO("Invalid command result".to_string())),
199    }
200  }
201}
202
203impl From<ServiceResult<Vec<Event>>> for CommandResult {
204  fn from(result: ServiceResult<Vec<Event>>) -> Self {
205    match result {
206      Ok(value) => CommandResult::Events(value),
207      Err(error) => CommandResult::ServiceError(error),
208    }
209  }
210}
211
212impl From<CommandResult> for ServiceResult<Option<ClipboardProviding>> {
213  fn from(result: CommandResult) -> Self {
214    match &result {
215      CommandResult::ClipboardProviding(clipboard_providing) => Ok(Some(clipboard_providing.clone())),
216      CommandResult::Void => Ok(None),
217      CommandResult::ServiceError(error) => Err(error.clone()),
218      _ => Err(ServiceError::IO("Invalid command result".to_string())),
219    }
220  }
221}
222
223impl From<ServiceResult<Option<ClipboardProviding>>> for CommandResult {
224  fn from(result: ServiceResult<Option<ClipboardProviding>>) -> Self {
225    match result {
226      Ok(Some(clipboard_providing)) => CommandResult::ClipboardProviding(clipboard_providing),
227      Ok(None) => CommandResult::Void,
228      Err(error) => CommandResult::ServiceError(error),
229    }
230  }
231}
232
233impl From<CommandResult> for SecretStoreResult<()> {
234  fn from(result: CommandResult) -> Self {
235    match &result {
236      CommandResult::Void => Ok(()),
237      CommandResult::SecretStoreError(error) => Err(error.clone()),
238      _ => Err(SecretStoreError::IO("Invalid command result".to_string())),
239    }
240  }
241}
242
243impl From<SecretStoreResult<()>> for CommandResult {
244  fn from(result: SecretStoreResult<()>) -> Self {
245    match result {
246      Ok(_) => CommandResult::Void,
247      Err(error) => CommandResult::SecretStoreError(error),
248    }
249  }
250}
251
252impl From<CommandResult> for SecretStoreResult<String> {
253  fn from(result: CommandResult) -> Self {
254    match &result {
255      CommandResult::String(value) => Ok(value.clone()),
256      CommandResult::SecretStoreError(error) => Err(error.clone()),
257      _ => Err(SecretStoreError::IO("Invalid command result".to_string())),
258    }
259  }
260}
261
262impl From<SecretStoreResult<String>> for CommandResult {
263  fn from(result: SecretStoreResult<String>) -> Self {
264    match result {
265      Ok(value) => CommandResult::String(value),
266      Err(error) => CommandResult::SecretStoreError(error),
267    }
268  }
269}
270
271impl From<CommandResult> for SecretStoreResult<Status> {
272  fn from(result: CommandResult) -> Self {
273    match &result {
274      CommandResult::Status(value) => Ok(value.clone()),
275      CommandResult::SecretStoreError(error) => Err(error.clone()),
276      _ => Err(SecretStoreError::IO("Invalid command result".to_string())),
277    }
278  }
279}
280
281impl From<SecretStoreResult<Status>> for CommandResult {
282  fn from(result: SecretStoreResult<Status>) -> Self {
283    match result {
284      Ok(value) => CommandResult::Status(value),
285      Err(error) => CommandResult::SecretStoreError(error),
286    }
287  }
288}
289
290impl From<CommandResult> for SecretStoreResult<Vec<Identity>> {
291  fn from(result: CommandResult) -> Self {
292    match &result {
293      CommandResult::Identities(value) => Ok(value.clone()),
294      CommandResult::SecretStoreError(error) => Err(error.clone()),
295      _ => Err(SecretStoreError::IO("Invalid command result".to_string())),
296    }
297  }
298}
299
300impl From<SecretStoreResult<Vec<Identity>>> for CommandResult {
301  fn from(result: SecretStoreResult<Vec<Identity>>) -> Self {
302    match result {
303      Ok(value) => CommandResult::Identities(value),
304      Err(error) => CommandResult::SecretStoreError(error),
305    }
306  }
307}
308
309impl From<CommandResult> for SecretStoreResult<SecretList> {
310  fn from(result: CommandResult) -> Self {
311    match &result {
312      CommandResult::SecretList(value) => Ok(value.clone()),
313      CommandResult::SecretStoreError(error) => Err(error.clone()),
314      _ => Err(SecretStoreError::IO("Invalid command result".to_string())),
315    }
316  }
317}
318
319impl From<SecretStoreResult<SecretList>> for CommandResult {
320  fn from(result: SecretStoreResult<SecretList>) -> Self {
321    match result {
322      Ok(value) => CommandResult::SecretList(value),
323      Err(error) => CommandResult::SecretStoreError(error),
324    }
325  }
326}
327
328impl From<CommandResult> for SecretStoreResult<Secret> {
329  fn from(result: CommandResult) -> Self {
330    match &result {
331      CommandResult::Secret(value) => Ok(value.clone()),
332      CommandResult::SecretStoreError(error) => Err(error.clone()),
333      _ => Err(SecretStoreError::IO("Invalid command result".to_string())),
334    }
335  }
336}
337
338impl From<SecretStoreResult<Secret>> for CommandResult {
339  fn from(result: SecretStoreResult<Secret>) -> Self {
340    match result {
341      Ok(value) => CommandResult::Secret(value),
342      Err(error) => CommandResult::SecretStoreError(error),
343    }
344  }
345}
346
347impl From<CommandResult> for SecretStoreResult<SecretVersion> {
348  fn from(result: CommandResult) -> Self {
349    match &result {
350      CommandResult::SecretVersion(value) => Ok(value.clone()),
351      CommandResult::SecretStoreError(error) => Err(error.clone()),
352      _ => Err(SecretStoreError::IO("Invalid command result".to_string())),
353    }
354  }
355}
356
357impl From<SecretStoreResult<SecretVersion>> for CommandResult {
358  fn from(result: SecretStoreResult<SecretVersion>) -> Self {
359    match result {
360      Ok(value) => CommandResult::SecretVersion(value),
361      Err(error) => CommandResult::SecretStoreError(error),
362    }
363  }
364}