Skip to content

Commit 500934d

Browse files
committed
Started Tera migration
1 parent 4dc57be commit 500934d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2755
-2056
lines changed

Cargo.lock

Lines changed: 253 additions & 104 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ serde_json = "1.0"
4747
# iron dependencies
4848
iron = "0.5"
4949
router = "0.5"
50-
handlebars-iron = "0.25"
5150
params = "0.8"
5251
staticfile = { version = "0.4", features = [ "cache" ] }
5352
tempfile = "3.1.0"
5453

54+
# Templating
55+
tera = { version = "1.3.0", features = ["builtins"] }
56+
57+
# Template hot-reloading
58+
arc-swap = "0.4.6"
59+
5560
[target.'cfg(not(windows))'.dependencies]
5661
libc = "0.2"
5762

@@ -71,6 +76,7 @@ kuchiki = "0.8"
7176
criterion = "0.3"
7277
rand = "0.7.3"
7378

79+
7480
[[bench]]
7581
name = "html5ever"
7682
harness = false

src/bin/cratesfyi.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ enum CommandLine {
5252
StartWebServer {
5353
#[structopt(name = "SOCKET_ADDR", default_value = "0.0.0.0:3000")]
5454
socket_addr: String,
55+
/// Reload templates when they're changed
56+
#[structopt(long = "reload")]
57+
reload: bool,
5558
},
5659

5760
/// Starts cratesfyi daemon
@@ -78,8 +81,11 @@ impl CommandLine {
7881
pub fn handle_args(self) {
7982
match self {
8083
Self::Build(build) => build.handle_args(),
81-
Self::StartWebServer { socket_addr } => {
82-
Server::start(Some(&socket_addr));
84+
Self::StartWebServer {
85+
socket_addr,
86+
reload,
87+
} => {
88+
Server::start(Some(&socket_addr), reload);
8389
}
8490
Self::Daemon { foreground } => cratesfyi::utils::start_daemon(!foreground),
8591
Self::Database { subcommand } => subcommand.handle_args(),

src/docbuilder/limits.rs

Lines changed: 2 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::error::Result;
22
use postgres::Connection;
3-
use std::collections::BTreeMap;
3+
use serde::Serialize;
44
use std::time::Duration;
55

6-
#[derive(Debug, Clone, PartialEq, Eq)]
6+
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
77
pub(crate) struct Limits {
88
memory: usize,
99
targets: usize,
@@ -67,52 +67,6 @@ impl Limits {
6767
pub(crate) fn targets(&self) -> usize {
6868
self.targets
6969
}
70-
71-
pub(crate) fn for_website(&self) -> BTreeMap<String, String> {
72-
let mut res = BTreeMap::new();
73-
res.insert("Available RAM".into(), SIZE_SCALE(self.memory));
74-
res.insert(
75-
"Maximum rustdoc execution time".into(),
76-
TIME_SCALE(self.timeout.as_secs() as usize),
77-
);
78-
res.insert(
79-
"Maximum size of a build log".into(),
80-
SIZE_SCALE(self.max_log_size),
81-
);
82-
if self.networking {
83-
res.insert("Network access".into(), "allowed".into());
84-
} else {
85-
res.insert("Network access".into(), "blocked".into());
86-
}
87-
res.insert(
88-
"Maximum number of build targets".into(),
89-
self.targets.to_string(),
90-
);
91-
res
92-
}
93-
}
94-
95-
const TIME_SCALE: fn(usize) -> String = |v| scale(v, 60, &["seconds", "minutes", "hours"]);
96-
const SIZE_SCALE: fn(usize) -> String = |v| scale(v, 1024, &["bytes", "KB", "MB", "GB"]);
97-
98-
fn scale(value: usize, interval: usize, labels: &[&str]) -> String {
99-
let (mut value, interval) = (value as f64, interval as f64);
100-
let mut chosen_label = &labels[0];
101-
for label in &labels[1..] {
102-
if value / interval >= 1.0 {
103-
chosen_label = label;
104-
value /= interval;
105-
} else {
106-
break;
107-
}
108-
}
109-
// 2.x
110-
let mut value = format!("{:.1}", value);
111-
// 2.0 -> 2
112-
if value.ends_with(".0") {
113-
value.truncate(value.len() - 2);
114-
}
115-
format!("{} {}", value, chosen_label)
11670
}
11771

11872
#[cfg(test)]
@@ -161,56 +115,4 @@ mod test {
161115
Ok(())
162116
});
163117
}
164-
165-
#[test]
166-
fn display_limits() {
167-
let limits = Limits {
168-
memory: 102_400,
169-
timeout: Duration::from_secs(300),
170-
targets: 1,
171-
..Limits::default()
172-
};
173-
let display = limits.for_website();
174-
assert_eq!(display.get("Network access"), Some(&"blocked".into()));
175-
assert_eq!(
176-
display.get("Maximum size of a build log"),
177-
Some(&"100 KB".into())
178-
);
179-
assert_eq!(
180-
display.get("Maximum number of build targets"),
181-
Some(&limits.targets.to_string())
182-
);
183-
assert_eq!(
184-
display.get("Maximum rustdoc execution time"),
185-
Some(&"5 minutes".into())
186-
);
187-
assert_eq!(display.get("Available RAM"), Some(&"100 KB".into()));
188-
}
189-
190-
#[test]
191-
fn scale_limits() {
192-
// time
193-
assert_eq!(TIME_SCALE(300), "5 minutes");
194-
assert_eq!(TIME_SCALE(1), "1 seconds");
195-
assert_eq!(TIME_SCALE(7200), "2 hours");
196-
197-
// size
198-
assert_eq!(SIZE_SCALE(1), "1 bytes");
199-
assert_eq!(SIZE_SCALE(100), "100 bytes");
200-
assert_eq!(SIZE_SCALE(1024), "1 KB");
201-
assert_eq!(SIZE_SCALE(10240), "10 KB");
202-
assert_eq!(SIZE_SCALE(1_048_576), "1 MB");
203-
assert_eq!(SIZE_SCALE(10_485_760), "10 MB");
204-
assert_eq!(SIZE_SCALE(1_073_741_824), "1 GB");
205-
assert_eq!(SIZE_SCALE(10_737_418_240), "10 GB");
206-
assert_eq!(SIZE_SCALE(std::u32::MAX as usize), "4 GB");
207-
208-
// fractional sizes
209-
assert_eq!(TIME_SCALE(90), "1.5 minutes");
210-
assert_eq!(TIME_SCALE(5400), "1.5 hours");
211-
212-
assert_eq!(SIZE_SCALE(1_288_490_189), "1.2 GB");
213-
assert_eq!(SIZE_SCALE(3_758_096_384), "3.5 GB");
214-
assert_eq!(SIZE_SCALE(1_048_051_712), "999.5 MB");
215-
}
216118
}

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ mod web;
2020
use web::page::GlobalAlert;
2121

2222
// Warning message shown in the navigation bar of every page. Set to `None` to hide it.
23-
pub(crate) static GLOBAL_ALERT: Option<GlobalAlert> = None;
24-
/*
23+
// pub(crate) static GLOBAL_ALERT: Option<GlobalAlert> = None;
24+
2525
pub(crate) static GLOBAL_ALERT: Option<GlobalAlert> = Some(GlobalAlert {
2626
url: "https://blog.rust-lang.org/2019/09/18/upcoming-docsrs-changes.html",
2727
text: "Upcoming docs.rs breaking changes!",
2828
css_class: "error",
2929
fa_icon: "warning",
3030
});
31-
*/
3231

3332
/// Version string generated at build time contains last git
3433
/// commit hash and build date

src/utils/daemon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ pub fn start_daemon(background: bool) {
260260
// at least start web server
261261
info!("Starting web server");
262262

263-
crate::Server::start(None);
263+
crate::Server::start(None, false);
264264
}
265265

266266
fn opts() -> DocBuilderOptions {

0 commit comments

Comments
 (0)