1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use std::collections::HashMap;
4use thiserror::Error;
5
6pub type LockerResult<T> = Result<T, SmartLockerError>;
7
8pub mod commands;
9pub use crate::commands::{
10 decrypt::decrypt,
11 encrypt::encrypt,
12 export::export,
13 init::{backup_key, init_locker_with_passphrase, restore_key},
14 list::list_secrets,
15 remove::remove_secret,
16 renew::renew_secret,
17};
18pub mod utils;
19
20pub use crate::utils::config::EncryptionConfig;
21
22pub use crate::utils::toolbox::{copy_to_clipboard, get_locker_dir};
23
24#[derive(Error, Debug)]
25pub enum SmartLockerError {
26 #[error("File system error: {0}")]
27 FileSystemError(String),
28 #[error("Encryption error: {0}")]
29 EncryptionError(String),
30 #[error("Decryption error: {0}")]
31 DecryptionError(String),
32 #[error("Initialization error: {0}")]
33 InitializationError(String),
34 #[error("Unknown error: {0}")]
35 UnknownError(String),
36}
37
38#[derive(Serialize, Deserialize, Clone, Debug)]
39pub struct SecretMetadata {
40 name: String,
41 created_at: u64,
42 expire_at: u64,
43 expired: bool,
44 tags: Vec<String>,
45}
46
47impl SecretMetadata {
48 pub fn field_count(instance: Option<&Self>) -> usize {
49 let json_value = if let Some(instance) = instance {
51 serde_json::to_value(instance).expect("Failed to serialize instance")
52 } else {
53 serde_json::to_value(SecretMetadata {
55 name: String::new(),
56 created_at: 0,
57 expire_at: 0,
58 expired: false,
59 tags: Vec::new(),
60 })
61 .expect("Failed to serialize default instance")
62 };
63
64 if let Value::Object(map) = json_value {
66 map.len() } else {
68 0 }
70 }
71}
72
73#[derive(Serialize, Deserialize, Debug)]
74pub struct MetadataFile {
75 pub secrets: HashMap<String, SecretMetadata>, }