tinted_builder/
template.rs

1mod base16;
2
3use crate::{error::TintedBuilderError, scheme::Scheme};
4
5/// A struct representing a template that can be rendered with the provided color scheme.
6///
7/// The `Template` struct holds the content of the template and the scheme used to render it. It
8/// provides methods to create a new template and render it into a `String` using the specified
9/// color scheme.
10pub struct Template {
11    content: String,
12    scheme: Scheme,
13}
14
15impl Template {
16    /// Creates a new `Template` instance.
17    ///
18    /// # Arguments
19    ///
20    /// * `content` - A `String` representing the content of the template.
21    /// * `scheme` - A `Scheme` enum that determines which color scheme to use when rendering the template.
22    ///
23    /// # Returns
24    ///
25    /// A new `Template` instance with the provided content and scheme.
26    #[must_use]
27    pub const fn new(content: String, scheme: Scheme) -> Self {
28        Self { content, scheme }
29    }
30
31    /// Renders the template into a `String` using the provided color scheme.
32    ///
33    /// This method applies the specified `Scheme` to the template content, converting placeholders
34    /// in the content to their corresponding values from the scheme context.
35    ///
36    /// # Errors
37    ///
38    /// Returns a `TintedBuilderError` if the rendering process fails. This could happen if the
39    /// content contains placeholders that cannot be resolved using the scheme context.
40    ///
41    /// # Examples
42    ///
43    /// ```
44    /// use tinted_builder::{Template, Scheme};
45    ///
46    /// let scheme_yaml = r#"
47    /// system: "base16"
48    /// name: "Some name"
49    /// author: "Some author"
50    /// variant: "dark"
51    /// palette:
52    ///   base00: "241b26"
53    ///   base01: "2f2a3f"
54    ///   base02: "46354a"
55    ///   base03: "6c3cb2"
56    ///   base04: "7e5f83"
57    ///   base05: "eed5d9"
58    ///   base06: "d9c2c6"
59    ///   base07: "e4ccd0"
60    ///   base08: "877bb6"
61    ///   base09: "de5b44"
62    ///   base0A: "a84a73"
63    ///   base0B: "c965bf"
64    ///   base0C: "9c5fce"
65    ///   base0D: "6a9eb5"
66    ///   base0E: "78a38f"
67    ///   base0F: "a3a079"
68    /// "#;
69    /// let template = Template::new(
70    ///     r#"{{scheme-system}} scheme name is "{{scheme-name}}" and first color is #{{base00-hex}}"#.to_string(),
71    ///     Scheme::Base16(serde_yaml::from_str(scheme_yaml).unwrap())
72    /// );
73    /// let rendered = template.render().unwrap();
74    ///
75    /// assert_eq!(
76    ///     rendered,
77    ///     r#"base16 scheme name is "Some name" and first color is #241b26"#
78    /// );
79    /// ```
80    pub fn render(&self) -> Result<String, TintedBuilderError> {
81        match self.scheme {
82            Scheme::Base16(ref scheme) | Scheme::Base24(ref scheme) => {
83                let ctx = base16::to_template_context(&scheme.clone());
84                let rendered = base16::render(&self.content, &ctx)?;
85
86                Ok(rendered)
87            }
88        }
89    }
90}