1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use crate::{
storage::{BackupHandle, FileHandle},
utils::error_notes::ErrorNotes,
};
use anyhow::Result;
use serde::Deserialize;
use std::path::Path;
use tokio::io::AsyncReadExt;
#[derive(Clone, Deserialize)]
pub struct EnvVar {
pub key: String,
pub value: String,
}
impl EnvVar {
pub fn backup_name(value: String) -> Self {
Self::new("BACKUP_NAME".to_string(), value)
}
pub fn file_name(value: String) -> Self {
Self::new("FILE_NAME".to_string(), value)
}
pub fn file_handle(value: FileHandle) -> Self {
Self::new("FILE_HANDLE".to_string(), value)
}
pub fn backup_handle(value: BackupHandle) -> Self {
Self::new("BACKUP_HANDLE".to_string(), value)
}
pub fn new(key: String, value: String) -> Self {
Self { key, value }
}
}
#[derive(Clone, Default, Deserialize)]
pub struct Commands {
pub create_backup: String,
pub create_for_write: String,
pub open_for_read: String,
pub save_metadata_line: String,
pub list_metadata_files: String,
}
#[derive(Clone, Default, Deserialize)]
pub struct CommandAdapterConfig {
pub commands: Commands,
pub env_vars: Vec<EnvVar>,
}
impl CommandAdapterConfig {
pub async fn load_from_file(path: &Path) -> Result<Self> {
let path_str = path.to_str().unwrap_or_default();
let mut file = tokio::fs::File::open(path).await.err_notes(path_str)?;
let mut content = Vec::new();
file.read_to_end(&mut content).await.err_notes(path_str)?;
Ok(toml::from_slice(&content)?)
}
pub fn load_from_str(content: &str) -> Result<Self> {
Ok(toml::from_str(content)?)
}
}