credence_lib/configuration/
render.rs

1use super::{
2    super::{render::*, resolve::*, util::*},
3    annotations::*,
4    constants::*,
5};
6
7use {
8    bytestring::*,
9    compris::{normal::*, resolve::*, *},
10    kutil_cli::debug::*,
11    kutil_std::{collections::*, metric::*},
12};
13
14//
15// RenderConfiguration
16//
17
18/// Render configuration.
19#[derive(Clone, Debug, Debuggable, Resolve)]
20pub struct RenderConfiguration {
21    /// Global variables.
22    #[resolve]
23    #[debuggable(iter(kv), as(debuggable), key_style(string))]
24    pub variables: FastHashMap<ByteString, Value>,
25
26    /// Rendered page URI midfix.
27    #[resolve(key = "midfix")]
28    #[debuggable(style(string))]
29    pub midfix: ByteString,
30
31    /// Annotations.
32    #[resolve]
33    #[debuggable(as(debuggable))]
34    pub annotations: AnnotationsConfiguration,
35
36    /// Default renderer.
37    #[resolve(key = "default-renderer")]
38    #[debuggable(as(display), style(symbol))]
39    pub default_renderer: Renderer,
40
41    /// Default template.
42    #[resolve(key = "default-template")]
43    #[debuggable(style(string))]
44    pub default_template: ByteString,
45
46    /// Maximum content size.
47    #[resolve(key = "max-content-size")]
48    #[debuggable(as(display), style(symbol))]
49    pub max_content_size: ResolveByteCount,
50}
51
52impl RenderConfiguration {
53    /// Whether the URI points to a rendered page, and if so returns its type.
54    pub fn is_rendered_page(&self, uri_path: &str) -> Option<RenderedPageType> {
55        if let Some(last_dot) = uri_path.rfind(EXTENSION_SEPARATOR) {
56            let uri_path_without_extension = &uri_path[..last_dot];
57            let rendered_page_midfix: &str = &self.midfix;
58            if uri_path_without_extension.ends_with(rendered_page_midfix) {
59                let extension = &uri_path[last_dot + 1..];
60
61                let format_result: Result<Format, _> = extension.parse();
62                return Some(match format_result {
63                    Ok(format) => RenderedPageType::Annotations(format),
64                    Err(_) => RenderedPageType::ContentWithEmbeddedAnnotations,
65                });
66            }
67        }
68
69        None
70    }
71}
72
73impl Default for RenderConfiguration {
74    fn default() -> Self {
75        Self {
76            variables: FastHashMap::default(),
77            annotations: AnnotationsConfiguration::default(),
78            midfix: ".r".into(),
79            default_renderer: Renderer::default(),
80            default_template: DEFAULT_TEMPLATE.into(),
81            max_content_size: ByteCount::from_mebibytes(10).into(),
82        }
83    }
84}