credence_lib/configuration/
render.rs

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