aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Letan <lthms@soap.coffee>2020-10-21 22:38:18 +0200
committerThomas Letan <lthms@soap.coffee>2020-10-21 22:38:18 +0200
commit462c2179ed8150dba77fee4b1985e54ce44cd245 (patch)
tree5ca8c852198b5bed5e87e167a2f3f484df2f0bf2
parentImplement Display (diff)
Introduce typed-urls-tera
-rw-r--r--typed-urls-tera/Cargo.toml10
-rw-r--r--typed-urls-tera/src/lib.rs32
2 files changed, 42 insertions, 0 deletions
diff --git a/typed-urls-tera/Cargo.toml b/typed-urls-tera/Cargo.toml
new file mode 100644
index 0000000..85c9cf9
--- /dev/null
+++ b/typed-urls-tera/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "typed-urls-tera"
+version = "0.1.0"
+authors = ["Thomas Letan <lthms@soap.coffee>"]
+edition = "2018"
+
+[dependencies]
+tera = "*"
+serde_json = "*"
+typed-urls = { path = "../" } \ No newline at end of file
diff --git a/typed-urls-tera/src/lib.rs b/typed-urls-tera/src/lib.rs
new file mode 100644
index 0000000..2b32737
--- /dev/null
+++ b/typed-urls-tera/src/lib.rs
@@ -0,0 +1,32 @@
+use std::collections::HashMap;
+use std::fmt::Debug;
+
+use serde_json::Value;
+use tera::{Tera, Error, Function};
+use typed_urls::Routing;
+
+struct TeraFunction<R>(R);
+
+impl<R> Function for TeraFunction<R>
+where
+ R : Routing + Sync + Send,
+{
+ fn call(
+ &self,
+ args : &HashMap<String, Value>,
+ ) -> Result<Value, Error> {
+ let res = self.0.to_url(args).ok_or(Error::msg("missing keys"))?;
+
+ Ok(Value::String(res.to_string()))
+ }
+}
+
+pub fn register_typed_url<R>(tera : &mut Tera) -> ()
+where R : Routing + Sync + Send + Debug + Clone + 'static {
+ for route in R::enumerate() {
+ tera.register_function(
+ &format!("{:?}Url", route),
+ TeraFunction(route.clone()),
+ )
+ }
+}