Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Valuable adoption #452

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -29,6 +29,7 @@ serde = "1.0.0"
serde_json = "1.0.39"
walkdir = { version = "2.2.3", optional = true }
rhai = { version = "0.20", optional = true, features = ["sync", "serde"] }
valuable = { git = "https://github.com/tokio-rs/valuable.git" }

[dev-dependencies]
env_logger = "0.8"
Expand Down
21 changes: 7 additions & 14 deletions src/context.rs
@@ -1,7 +1,7 @@
use std::collections::{HashMap, VecDeque};

use serde::Serialize;
use serde_json::value::{to_value, Map, Value as Json};
use valuable::Valuable;

use crate::block::{BlockContext, BlockParamHolder};
use crate::error::RenderError;
Expand All @@ -15,8 +15,8 @@ pub type Object = HashMap<String, Json>;
/// The context wrap data you render on your templates.
///
#[derive(Debug, Clone)]
pub struct Context {
data: Json,
pub struct Context<'rc, V: Valuable> {
data: &'rc V,
}

#[derive(Debug)]
Expand Down Expand Up @@ -151,21 +151,14 @@ pub(crate) fn merge_json(base: &Json, addition: &HashMap<&str, &Json>) -> Json {
Json::Object(base_map)
}

impl Context {
/// Create a context with null data
pub fn null() -> Context {
Context { data: Json::Null }
}

impl<'rc, V: Valuable> Context<'rc, V> {
/// Create a context with given data
pub fn wraps<T: Serialize>(e: T) -> Result<Context, RenderError> {
to_value(e)
.map_err(RenderError::from)
.map(|d| Context { data: d })
pub fn wraps(e: &'rc V) -> Result<Context<'rc, V>, RenderError> {
Ok(Context { data: e })
}

/// Navigate the context with relative path and block scopes
pub(crate) fn navigate<'reg, 'rc>(
pub(crate) fn navigate<'reg>(
&'rc self,
relative_path: &[PathSeg],
block_contexts: &VecDeque<BlockContext<'reg>>,
Expand Down
5 changes: 4 additions & 1 deletion src/json/value.rs
@@ -1,3 +1,5 @@
use valuable::Value;

use serde::Serialize;
use serde_json::value::{to_value, Value as Json};

Expand All @@ -8,13 +10,14 @@ pub(crate) static DEFAULT_VALUE: Json = Json::Null;
/// * Constant: the JSON value hardcoded into template
/// * Context: the JSON value referenced in your provided data context
/// * Derived: the owned JSON value computed during rendering process
/// * Missing: the value was not found
///
#[derive(Debug)]
pub enum ScopedJson<'reg: 'rc, 'rc> {
Constant(&'reg Json),
Derived(Json),
// represents a json reference to context value, its full path
Context(&'rc Json, Vec<String>),
Context(Value<'rc>, Vec<String>),
Missing,
}

Expand Down
12 changes: 6 additions & 6 deletions src/registry.rs
Expand Up @@ -5,7 +5,7 @@ use std::io::{Error as IoError, Write};
use std::path::Path;
use std::sync::Arc;

use serde::Serialize;
use valuable::Valuable;

use crate::context::Context;
use crate::decorators::{self, DecoratorDef};
Expand Down Expand Up @@ -496,12 +496,12 @@ impl<'reg> Registry<'reg> {
/// Render a registered template with some data into a string
///
/// * `name` is the template name you registered previously
/// * `data` is the data that implements `serde::Serialize`
/// * `data` is the data that implements `valuable::Valuable`
///
/// Returns rendered string or a struct with error information
pub fn render<T>(&self, name: &str, data: &T) -> Result<String, RenderError>
where
T: Serialize,
T: Valuable,
{
let mut output = StringOutput::new();
let ctx = Context::wraps(&data)?;
Expand All @@ -519,7 +519,7 @@ impl<'reg> Registry<'reg> {
/// Render a registered template and write some data to the `std::io::Write`
pub fn render_to_write<T, W>(&self, name: &str, data: &T, writer: W) -> Result<(), RenderError>
where
T: Serialize,
T: Valuable,
W: Write,
{
let mut output = WriteOutput::new(writer);
Expand All @@ -530,7 +530,7 @@ impl<'reg> Registry<'reg> {
/// Render a template string using current registry without registering it
pub fn render_template<T>(&self, template_string: &str, data: &T) -> Result<String, RenderError>
where
T: Serialize,
T: Valuable,
{
let mut writer = StringWriter::new();
self.render_template_to_write(template_string, data, &mut writer)?;
Expand Down Expand Up @@ -562,7 +562,7 @@ impl<'reg> Registry<'reg> {
writer: W,
) -> Result<(), RenderError>
where
T: Serialize,
T: Valuable,
W: Write,
{
let tpl = Template::compile(template_string)?;
Expand Down