1use anyhow::Result;
2use serde::Serialize;
3use tera::{Context, Tera};
4
5pub mod configuration;
6#[cfg(feature = "actix")]
7pub mod scalar_actix;
8
9static TEMPLATE: &str = include_str!("templates/template.html");
10
11#[derive(Clone)]
12pub enum Theme {
13 Alternate,
14 Default,
15 Moon,
16 Purple,
17 Solarized,
18 BluePlanet,
19 Saturn,
20 Kepler,
21 Mars,
22 DeepSpace,
23 Elysiajs,
24 Fastify,
25 None,
26}
27
28impl Serialize for Theme {
29 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
30 where
31 S: serde::ser::Serializer,
32 {
33 serializer.serialize_str(self.as_str())
34 }
35}
36
37impl Theme {
38 fn as_str(&self) -> &str {
39 match self {
40 Theme::Alternate => "alternate",
41 Theme::Default => "default",
42 Theme::Moon => "moon",
43 Theme::Purple => "purple",
44 Theme::Solarized => "solarized",
45 Theme::BluePlanet => "blue-planet",
46 Theme::Saturn => "saturn",
47 Theme::Kepler => "kepler",
48 Theme::Mars => "mars",
49 Theme::DeepSpace => "deep-space",
50 Theme::Elysiajs => "elysiajs",
51 Theme::Fastify => "fastify",
52 Theme::None => "none",
53 }
54 }
55}
56
57pub struct Documentation {
58 title: String,
59 content: String,
60 configuration: configuration::Configuration,
61}
62
63impl Documentation {
64 pub fn new(title: &str, content: &str) -> Self {
65 Self {
66 title: title.to_string(),
67 content: content.to_string(),
68 configuration: configuration::Configuration::default(),
69 }
70 }
71
72 pub fn theme(&mut self, theme: Theme) -> &mut Self {
73 self.configuration.theme = Some(theme);
74 self
75 }
76
77 pub fn build(&self) -> Result<String, anyhow::Error> {
78 let configuration = serde_json::to_string(&self.configuration)?;
79 templatize(self.content.clone(), self.title.clone(), configuration)
80 }
81}
82
83pub fn templatize(
84 content: String,
85 title: String,
86 configuration: String,
87) -> Result<String, anyhow::Error> {
88 let mut tera = Tera::default();
89 tera.add_raw_template("template.html", TEMPLATE)?;
90
91 let mut context = Context::new();
92 context.insert("documentation", &content);
93 context.insert("title", &title);
94 context.insert("configuration", &configuration);
95
96 let parsed_doc = tera.render("template.html", &context)?;
97
98 Ok(parsed_doc)
99}