mustachers 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 856f256560d1bde9d3a7c00bb9ff0aa79b3dc00d4a999f46c57193c7d70c82fd
4
- data.tar.gz: 4dfd897b7d42617534044a649618897df0743e37b06fa3a3c75b9ffe9d1c576e
3
+ metadata.gz: dcf63941db7f11f8d4f5e73496a893b081c5a52afdbac876700584e3b73be5c0
4
+ data.tar.gz: ca6b87ba93af3cde1b434c2928ba2db5655247ecbc7c30d276309f8d5ee4631d
5
5
  SHA512:
6
- metadata.gz: 5c4391f93cf872b2aa394400b2b815b572d7a19bed4353cee82a537bc1d1be7df064256ff5a49a6c65b1a867a3cd616de7e257ba8a8548f2c120ba88c227aae8
7
- data.tar.gz: 344d0c30e6286aeef9b3b775d0f7d5a0f716d04037d6cec6fc1e602db6872ed180e48602f80ade7661ade00ca10e6916f8ed9744e57f21f2a64cce2c491bd01e
6
+ metadata.gz: e5beef1587a5798e47d313db2eaac20d290586750f480f452ad78e7c5339a657d24c37a184c17e04c7d899ea19cf866b641686d2e82ae48427f34a528b727508
7
+ data.tar.gz: 94cd2f2a78a91248cd0e5a0ce27a30dd5324d6e41d51cdeb892eb89529fd1020276100ead46c03af746ec5ab3b918b355280ba1bcbe488e32b96f1a17039c384
@@ -2,11 +2,12 @@
2
2
  name = "mustachers"
3
3
  version = "0.1.0"
4
4
  edition = "2021"
5
- authors = ["Sam French <Sam.French@bbc.co.uk>"]
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" }
@@ -1,11 +1,12 @@
1
- extern crate mustache;
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
- fn render(template: String, params: RHash) -> Result<String, Error> {
7
- let template = mustache::compile_str(&template).expect("Failed to compile");
8
- let mut bytes = vec![];
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
- template.render(&mut bytes, &data).expect("Failed to render");
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
- renderer_module.define_singleton_method("render", function!(render, 2))?;
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, &params)
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
+ // }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mustachers
4
- VERSION = "0.1.1"
4
+ VERSION = '0.1.2'
5
5
  end
data/lib/mustachers.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "mustachers/version"
4
- require_relative "mustachers/mustachers"
3
+ require_relative 'mustachers/version'
4
+ require 'mustachers/mustachers'
5
5
 
6
6
  module Mustachers
7
7
  class Error < StandardError; end
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.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-07-27 00:00:00.000000000 Z
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