Skip to main content

lora_snapshot/
options.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct SnapshotOptions {
5    pub compression: Compression,
6    pub encryption: Option<SnapshotEncryption>,
7}
8
9impl Default for SnapshotOptions {
10    fn default() -> Self {
11        Self {
12            compression: Compression::Gzip { level: 1 },
13            encryption: None,
14        }
15    }
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
19pub enum Compression {
20    None,
21    Gzip { level: u32 },
22}
23
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct EncryptionKey {
26    pub key_id: String,
27    pub key: [u8; 32],
28}
29
30impl EncryptionKey {
31    pub fn new(key_id: impl Into<String>, key: [u8; 32]) -> Self {
32        Self {
33            key_id: key_id.into(),
34            key,
35        }
36    }
37}
38
39#[derive(Debug, Clone, PartialEq, Eq)]
40pub struct SnapshotPassword {
41    pub key_id: String,
42    pub(crate) password: Vec<u8>,
43    pub params: PasswordKdfParams,
44}
45
46impl SnapshotPassword {
47    pub fn new(key_id: impl Into<String>, password: impl AsRef<[u8]>) -> Self {
48        Self {
49            key_id: key_id.into(),
50            password: password.as_ref().to_vec(),
51            params: PasswordKdfParams::interactive(),
52        }
53    }
54
55    pub fn with_params(
56        key_id: impl Into<String>,
57        password: impl AsRef<[u8]>,
58        params: PasswordKdfParams,
59    ) -> Self {
60        Self {
61            key_id: key_id.into(),
62            password: password.as_ref().to_vec(),
63            params,
64        }
65    }
66}
67
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
69pub struct PasswordKdfParams {
70    pub memory_cost_kib: u32,
71    pub time_cost: u32,
72    pub parallelism: u32,
73}
74
75impl PasswordKdfParams {
76    pub fn interactive() -> Self {
77        Self {
78            memory_cost_kib: 19 * 1024,
79            time_cost: 2,
80            parallelism: 1,
81        }
82    }
83}
84
85#[derive(Debug, Clone, PartialEq, Eq)]
86pub enum SnapshotEncryption {
87    Key(EncryptionKey),
88    Password(SnapshotPassword),
89}
90
91impl From<EncryptionKey> for SnapshotEncryption {
92    fn from(value: EncryptionKey) -> Self {
93        Self::Key(value)
94    }
95}
96
97impl From<SnapshotPassword> for SnapshotEncryption {
98    fn from(value: SnapshotPassword) -> Self {
99        Self::Password(value)
100    }
101}
102
103pub type SnapshotCredentials = SnapshotEncryption;