-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake.rs
228 lines (202 loc) · 6.41 KB
/
fake.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use chrono::{offset::Utc, DateTime, Datelike, TimeZone, Timelike};
use rand::{
distributions::{Alphanumeric, DistString},
prelude::*,
Fill,
};
use typed_html::{dom::DOMTree, html, text, types::Metadata};
use crate::prelude::*;
// hashes path and seed together
fn hash_path_seed<T: Hash>(seed: T, path: &str) -> u64 {
let mut hasher = DefaultHasher::new();
seed.hash(&mut hasher);
path.hash(&mut hasher);
hasher.finish()
}
/// Return a rendered listing links provided with the same named
/// subpath. The seed is used with the provided path to deterministically
/// generate random directories and folders.
fn gen_fake_nodes<T: Hash>(seed: T, path: &str) -> Vec<Node> {
let mut rng = StdRng::seed_from_u64(hash_path_seed(seed, path));
let files = rng.gen_range(2..=8);
let folders = rng.gen_range(4..=15);
(0..folders)
.map(|_| Node::Right(Default::default()))
.chain((0..files).map(|_| Node::Left(Default::default())))
.map(|mut n| {
rng.fill(&mut n);
n
})
.collect()
}
pub fn gen_fake_listing<T: Hash>(seed: T, path: &str) -> String {
let nodes = gen_fake_nodes(seed, path);
let basepath = if path == "" {
"/".to_string()
} else if path.ends_with("/") {
path.to_string()
} else {
path.to_owned() + "/"
};
let doc: DOMTree<String> = html!(
<html>
<head>
<title>{ text!("Index of {}", basepath) }</title>
<meta name=Metadata::Description content="Generated Directory Listing"/>
</head>
<body>
<h1>{ text!("Index of {}", basepath) }</h1>
<hr />
<pre>
<a href="../">"../"</a> "\n"
{ nodes.into_iter().map(|n| {
let line = format!("{: <40}{: >20}{: >20}", n.name(), n.modified_at(), n.size().map(|s| s.to_string()).unwrap_or_else(|| "-".to_string()));
// take the non-name portion of the line which is appropriately
// space padded now
let line = line[n.name().len()..].to_string();
html!(
<span>
<a href=n.name()>{ text!("{}", n.name()) }</a>
<span>
{ text!("{}", line) }
</span>
"\n"
</span>
)
})
}
</pre>
<hr />
</body>
</html>
);
doc.to_string()
}
type Node = Either<File, Folder>;
#[derive(Debug)]
enum Either<L, R> {
Left(L),
Right(R),
}
#[derive(Debug, Clone, Default)]
struct File {
name: String,
modified_at: DateTime<Utc>,
size: usize,
}
#[derive(Debug, Clone, Default)]
struct Folder {
name: String,
modified_at: DateTime<Utc>,
}
const MODIFIED_FORMAT: &str = "%d-%m-%Y %H:%M";
impl Node {
pub fn name(&self) -> String {
match self {
Node::Left(n) => n.name.to_owned(),
Node::Right(n) => n.name.to_owned() + "/",
}
}
pub fn modified_at(&self) -> String {
let dt = match self {
Node::Left(n) => n.modified_at.format(MODIFIED_FORMAT),
Node::Right(n) => n.modified_at.format(MODIFIED_FORMAT),
};
format!("{}", dt)
}
pub fn size(&self) -> Option<usize> {
match self {
Node::Left(n) => Some(n.size),
Node::Right(_) => None,
}
}
}
impl Fill for Node {
fn try_fill<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Result<(), rand::Error> {
match self {
Node::Left(ref mut f) => {
f.name = string_of_size(rng, 4, 10) + "." + &string_of_size(rng, 1, 3);
f.modified_at = plausible_datetime(rng).unwrap_or_default();
f.size = rng.gen_range(0..=(32 * 1024 * 1024));
}
Node::Right(ref mut n) => {
n.name = string_of_size(rng, 6, 15);
n.modified_at = plausible_datetime(rng).unwrap_or_default();
}
}
Ok(())
}
}
fn string_of_size<R: Rng + ?Sized>(rng: &mut R, min: usize, max: usize) -> String {
let size = rng.gen_range(min..=max);
Alphanumeric.sample_string(rng, size)
}
/// creates a plausible datetime from 2000 to now which does not go into the future.
fn plausible_datetime<R: Rng + ?Sized>(rng: &mut R) -> Result<DateTime<Utc>> {
let now = Utc::now();
let year = rng.gen_range(2000..=now.year());
let month = if year == now.year() {
rng.gen_range(1..=now.month())
} else {
rng.gen_range(1..=12)
};
let day = if year == now.year() && month == now.month() {
rng.gen_range(1..=now.day())
} else {
rng.gen_range(1..=get_days_in_month(year, month)?)
};
let hour = if year == now.year() && month == now.month() && day == now.day() {
rng.gen_range(0..=now.hour())
} else {
rng.gen_range(0..24)
};
let minute =
if year == now.year() && month == now.month() && day == now.day() && hour == now.hour() {
rng.gen_range(0..=now.minute())
} else {
rng.gen_range(0..60)
};
let second = if year == now.year()
&& month == now.month()
&& day == now.day()
&& hour == now.hour()
&& minute == now.minute()
{
rng.gen_range(0..=now.second())
} else {
rng.gen_range(0..60)
};
Utc.with_ymd_and_hms(year, month, day, hour, minute, second)
.latest()
.ok_or_else(|| anyhow!("failed to generate random date"))
}
// tyty https://stackoverflow.com/a/58188385
fn get_days_in_month(year: i32, month: u32) -> Result<u32> {
chrono::NaiveDate::from_ymd_opt(
match month {
12 => year + 1,
_ => year,
},
match month {
12 => 1,
_ => month + 1,
},
1,
)
.ok_or_else(|| {
anyhow!(
"failed to convert yy/mm {}/{} to get days in month",
year,
month
)
})?
.signed_duration_since(
chrono::NaiveDate::from_ymd_opt(year, month, 1)
.ok_or_else(|| anyhow!("to create signed duration since {}/{}/{}", year, month, 1))?,
)
.num_days()
.try_into()
.map_err(|e| anyhow!("failed to subtract for yy/mm {}/{}: {}", year, month, e))
}