bitwarden_uniffi/tool/
sends.rs

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
94
95
96
97
98
99
use std::{path::Path, sync::Arc};

use bitwarden_send::{Send, SendClientExt, SendListView, SendView};

use crate::{error::Error, Client, Result};

#[derive(uniffi::Object)]
pub struct SendClient(pub Arc<Client>);

#[uniffi::export]
impl SendClient {
    /// Encrypt send
    pub fn encrypt(&self, send: SendView) -> Result<Send> {
        Ok(self
            .0
             .0
            .sends()
            .encrypt(send)
            .map_err(Error::SendEncrypt)?)
    }

    /// Encrypt a send file in memory
    pub fn encrypt_buffer(&self, send: Send, buffer: Vec<u8>) -> Result<Vec<u8>> {
        Ok(self
            .0
             .0
            .sends()
            .encrypt_buffer(send, &buffer)
            .map_err(Error::SendEncrypt)?)
    }

    /// Encrypt a send file located in the file system
    pub fn encrypt_file(
        &self,
        send: Send,
        decrypted_file_path: String,
        encrypted_file_path: String,
    ) -> Result<()> {
        Ok(self
            .0
             .0
            .sends()
            .encrypt_file(
                send,
                Path::new(&decrypted_file_path),
                Path::new(&encrypted_file_path),
            )
            .map_err(Error::SendEncryptFile)?)
    }

    /// Decrypt send
    pub fn decrypt(&self, send: Send) -> Result<SendView> {
        Ok(self
            .0
             .0
            .sends()
            .decrypt(send)
            .map_err(Error::SendDecrypt)?)
    }

    /// Decrypt send list
    pub fn decrypt_list(&self, sends: Vec<Send>) -> Result<Vec<SendListView>> {
        Ok(self
            .0
             .0
            .sends()
            .decrypt_list(sends)
            .map_err(Error::SendDecrypt)?)
    }

    /// Decrypt a send file in memory
    pub fn decrypt_buffer(&self, send: Send, buffer: Vec<u8>) -> Result<Vec<u8>> {
        Ok(self
            .0
             .0
            .sends()
            .decrypt_buffer(send, &buffer)
            .map_err(Error::SendDecrypt)?)
    }

    /// Decrypt a send file located in the file system
    pub fn decrypt_file(
        &self,
        send: Send,
        encrypted_file_path: String,
        decrypted_file_path: String,
    ) -> Result<()> {
        Ok(self
            .0
             .0
            .sends()
            .decrypt_file(
                send,
                Path::new(&encrypted_file_path),
                Path::new(&decrypted_file_path),
            )
            .map_err(Error::SendDecryptFile)?)
    }
}