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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
pub mod url {
use std::{borrow::Borrow, string::ToString};
use url::form_urlencoded;
pub fn construct_ep<E, Q>(ep: E, query: Option<Q>) -> String
where
E: Into<String>,
Q: AsRef<str>,
{
let mut ep = ep.into();
if let Some(query) = query {
append_query(&mut ep, query);
}
ep
}
pub fn append_query<Q>(ep: &mut String, query: Q)
where
Q: AsRef<str>,
{
ep.push('?');
ep.push_str(query.as_ref());
}
pub fn encoded_pair<K, V>(key: K, val: V) -> String
where
K: AsRef<str> + 'static,
V: ToString,
{
form_urlencoded::Serializer::new(String::new())
.append_pair(key.as_ref(), &val.to_string())
.finish()
}
pub fn encoded_pairs<I, K, V>(iter: I) -> String
where
I: IntoIterator,
I::Item: Borrow<(K, V)>,
K: AsRef<str>,
V: AsRef<str>,
{
iter.into_iter()
.fold(
form_urlencoded::Serializer::new(String::new()),
|mut acc, v| {
let &(ref k, ref v) = v.borrow();
let k = k.as_ref();
let v = v.as_ref();
if v.is_empty() {
acc.append_key_only(k);
} else {
acc.append_pair(k, v);
}
acc
},
)
.finish()
}
}
#[cfg(feature = "chrono")]
pub mod datetime {
use chrono::{DateTime, Utc};
use serde::Deserialize;
pub(crate) fn datetime_from_unix_timestamp<'de, D>(
deserializer: D,
) -> Result<DateTime<Utc>, D::Error>
where
D: serde::Deserializer<'de>,
{
let timestamp = chrono::NaiveDateTime::from_timestamp(i64::deserialize(deserializer)?, 0);
Ok(DateTime::<Utc>::from_utc(timestamp, Utc))
}
pub(crate) fn datetime_from_nano_timestamp<'de, D>(
deserializer: D,
) -> Result<DateTime<Utc>, D::Error>
where
D: serde::Deserializer<'de>,
{
let timestamp_nano = u64::deserialize(deserializer)?;
let timestamp = chrono::NaiveDateTime::from_timestamp(
(timestamp_nano / 1_000_000_000) as i64,
(timestamp_nano % 1_000_000_000) as u32,
);
Ok(DateTime::<Utc>::from_utc(timestamp, Utc))
}
}
pub mod tarball {
use flate2::{write::GzEncoder, Compression};
use std::{
fs::{self, File},
io::{self, Write},
path::{Path, MAIN_SEPARATOR},
};
use tar::Builder;
pub fn dir<W, P>(buf: W, path: P) -> io::Result<()>
where
W: Write,
P: AsRef<Path>,
{
let mut archive = Builder::new(GzEncoder::new(buf, Compression::best()));
fn bundle<F>(dir: &Path, f: &mut F, bundle_dir: bool) -> io::Result<()>
where
F: FnMut(&Path) -> io::Result<()>,
{
if fs::metadata(dir)?.is_dir() {
if bundle_dir {
f(&dir)?;
}
for entry in fs::read_dir(dir)? {
let entry = entry?;
if fs::metadata(entry.path())?.is_dir() {
bundle(&entry.path(), f, true)?;
} else {
f(&entry.path().as_path())?;
}
}
}
Ok(())
}
{
let path = path.as_ref();
let base_path = path.canonicalize()?;
let mut base_path_str = base_path
.to_str()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "invalid base path"))?
.to_owned();
if let Some(last) = base_path_str.chars().last() {
if last != MAIN_SEPARATOR {
base_path_str.push(MAIN_SEPARATOR)
}
}
let mut append = |path: &Path| {
let canonical = path.canonicalize()?;
let relativized = canonical
.to_str()
.ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidInput, "invalid canonicalized path")
})?
.trim_start_matches(&base_path_str[..]);
if path.is_dir() {
archive.append_dir(Path::new(relativized), &canonical)?
} else {
archive.append_file(Path::new(relativized), &mut File::open(&canonical)?)?
}
Ok(())
};
bundle(path, &mut append, false)?;
}
archive.finish()?;
Ok(())
}
}