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
100
101
//! Destination types for data packages.
use std::{
fmt::Debug,
path::{Path, PathBuf},
};
/// Destination type for data packages.
#[derive(Debug)]
pub enum Destination {
/// Location in the local filesystem.
Local(Local),
/// Standard output.
Stdout,
/// Location on a SFTP server.
Sftp(Sftp),
/// Location on an S3-compatible object store.
S3(crate::remote::s3::Location),
}
impl From<Local> for Destination {
fn from(opts: Local) -> Self {
Destination::Local(opts)
}
}
impl From<Sftp> for Destination {
fn from(sftp: Sftp) -> Self {
Destination::Sftp(sftp)
}
}
impl From<crate::remote::s3::Location> for Destination {
fn from(s3: crate::remote::s3::Location) -> Self {
Destination::S3(s3)
}
}
#[derive(Debug, Clone)]
/// Local destination.
pub struct Local {
/// Path to the directory where the data package will be stored.
pub(crate) path: PathBuf,
/// Name of the encrypted data package.
pub(crate) name: Option<String>,
}
impl Local {
/// Creates a new local destination.
///
/// `path` must be a directory. If `name` is `None`, a package name will be generated.
/// If `name` is `Some`, it will be used as the package name.
pub fn new(
path: impl Into<PathBuf>,
name: Option<impl Into<String>>,
) -> Result<Self, std::io::Error> {
Ok(Self {
path: path.into().canonicalize()?,
name: name.map(Into::into),
})
}
pub(crate) fn package_name(
&self,
timestamp: &chrono::DateTime<chrono::Utc>,
prefix: Option<&str>,
) -> String {
self.name.as_ref().map_or_else(
|| crate::package::generate_package_name(timestamp, prefix),
Into::into,
)
}
}
/// SFTP destination.
#[derive(Debug)]
pub struct Sftp {
/// Client object to connect with SFTP server.
client: crate::remote::sftp::Client,
/// Path on the remote SFTP host where encrypted files will be stored.
base_path: PathBuf,
}
impl Sftp {
/// Creates a new SFTP destination.
pub fn new(client: crate::remote::sftp::Client, base_path: impl Into<PathBuf>) -> Self {
Self {
client,
base_path: base_path.into(),
}
}
/// Returns the SFTP client.
pub fn client(&self) -> &crate::remote::sftp::Client {
&self.client
}
/// Returns the base path.
pub fn base_path(&self) -> &Path {
&self.base_path
}
}