Skip to content

Commit

Permalink
update doc and use crates for gtmpl_value/derive
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Nov 15, 2017
1 parent 23dce74 commit b091121
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 22 deletions.
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gtmpl"
version = "0.1.3"
version = "0.2.0"
authors = ["Florian Merz <flomerz@gmail.com>"]
description = "The Golang Templating Language for Rust"
license = "MIT"
Expand All @@ -18,7 +18,5 @@ travis-ci = { repository = "fiji-flo/gtmpl-rust" }
itertools = "0.6"
lazy_static = "0.2"
percent-encoding = "1.0.0"
gtmpl_value = { git = "https://github.com/fiji-flo/gtmpl_value.git" }
gtmpl_derive = { git = "https://github.com/fiji-flo/gtmpl_derive.git" }
#gtmpl_value = { path = "../gtmpl_value" }
#gtmpl_derive = { path = "../gtmpl_derive" }
gtmpl_value = "0.1.0"
gtmpl_derive = "0.1.0"
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

```toml
[dependencies]
gtmpl = "0.1.2"
gtmpl = "0.2.0"
```

* [gtmpl at crates.io](https://crates.io/crate/gtmpl)
Expand All @@ -23,16 +23,9 @@ gtmpl = "0.1.2"
This is work in progress. Currently the following features are not supported:

* complex numbers
* comparing different number types
* `eq 1 1.0` will be `false`
* the following functions have not been implemented:
* `html`, `js`, `call` and `printf`

For now we use [serde_json](https://github.com/serde-rs/json)'s Value as internal
data type. However, this can not support passing functions to the context. In a
future release we will move to a custom data type that will be compatible with
serde_json.

## Usage

Basic template rendering can be achieved in a single line.
Expand All @@ -50,6 +43,19 @@ fn basic_template_rendering() {
For more examples please take a look at the
[gtmpl documentation](https://docs.rs/crate/gtmpl).

## Context

We use [gtmpl_value](https://github.com/fiji-flo/gtmpl_value)'s Value as internal
data type. [gtmpl_derive](https://github.com/fiji-flo/gtmpl_derive) provides a
handy `derive` marco to generate the `From` implmentation for `Value`.

See:

* [gtmpl_value at crates.io](https://crates.io/crate/gtmpl_value)
* [gtmpl_value documentation](https://docs.rs/crate/gtmpl_value)
* [gtmpl_derive at crates.io](https://crates.io/crate/gtmpl_derive)
* [gtmpl_derive documentation](https://docs.rs/crate/gtmpl_derive)

## Why do we need this?

The main motivation for this is to make it easier to write dev-ops tools in Rust
Expand Down
3 changes: 1 addition & 2 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use std::sync::Arc;
use std::io::Write;
use std::collections::{HashMap, VecDeque};

use funcs::Func;
use template::Template;
use utils::is_true;
use node::*;

use gtmpl_value::Value;
use gtmpl_value::{Func, Value};

struct Variable {
name: String,
Expand Down
53 changes: 49 additions & 4 deletions src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ use std::cmp::Ordering;
use std::fmt::Write;
use std::sync::Arc;

use gtmpl_value::Value;
use gtmpl_value::{Func, Value};

extern crate percent_encoding;
use self::percent_encoding::{DEFAULT_ENCODE_SET, utf8_percent_encode};

use utils::is_true;

/// Function type that is used to implement builtin and custom functions.
pub type Func = fn(&[Arc<Any>]) -> Result<Arc<Any>, String>;

lazy_static! {
/// Map of all builtin function.
pub static ref BUILTINS: HashMap<String, Func> = {
Expand All @@ -41,6 +38,54 @@ macro_rules! varc(
($x:expr) => { Arc::new(Value::from($x)) }
);

/// Help to write new functions for gtmpl.
#[macro_export]
macro_rules! gtmpl_fn {
(
$(#[$outer:meta])*
fn $name:ident() -> Result<$otyp:ty, String>
{ $($body:tt)* }
) => {
$(#[$outer])*
pub fn $name(args: &[Arc<Any>]) -> Result<Arc<Any>, String> {
fn inner() -> Result<$otyp, String> {
$($body)*
}
Ok(Arc::new(Value::from(inner()?)))
}
};
(
$(#[$outer:meta])*
fn $name:ident($arg0:ident : $typ0:ty$(, $arg:ident : $typ:ty),*) -> Result<$otyp:ty, String>
{ $($body:tt)* }
) => {
$(#[$outer])*
pub fn $name(args: &[::std::sync::Arc<::std::any::Any>]) -> Result<::std::sync::Arc<::std::any::Any>, String> {
#[allow(unused_mut)]
let mut args = args;
if args.is_empty() {
return Err(String::from("at least one argument required"));
}
let x = &args[0];
let $arg0 = x.downcast_ref::<::gtmpl_value::Value>()
.ok_or_else(|| "unable to downcast".to_owned())?;
let $arg0: $typ0 = ::gtmpl_value::from_value($arg0)
.ok_or_else(|| "unable to convert from Value".to_owned())?;
$(args = &args[1..];
let x = &args[0];
let $arg = x.downcast_ref::<::gtmpl_value::Value>()
.ok_or_else(|| "unable to downcast".to_owned())?;
let $arg: $typ = ::gtmpl_value::from_value($arg)
.ok_or_else(|| "unable to convert from Value".to_owned())?;)*;
fn inner($arg0 : $typ0, $($arg : $typ,)*) -> Result<$otyp, String> {
$($body)*
}
let ret: ::gtmpl_value::Value = inner($arg0, $($arg),*)?.into();
Ok(::std::sync::Arc::new(ret))
}
}
}

macro_rules! gn {
(
$(#[$outer:meta])*
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub use template::Template;
pub use exec::Context;

#[doc(inline)]
pub use funcs::Func;
pub use gtmpl_value::Func;

pub use gtmpl_value::Value;

Expand Down
2 changes: 1 addition & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::{HashMap, VecDeque};
use lexer::{Item, ItemType, Lexer};
use node::*;
use utils::*;
use funcs::Func;
use gtmpl_value::Func;

pub struct Parser<'a> {
name: &'a str,
Expand Down
3 changes: 2 additions & 1 deletion src/template.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::collections::HashMap;

use parse::{Tree, Parser, parse};
use funcs::Func;
use funcs::BUILTINS;
use node::TreeId;

use gtmpl_value::Func;

/// The main template structure.
#[derive(Default)]
pub struct Template<'a> {
Expand Down

0 comments on commit b091121

Please sign in to comment.