From 442b23fb8ecf30af5310c03969db41c97e156c5c Mon Sep 17 00:00:00 2001 From: Karthik Ganeshram Date: Wed, 14 Sep 2022 13:21:26 -0700 Subject: [PATCH] Seperate server functionality into server feature Signed-off-by: Karthik Ganeshram --- Cargo.toml | 5 +++-- src/template.rs | 19 +++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1e7f17c..6e393a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ bytes = "1.1.0" chrono = { version = "0.4.19", features = ["serde"] } flate2 = "1.0.23" handlebars = { version = "4.2.2", features = ["dir_source", "script_helper"] } -handlebars_sprig = { git = "https://github.com/rajatjindal/handlebars-sprig", rev = "f2d42142121d4f04b35ce00fdd26ba48b0993fe0" } +handlebars_sprig = { git = "https://github.com/rajatjindal/handlebars-sprig", rev = "f2d42142121d4f04b35ce00fdd26ba48b0993fe0", optional = true } http = "0.2.6" pulldown-cmark = { version = "0.9.1", default-features = false } serde = { version = "1.0.136", features = ["derive"] } @@ -28,5 +28,6 @@ members = ["bart"] [features] -default = ["spin"] +default = ["server"] spin = ["dep:spin-sdk"] +server = ["spin", "dep:handlebars_sprig"] diff --git a/src/template.rs b/src/template.rs index e494b9b..be0ad42 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,18 +1,19 @@ -use std::collections::BTreeMap; -use std::path::PathBuf; +#[cfg(feature = "server")] +use { + handlebars::Handlebars, handlebars_sprig, http::HeaderMap, std::collections::BTreeMap, + std::path::PathBuf, +}; use super::content::{Content, Head}; -use handlebars::Handlebars; -use http::HeaderMap; use serde::{Deserialize, Serialize}; -use handlebars_sprig; - /// The name of the default template. /// This will be resolved to $TEMPLATE_DIR/$DEFAULT_TEMPLATE.hbs +#[cfg(feature = "server")] const DEFAULT_TEMPLATE: &str = "main"; /// Describe the site itself +#[cfg(feature = "server")] #[derive(Serialize, Deserialize)] pub struct SiteInfo { pub title: String, @@ -25,6 +26,7 @@ pub struct SiteInfo { } /// Context for a template render. +#[cfg(feature = "server")] #[derive(Serialize)] pub struct TemplateContext { request: BTreeMap, @@ -46,6 +48,7 @@ pub struct TemplateContext { // pub struct RequestValues {} /// Information about the site, including site info and all of the pages. +#[cfg(feature = "server")] #[derive(Serialize)] pub struct SiteValues { info: SiteInfo, @@ -72,6 +75,7 @@ impl From for PageValues { } /// Renderer can execute a handlebars template and render the results into HTML. +#[cfg(feature = "server")] pub struct Renderer<'a> { pub template_dir: PathBuf, pub theme_dir: Option, @@ -82,6 +86,7 @@ pub struct Renderer<'a> { handlebars: handlebars::Handlebars<'a>, } +#[cfg(feature = "server")] impl<'a> Renderer<'a> { /// Create a new renderer with the necessary directories attached. pub fn new( @@ -108,6 +113,7 @@ impl<'a> Renderer<'a> { /// Load the template directory. pub fn load_template_dir(&mut self) -> Result<(), anyhow::Error> { + #[cfg(feature = "server")] self.register_helpers(); // If there is a theme, load the templates provided by it first @@ -216,6 +222,7 @@ impl<'a> Renderer<'a> { } /// Add all of the helper functions to this renderer. + #[cfg(feature = "server")] fn register_helpers(&mut self) { handlebars_sprig::addhelpers(&mut self.handlebars) }