apistos_rapidoc/
lib.rs

1//! This crate allow to expose the generated openapi specification through [RapiDoc](https://rapidocweb.com/).
2
3use apistos_plugins::ui::{UIPlugin, UIPluginConfig};
4
5const RAPIDOC_DEFAULT: &str = include_str!("../assets/index.html");
6
7/// Config for exposing the openapi specification through `RapiDoc`
8pub struct RapidocConfig {
9  html: String,
10  path: String,
11}
12
13impl RapidocConfig {
14  /// Create a new [`RapidocConfig`] with the `path` on which to expose the rapidoc ui.
15  pub fn new<T: ToString>(path: &T) -> Self {
16    Self {
17      html: RAPIDOC_DEFAULT.to_string(),
18      path: path.to_string(),
19    }
20  }
21
22  /// Allow to override the default rapidoc html file path. The new file should contain the
23  /// `$specUrl` variable which refers the openapi specification url
24  pub fn with_html(mut self, html_path: String) -> Self {
25    self.html = html_path;
26    self
27  }
28}
29
30impl UIPluginConfig for RapidocConfig {
31  fn build(self: Box<Self>, openapi_path: &str) -> Box<dyn UIPlugin> {
32    Box::new(Rapidoc::new(*self, openapi_path))
33  }
34}
35
36#[doc(hidden)]
37pub struct Rapidoc {
38  html: String,
39  path: String,
40  openapi_path: String,
41}
42
43impl Rapidoc {
44  pub fn new(config: RapidocConfig, openapi_path: &str) -> Self {
45    Self {
46      html: config.html,
47      path: config.path,
48      openapi_path: openapi_path.to_string(),
49    }
50  }
51}
52
53impl UIPlugin for Rapidoc {
54  fn path(&self) -> String {
55    self.path.clone()
56  }
57
58  fn to_html(&self) -> String {
59    self.html.replace("$specUrl", &self.openapi_path)
60  }
61}