extensible_encrypter/decrypter/
builder.rs

1pub struct Decrypter {
2    salt: Vec<u8>,
3    nonce: Vec<u8>,
4    ciphertext: Vec<u8>,
5}
6
7impl Decrypter {
8    pub fn from_payload(payload: &impl DecrypterPayload) -> Self {
9        Self {
10            salt: payload.salt().clone(),
11            nonce: payload.nonce().clone(),
12            ciphertext: payload.ciphertext().clone(),
13        }
14    }
15}
16
17pub trait DecrypterPayload {
18    fn salt(&self) -> &Vec<u8>;
19    fn nonce(&self) -> &Vec<u8>;
20    fn ciphertext(&self) -> &Vec<u8>;
21}
22
23impl DecrypterPayload for &mut Decrypter {
24    fn salt(&self) -> &Vec<u8> {
25        &self.salt
26    }
27
28    fn nonce(&self) -> &Vec<u8> {
29        &self.nonce
30    }
31
32    fn ciphertext(&self) -> &Vec<u8> {
33        &self.ciphertext
34    }
35}
36
37#[derive(Debug, Default)]
38pub struct DecrypterBuilder {
39    salt: Vec<u8>,
40    nonce: Vec<u8>,
41    ciphertext: Vec<u8>,
42}
43
44impl DecrypterBuilder {
45    pub fn new() -> Self {
46        Self {
47            salt: Vec::new(),
48            nonce: Vec::new(),
49            ciphertext: Vec::new(),
50        }
51    }
52
53    pub fn salt(mut self, key: Vec<u8>) -> Self {
54        self.salt = key;
55        self
56    }
57
58    pub fn nonce(mut self, nonce: Vec<u8>) -> Self {
59        self.nonce = nonce;
60        self
61    }
62
63    pub fn ciphertext(mut self, ciphertext: Vec<u8>) -> Self {
64        self.ciphertext = ciphertext;
65        self
66    }
67
68    pub fn build(self) -> Decrypter {
69        Decrypter {
70            salt: self.salt,
71            nonce: self.nonce,
72            ciphertext: self.ciphertext,
73        }
74    }
75}