use crate::config::{AppConfig, ViewConfig};
use crate::error::Result;
use serde_json::Value;
use std::sync::Arc;
pub mod api;
pub mod embed_provider;
pub mod minifier;
pub mod totaljs;
#[derive(Debug, Clone, Default)]
pub struct RequestMeta {
pub url: String,
pub hostname: String,
pub mobile: bool,
}
pub fn is_mobile(user_agent: &str) -> bool {
let ua = user_agent.to_ascii_lowercase();
[
"mobi",
"android",
"iphone",
"ipod",
"windows phone",
"blackberry",
"opera mini",
"iemobile",
]
.iter()
.any(|token| ua.contains(token))
}
pub trait ViewEngineImpl: Send + Sync {
fn render(&self, template: &str, data: &Value, layout: Option<&str>) -> Result<String>;
fn render_rich(
&self,
template: &str,
data: &Value,
layout: Option<&str>,
repository: Option<&Value>,
session: Option<&Value>,
request: Option<&RequestMeta>,
) -> Result<String> {
let _ = request;
let mut packed = data.clone();
if let Value::Object(ref mut map) = packed {
if let Some(repo) = repository {
map.insert("_context_repository".to_string(), repo.clone());
}
if let Some(sess) = session {
map.insert("_context_session".to_string(), sess.clone());
}
}
self.render(template, &packed, layout)
}
fn set_directory(&mut self, dir: &str);
}
pub struct ViewEngine {
engine: Box<dyn ViewEngineImpl>,
}
pub use totaljs::TotalJsEngine;
pub use api::VIEW;
pub use embed_provider::{
get_assets_provider, get_views_provider, register_assets_provider, register_views_provider,
EmbeddedFileProvider,
};
impl Default for ViewEngine {
fn default() -> Self {
Self::new()
}
}
impl ViewEngine {
pub fn new() -> Self {
Self::from_directory("views")
}
pub fn from_directory(directory: &str) -> Self {
Self::totaljs_filesystem(directory)
}
pub fn builder() -> ViewEngineBuilder {
ViewEngineBuilder::new()
}
pub fn totaljs_filesystem(base_dir: &str) -> Self {
Self::totaljs_filesystem_with_config(base_dir, None)
}
pub fn totaljs_filesystem_with_config(
base_dir: &str,
view_config: Option<&ViewConfig>,
) -> Self {
Self {
engine: Box::new(totaljs::TotalJsEngine::with_config(base_dir, view_config)),
}
}
pub fn totaljs_filesystem_with_app_config(base_dir: &str, app_config: Arc<AppConfig>) -> Self {
Self {
engine: Box::new(totaljs::TotalJsEngine::with_app_config(
base_dir, app_config,
)),
}
}
#[cfg(feature = "embedded-views")]
pub fn totaljs_embedded() -> Self {
Self {
engine: Box::new(totaljs::EmbeddedTotalJsEngine::new()),
}
}
#[cfg(feature = "embedded-views")]
pub fn totaljs_embedded_with_config(view_config: Option<&ViewConfig>) -> Self {
Self {
engine: Box::new(totaljs::EmbeddedTotalJsEngine::with_config(view_config)),
}
}
#[cfg(feature = "embedded-views")]
pub fn totaljs_embedded_with_app_config(app_config: Arc<AppConfig>) -> Self {
Self {
engine: Box::new(totaljs::EmbeddedTotalJsEngine::with_app_config(app_config)),
}
}
#[cfg(not(feature = "embedded-views"))]
pub fn totaljs_embedded() -> Self {
panic!("Embedded views not available! Enable 'embedded-views' feature.")
}
#[cfg(not(feature = "embedded-views"))]
pub fn totaljs_embedded_with_config(_view_config: Option<&ViewConfig>) -> Self {
panic!("Embedded views not available! Enable 'embedded-views' feature.")
}
#[deprecated(note = "Use totaljs_embedded() instead")]
pub fn embedded() -> Self {
Self::totaljs_embedded()
}
pub fn set_directory(&mut self, dir: &str) {
self.engine.set_directory(dir);
}
pub fn render(&self, template: &str, data: &Value, layout: Option<&str>) -> Result<String> {
self.engine.render(template, data, layout)
}
pub fn render_rich(
&self,
template: &str,
data: &Value,
layout: Option<&str>,
repository: Option<&Value>,
session: Option<&Value>,
request: Option<&RequestMeta>,
) -> Result<String> {
self.engine
.render_rich(template, data, layout, repository, session, request)
}
}
pub struct ViewEngineBuilder {
views_path: String,
extension: String,
}
impl Default for ViewEngineBuilder {
fn default() -> Self {
Self::new()
}
}
impl ViewEngineBuilder {
pub fn new() -> Self {
Self {
views_path: "views".to_string(),
extension: "html".to_string(),
}
}
pub fn views_path(mut self, path: &str) -> Self {
self.views_path = path.to_string();
self
}
pub fn extension(mut self, ext: &str) -> Self {
self.extension = ext.to_string();
self
}
pub fn build(self) -> ViewEngine {
ViewEngine::totaljs_filesystem(&self.views_path)
}
}