mustachers 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/mustachers/Cargo.toml +2 -1
- data/ext/mustachers/src/lib.rs +11 -10
- data/ext/mustachers/src/renderer.rs +43 -0
- data/ext/mustachers/tests/mustachers_test.rs +16 -0
- data/lib/mustachers/version.rb +1 -1
- data/lib/mustachers.rb +2 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dcf63941db7f11f8d4f5e73496a893b081c5a52afdbac876700584e3b73be5c0
|
4
|
+
data.tar.gz: ca6b87ba93af3cde1b434c2928ba2db5655247ecbc7c30d276309f8d5ee4631d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5beef1587a5798e47d313db2eaac20d290586750f480f452ad78e7c5339a657d24c37a184c17e04c7d899ea19cf866b641686d2e82ae48427f34a528b727508
|
7
|
+
data.tar.gz: 94cd2f2a78a91248cd0e5a0ce27a30dd5324d6e41d51cdeb892eb89529fd1020276100ead46c03af746ec5ab3b918b355280ba1bcbe488e32b96f1a17039c384
|
data/ext/mustachers/Cargo.toml
CHANGED
@@ -2,11 +2,12 @@
|
|
2
2
|
name = "mustachers"
|
3
3
|
version = "0.1.0"
|
4
4
|
edition = "2021"
|
5
|
-
authors = ["Sam French
|
5
|
+
authors = ["Sam French"]
|
6
6
|
publish = false
|
7
7
|
|
8
8
|
[lib]
|
9
9
|
crate-type = ["cdylib"]
|
10
|
+
name = "mustachers"
|
10
11
|
|
11
12
|
[dependencies]
|
12
13
|
magnus = { version = "0.4" }
|
data/ext/mustachers/src/lib.rs
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
use magnus::{
|
2
|
+
define_module, exception::runtime_error, function, prelude::*, r_hash::ForEach, Error, RHash,
|
3
|
+
Symbol,
|
4
|
+
};
|
3
5
|
use std::collections::HashMap;
|
4
|
-
use magnus::{define_module, prelude::*, Error, function, RHash, Symbol, r_hash::ForEach};
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
mod renderer;
|
8
|
+
|
9
|
+
pub fn wrapper(template: String, params: RHash) -> Result<String, Error> {
|
9
10
|
let mut data: HashMap<String, String> = HashMap::new();
|
10
11
|
|
11
12
|
params.foreach(|key: Symbol, value: String| {
|
@@ -14,16 +15,16 @@ fn render(template: String, params: RHash) -> Result<String, Error> {
|
|
14
15
|
return Ok(ForEach::Continue);
|
15
16
|
})?;
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
return Ok(String::from_utf8(bytes).expect("Failed to encode string"));
|
18
|
+
return renderer::render(template, data)
|
19
|
+
.map_err(|e| Error::new(runtime_error(), e.to_string()));
|
20
20
|
}
|
21
21
|
|
22
22
|
#[magnus::init]
|
23
23
|
fn init() -> Result<(), Error> {
|
24
24
|
let mustachers_module = define_module("Mustachers")?;
|
25
25
|
let renderer_module = mustachers_module.define_module("Renderer")?;
|
26
|
-
|
26
|
+
|
27
|
+
renderer_module.define_singleton_method("render", function!(wrapper, 2))?;
|
27
28
|
|
28
29
|
Ok(())
|
29
30
|
}
|
@@ -0,0 +1,43 @@
|
|
1
|
+
use core::fmt::Error;
|
2
|
+
use std::collections::HashMap;
|
3
|
+
|
4
|
+
extern crate mustache;
|
5
|
+
|
6
|
+
pub fn render(template: String, params: HashMap<String, String>) -> Result<String, Error> {
|
7
|
+
let template = mustache::compile_str(&template).expect("Failed to compile");
|
8
|
+
let mut bytes = vec![];
|
9
|
+
|
10
|
+
template
|
11
|
+
.render(&mut bytes, ¶ms)
|
12
|
+
.expect("Failed to render");
|
13
|
+
|
14
|
+
return Ok(String::from_utf8(bytes).expect("Failed to encode string"));
|
15
|
+
}
|
16
|
+
|
17
|
+
#[cfg(test)]
|
18
|
+
mod tests {
|
19
|
+
use crate::renderer::render;
|
20
|
+
use std::collections::HashMap;
|
21
|
+
|
22
|
+
#[test]
|
23
|
+
fn it_interpolates_correctly() {
|
24
|
+
let template =
|
25
|
+
String::from("<html lang=\"{{{lang}}}\"/>{{{ top }}}{{{ middle }}}{{{ bottom }}}");
|
26
|
+
let params = HashMap::from([
|
27
|
+
(String::from("top"), String::from("<h1>Page heading</h1>")),
|
28
|
+
(
|
29
|
+
String::from("middle"),
|
30
|
+
String::from("<p>Paragraph of text.</p>"),
|
31
|
+
),
|
32
|
+
(
|
33
|
+
String::from("bottom"),
|
34
|
+
String::from("<a href=\"#about\">Page link</a>"),
|
35
|
+
),
|
36
|
+
(String::from("lang"), String::from("en-GB")),
|
37
|
+
(String::from("foo"), String::from("bar")),
|
38
|
+
]);
|
39
|
+
|
40
|
+
let result = render(template, params).unwrap();
|
41
|
+
assert_eq!(result, "<html lang=\"en-GB\"/><h1>Page heading</h1><p>Paragraph of text.</p><a href=\"#about\">Page link</a>");
|
42
|
+
}
|
43
|
+
}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
// Waiting on a fix in: https://github.com/rust-lang/cargo/issues/6659
|
2
|
+
// use magnus::{RHash, Symbol};
|
3
|
+
// use mustachers;
|
4
|
+
|
5
|
+
// #[test]
|
6
|
+
// fn it_interpolates_correctly() {
|
7
|
+
// let params = RHash::new();
|
8
|
+
// params.aset(Symbol::new("top"), String::from("<h1>Page heading</h1>")).unwrap();
|
9
|
+
// params.aset(Symbol::new("middle"), String::from("<p>Paragraph of text.</p>")).unwrap();
|
10
|
+
// params.aset(Symbol::new("bottom"), String::from("<a href=\"#about\">Page link</a>")).unwrap();
|
11
|
+
// params.aset(Symbol::new("lang"), String::from("en-GB")).unwrap();
|
12
|
+
// params.aset(Symbol::new("foo"), String::from("bar")).unwrap();
|
13
|
+
|
14
|
+
// let result = mustachers::wrapper(String::from("<h1>{{template}}</h1>"), params).unwrap();
|
15
|
+
// assert_eq!(result, "<html lang=\"en-GB\"/><h1>Page heading</h1><p>Paragraph of text.</p><a href=\"#about\">Page link</a>")
|
16
|
+
// }
|
data/lib/mustachers/version.rb
CHANGED
data/lib/mustachers.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mustachers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam French
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-09-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -53,6 +53,8 @@ files:
|
|
53
53
|
- ext/mustachers/Cargo.toml
|
54
54
|
- ext/mustachers/extconf.rb
|
55
55
|
- ext/mustachers/src/lib.rs
|
56
|
+
- ext/mustachers/src/renderer.rs
|
57
|
+
- ext/mustachers/tests/mustachers_test.rs
|
56
58
|
- lib/mustachers.rb
|
57
59
|
- lib/mustachers/version.rb
|
58
60
|
- sig/mustachers.rbs
|