-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.rs
150 lines (143 loc) · 4.74 KB
/
utils.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
use crate::spec::ItemsSpec;
use anyhow::Result;
use minify_js::{minify, TopLevelMode};
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use tera::{Context, Tera};
pub(crate) fn render_static_files<P: AsRef<Path>>(path: P) -> Result<()> {
let path = Path::new(path.as_ref()).join("static");
fs::create_dir(&path)?;
let files = vec![
(
"bootstrap.bundle.min.js",
include_str!("../../../static/bootstrap.bundle.min.js"),
),
(
"bootstrap-table.min.js",
include_str!("../../../static/bootstrap-table.min.js"),
),
(
"bootstrap-table-fixed-columns.min.js",
include_str!("../../../static/bootstrap-table-fixed-columns.min.js"),
),
(
"jquery.min.js",
include_str!("../../../static/jquery.min.js"),
),
("jsonm.min.js", include_str!("../../../static/jsonm.min.js")),
(
"lz-string.min.js",
include_str!("../../../static/lz-string.min.js"),
),
("vega.min.js", include_str!("../../../static/vega.min.js")),
(
"vega-lite.min.js",
include_str!("../../../static/vega-lite.min.js"),
),
(
"vega-embed.min.js",
include_str!("../../../static/vega-embed.min.js"),
),
(
"bootstrap.min.css",
include_str!("../../../static/bootstrap.min.css"),
),
(
"bootstrap-table.min.css",
include_str!("../../../static/bootstrap-table.min.css"),
),
(
"bootstrap-table-filter-control.min.js",
include_str!("../../../static/bootstrap-table-filter-control.min.js"),
),
(
"bootstrap-table-fixed-columns.min.css",
include_str!("../../../static/bootstrap-table-fixed-columns.min.css"),
),
("datavzrd.css", include_str!("../../../static/datavzrd.css")),
(
"showdown.min.js",
include_str!("../../../static/showdown.min.js"),
),
(
"bootstrap-select.min.js",
include_str!("../../../static/bootstrap-select.min.js"),
),
(
"bootstrap-select.min.css",
include_str!("../../../static/bootstrap-select.min.css"),
),
];
for (name, file) in files {
let mut out = File::create(path.join(Path::new(name)))?;
out.write_all(file.as_bytes())?;
}
Ok(())
}
pub(crate) fn minify_js(file: &str) -> Result<Vec<u8>> {
let mut minified: Vec<u8> = Vec::new();
minify(
TopLevelMode::Global,
file.as_bytes().to_vec(),
&mut minified,
)
.expect("Failed minifying js");
Ok(minified)
}
pub(crate) fn render_index_file<P: AsRef<Path>>(path: P, specs: &ItemsSpec) -> Result<()> {
let table = if let Some(default_view) = &specs.default_view {
default_view
} else {
specs.views.keys().next().unwrap()
};
let mut templates = Tera::default();
templates.add_raw_template(
"index.html.tera",
include_str!("../../../templates/index.html.tera"),
)?;
let mut context = Context::new();
context.insert("table", table);
let file_path = Path::new(path.as_ref()).join(Path::new("index").with_extension("html"));
let html = templates.render("index.html.tera", &context)?;
let mut file = fs::File::create(file_path)?;
file.write_all(html.as_bytes())?;
Ok(())
}
#[cfg(test)]
mod tests {
use crate::{render_index_file, render_static_files, ItemsSpec};
use dir_assert::assert_paths;
use std::fs;
use std::path::Path;
#[test]
fn test_render_index_file() {
let spec = ItemsSpec {
report_name: "".to_string(),
datasets: Default::default(),
default_view: Some("my-view".to_string()),
max_in_memory_rows: 1000,
views: Default::default(),
aux_libraries: None,
};
render_index_file(Path::new("/tmp"), &spec).unwrap();
let rendered_file_content = fs::read_to_string("/tmp/index.html")
.expect("Could not read rendered test index file.");
fs::remove_file("/tmp/index.html")
.expect("Could not read remove rendered test index file.");
assert_eq!(
rendered_file_content,
include_str!("../../../tests/expected/index.html")
)
}
#[test]
fn test_render_static_files() {
render_static_files(Path::new("/tmp")).unwrap();
assert_paths!("/tmp/static", "static");
for entry in fs::read_dir("/tmp/static").unwrap() {
fs::remove_file(entry.unwrap().path()).unwrap();
}
fs::remove_dir("/tmp/static").unwrap();
}
}