Struct tinted_builder_rust::Template

source ·
pub struct Template { /* private fields */ }
Expand description

Add tinted-builder library test since tinted-builder-rust is exporting the structs

use tinted_builder_rust::{Scheme, Template};

let template = String::from(r#"/* Some CSS file with {{scheme-name}} theme */
.someCssSelector { background-color: #{{base00-hex}} }
.someOtherCssSelector { background-color: #{{base0F-hex}} }"#);
let scheme_str = r#"system: "base16"
name: "UwUnicorn"
author: "Fernando Marques (https://github.com/RakkiUwU) and Gabriel Fontes (https://github.com/Misterio77)"
variant: "dark"
palette:
  base00: "241b26"
  base01: "2f2a3f"
  base02: "46354a"
  base03: "6c3cb2"
  base04: "7e5f83"
  base05: "eed5d9"
  base06: "d9c2c6"
  base07: "e4ccd0"
  base08: "877bb6"
  base09: "de5b44"
  base0A: "a84a73"
  base0B: "c965bf"
  base0C: "9c5fce"
  base0D: "6a9eb5"
  base0E: "78a38f"
  base0F: "a3a079""#;
let scheme = Scheme::Base16(serde_yaml::from_str(&scheme_str).unwrap());
let template = Template::new(template, scheme);
let output = template
    .render()
    .unwrap();

 assert_eq!(output, r#"/* Some CSS file with UwUnicorn theme */
.someCssSelector { background-color: #241b26 }
.someOtherCssSelector { background-color: #a3a079 }"#);

A struct representing a template that can be rendered with the provided color scheme.

The Template struct holds the content of the template and the scheme used to render it. It provides methods to create a new template and render it into a String using the specified color scheme.

Implementations§

source§

impl Template

source

pub fn new(content: String, scheme: Scheme) -> Template

Creates a new Template instance.

§Arguments
  • content - A String representing the content of the template.
  • scheme - A Scheme enum that determines which color scheme to use when rendering the template.
§Returns

A new Template instance with the provided content and scheme.

source

pub fn render(&self) -> Result<String, TintedBuilderError>

Renders the template into a String using the provided color scheme.

This method applies the specified Scheme to the template content, converting placeholders in the content to their corresponding values from the scheme context.

§Errors

Returns a TintedBuilderError if the rendering process fails. This could happen if the content contains placeholders that cannot be resolved using the scheme context.

§Examples
use tinted_builder::{Template, Scheme};

let scheme_yaml = r#"
system: "base16"
name: "Some name"
author: "Some author"
variant: "dark"
palette:
  base00: "241b26"
  base01: "2f2a3f"
  base02: "46354a"
  base03: "6c3cb2"
  base04: "7e5f83"
  base05: "eed5d9"
  base06: "d9c2c6"
  base07: "e4ccd0"
  base08: "877bb6"
  base09: "de5b44"
  base0A: "a84a73"
  base0B: "c965bf"
  base0C: "9c5fce"
  base0D: "6a9eb5"
  base0E: "78a38f"
  base0F: "a3a079"
"#;
let template = Template::new(
    r#"{{scheme-system}} scheme name is "{{scheme-name}}" and first color is #{{base00-hex}}"#.to_string(),
    Scheme::Base16(serde_yaml::from_str(scheme_yaml).unwrap())
);
let rendered = template.render().unwrap();

assert_eq!(
    rendered,
    r#"base16 scheme name is "Some name" and first color is #241b26"#
);

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.