gotham_restful_redoc/
lib.rs1#![forbid(elided_lifetimes_in_paths, unsafe_code)]
2
3use base64::prelude::*;
6use either::Either;
7use sha2::{Digest, Sha256};
8use std::{io::Write, iter};
9
10#[doc(hidden)]
11pub struct Redoc {
12 pub html: Vec<u8>,
14
15 pub script_hash: String
17}
18
19#[doc(hidden)]
20pub fn html(spec: String) -> Redoc {
21 let encoded_spec = spec
22 .chars()
23 .flat_map(|c| match c {
24 '&' => Either::Left("&".chars()),
25 '<' => Either::Left("<".chars()),
26 '>' => Either::Left(">".chars()),
27 c => Either::Right(iter::once(c))
28 })
29 .collect::<String>();
30
31 let script = include_str!("script.min.js");
32 let mut script_hash = Sha256::new();
33 script_hash.update(script);
34 let script_hash = BASE64_STANDARD.encode(script_hash.finalize());
35
36 let mut html = Vec::<u8>::new();
37 write!(
38 html,
39 concat!(
40 "<!DOCTYPE HTML>",
41 "<html>",
42 "<head>",
43 r#"<meta charset="utf-8"/>"#,
44 r#"<meta name="viewport" content="width=device-width,initial-scale=1"/>"#,
45 "</head>",
46 r#"<body style="margin:0">"#,
47 r#"<div id="spec" style="display:none">{}</div>"#,
48 r#"<div id="redoc"></div>"#,
49 r#"<script>{}</script>"#,
50 "</body>",
51 "</html>"
52 ),
53 encoded_spec, script
54 )
55 .unwrap();
56
57 Redoc { html, script_hash }
58}