1pub mod project;
2
3use serde::{Deserialize, Serialize};
4use std::fs;
5use std::path::Path;
6
7use crate::error::Result;
8
9#[derive(Debug, Clone, Serialize, Deserialize, Default)]
11pub struct Config {
12 #[serde(default)]
13 pub vault: VaultConfig,
14 #[serde(default)]
15 pub audit: AuditConfig,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct VaultConfig {
20 #[serde(default = "default_auth_method")]
22 pub auth_method: String,
23 pub keyfile: Option<String>,
25}
26
27impl Default for VaultConfig {
28 fn default() -> Self {
29 Self {
30 auth_method: default_auth_method(),
31 keyfile: None,
32 }
33 }
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct AuditConfig {
38 #[serde(default = "default_true")]
39 pub enabled: bool,
40}
41
42impl Default for AuditConfig {
43 fn default() -> Self {
44 Self { enabled: true }
45 }
46}
47
48fn default_auth_method() -> String {
49 "passphrase".to_string()
50}
51
52fn default_true() -> bool {
53 true
54}
55
56impl Config {
57 pub fn load(path: &Path) -> Result<Self> {
59 if !path.exists() {
60 return Ok(Self::default());
61 }
62 let content = fs::read_to_string(path)?;
63 let config: Config = toml::from_str(&content)
64 .map_err(|e| crate::error::AuthyError::Other(format!("Invalid config: {}", e)))?;
65 Ok(config)
66 }
67
68 pub fn save(&self, path: &Path) -> Result<()> {
70 let content = toml::to_string_pretty(self)
71 .map_err(|e| crate::error::AuthyError::Other(format!("Config serialize error: {}", e)))?;
72 if let Some(dir) = path.parent() {
73 fs::create_dir_all(dir)?;
74 }
75 fs::write(path, content)?;
76 Ok(())
77 }
78}