summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2017-05-24 22:55:17 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2017-05-24 22:55:46 -0400
commit813996dd52a40124b453dfb35617036d1e88357b (patch)
tree8c6539dfffc7f892a25ddca0db334a01e4c6b06b /src
parent02e44e2a297b806b090d073a7696cb9587136b22 (diff)
render: Organize rendering functions in a module
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs4
-rw-r--r--src/render/mod.rs109
2 files changed, 113 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index fa22112..a5aebdf 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,8 @@
+#[macro_use]
+extern crate horrorshow;
+
pub mod systemd;
+pub mod render;
#[cfg(test)]
mod tests {
diff --git a/src/render/mod.rs b/src/render/mod.rs
new file mode 100644
index 0000000..d391b6e
--- /dev/null
+++ b/src/render/mod.rs
@@ -0,0 +1,109 @@
+use systemd::unit;
+
+use horrorshow::prelude::*;
+use horrorshow::Raw;
+
+fn render_in_page<'a>(content: Box<RenderBox + 'a>) -> String {
+ (html!{
+ : Raw("<!DOCTYPE html>");
+ html {
+ head {
+ title: "Systemhttpd";
+ link(rel="stylesheet", type="text/css", href="static/main.css");
+ }
+ body {
+ : content
+ }
+ }
+ }).into_string().unwrap()
+}
+
+fn unit_table<'a>(units: &'a [&unit::Unit]) -> Box<RenderBox + 'a> {
+ fn render_unit<'a>(unit: &'a unit::Unit) -> Box<RenderBox + 'a> {
+ box_html! {
+ tr {
+ td {
+ : &unit.name
+ }
+ td {
+ : format_args!("{} ({})",
+ &unit.active_state,
+ &unit.sub_state)
+ }
+ }
+ }
+ }
+
+ box_html! {
+ table {
+ tr {
+ th {
+ : "Unit"
+ }
+ th {
+ : "Active"
+ }
+ }
+ @ for unit in units {
+ : render_unit(unit)
+ }
+ }
+ }
+}
+
+pub fn system_status(sections: &[(String, Vec<&unit::Unit>)]) -> String {
+ let b =
+ box_html! {
+ @ for &(ref type_, ref units) in sections {
+ h1 {
+ : type_
+ }
+ : unit_table(&units)
+ }
+ };
+ render_in_page(b)
+}
+
+pub fn render_message(message: &str, units: &[unit::Unit]) -> String {
+ (html!{
+ : Raw("<!DOCTYPE html>");
+ html {
+ head {
+ title: "Title";
+ link(rel="stylesheet", type="text/css", href="static/main.css");
+ }
+ body {
+ p {
+ : message
+ }
+ h1 {
+ : "Units"
+ }
+ table {
+ tr {
+ th {
+ : "Unit"
+ }
+ th {
+ : "Active"
+ }
+ }
+ @ for unit in units {
+ tr {
+ td {
+ : &unit.name
+ }
+ td {
+ : format_args!("{} ({})",
+ &unit.active_state,
+ &unit.sub_state)
+ }
+ }
+ }
+ }
+ }
+ }
+ })
+ .into_string()
+ .unwrap()
+}