rustybars 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/rustybars/src/engine.rs +63 -0
- data/ext/rustybars/src/errors.rs +39 -0
- data/ext/rustybars/src/lib.rs +10 -57
- data/ext/rustybars/src/namespace.rs +12 -0
- data/ext/rustybars/src/template.rs +54 -0
- data/lib/rustybars/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63b3077155604ec100bf23a804db7cc05c58a2c638bf353069b54c033943e953
|
4
|
+
data.tar.gz: 10051bbaa90b5f132ab8790f443f77911f1677bef26dca75adf6cbdb9703ff1e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 437f489e8405d717bbeb5670a8a1acd318d5920385d85b239390f3b27586cb6534757f0d9d0b63573863ba6b0fb6650cdb2846f862fecff567b1e145aabfb5b1
|
7
|
+
data.tar.gz: e2cb1d4542158d1301c6287d961126f9ebc6ae4b578da45bfd8d36d853976090c81a74e425c269e9e7cc4757c8fc9b147fece6dac8038cb5b587a99674a7b8ee
|
@@ -0,0 +1,63 @@
|
|
1
|
+
use std::fmt::{self, Debug, Formatter};
|
2
|
+
|
3
|
+
use handlebars::Handlebars;
|
4
|
+
use magnus::{function, method, prelude::*, scan_args::{get_kwargs, scan_args}, Error, Object, RHash, Ruby, Value};
|
5
|
+
|
6
|
+
use crate::{namespace::RUSTYBARS_MODULE, template::CompiledTemplate};
|
7
|
+
|
8
|
+
#[magnus::wrap(class = "Rustybars::Engine")]
|
9
|
+
pub struct Engine {
|
10
|
+
pub registry: Handlebars<'static>
|
11
|
+
}
|
12
|
+
|
13
|
+
impl Debug for Engine {
|
14
|
+
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
|
15
|
+
f.debug_struct("Engine")
|
16
|
+
.field("strict_mode", &self.registry.strict_mode())
|
17
|
+
.field("dev_mode", &self.registry.dev_mode())
|
18
|
+
.field("prevent_indent", &self.registry.prevent_indent())
|
19
|
+
.finish()
|
20
|
+
}
|
21
|
+
}
|
22
|
+
|
23
|
+
impl Engine {
|
24
|
+
fn new(args: &[Value]) -> Result<Self, Error> {
|
25
|
+
let args = scan_args::<(), (), (), (), RHash, ()>(args)?;
|
26
|
+
let args = get_kwargs(
|
27
|
+
args.keywords,
|
28
|
+
&[],
|
29
|
+
&["strict_mode", "dev_mode", "prevent_indent"]
|
30
|
+
)?;
|
31
|
+
let _: () = args.required;
|
32
|
+
let (strict_mode, dev_mode, prevent_indent): (Option<bool>, Option<bool>, Option<bool>,) = args.optional;
|
33
|
+
let _: () = args.splat;
|
34
|
+
|
35
|
+
let mut hbs = Handlebars::new();
|
36
|
+
hbs.set_strict_mode(strict_mode.unwrap_or(false));
|
37
|
+
hbs.set_dev_mode(dev_mode.unwrap_or(false));
|
38
|
+
hbs.set_prevent_indent(prevent_indent.unwrap_or(false));
|
39
|
+
Ok(Self { registry: hbs })
|
40
|
+
}
|
41
|
+
|
42
|
+
fn render(ruby: &Ruby, rb_self: &'static Self, source: String, data: String) -> Result<String, Error> {
|
43
|
+
let template = Self::compile(ruby, rb_self, source)?;
|
44
|
+
CompiledTemplate::render(ruby, &template, data)
|
45
|
+
}
|
46
|
+
|
47
|
+
fn compile(ruby: &Ruby, rb_self: &'static Self, source: String) -> Result<CompiledTemplate, Error> {
|
48
|
+
CompiledTemplate::compile(ruby, &rb_self.registry, source)
|
49
|
+
}
|
50
|
+
|
51
|
+
fn inspect(&self) -> Result<String, Error> {
|
52
|
+
Ok(format!("<Rustybars::{:?}>", self))
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
pub fn init(ruby: &Ruby) -> Result<(), Error> {
|
57
|
+
let class = ruby.get_inner(&RUSTYBARS_MODULE).define_class("Engine", ruby.class_object())?;
|
58
|
+
class.define_singleton_method("new", function!(Engine::new, -1))?;
|
59
|
+
class.define_method("compile", method!(Engine::compile, 1))?;
|
60
|
+
class.define_method("render", method!(Engine::render, 2))?;
|
61
|
+
class.define_method("inspect", method!(Engine::inspect, 0))?;
|
62
|
+
Ok(())
|
63
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
use magnus::{exception::ExceptionClass, value::Lazy, Error, Module, Ruby};
|
2
|
+
|
3
|
+
use super::namespace::RUSTYBARS_MODULE;
|
4
|
+
|
5
|
+
pub static RUSTYBARS_ERROR: Lazy<ExceptionClass> = Lazy::new(|ruby| {
|
6
|
+
ruby
|
7
|
+
.get_inner(&RUSTYBARS_MODULE)
|
8
|
+
.define_error("Error", ruby.exception_standard_error())
|
9
|
+
.unwrap()
|
10
|
+
});
|
11
|
+
|
12
|
+
pub static RUSTYBARS_JSON_ERROR: Lazy<ExceptionClass> = Lazy::new(|ruby| {
|
13
|
+
ruby
|
14
|
+
.get_inner(&RUSTYBARS_MODULE)
|
15
|
+
.define_error("JsonError", ruby.get_inner(&RUSTYBARS_ERROR))
|
16
|
+
.unwrap()
|
17
|
+
});
|
18
|
+
|
19
|
+
pub static RUSTYBARS_COMPILE_ERROR: Lazy<ExceptionClass> = Lazy::new(|ruby| {
|
20
|
+
ruby
|
21
|
+
.get_inner(&RUSTYBARS_MODULE)
|
22
|
+
.define_error("CompileError", ruby.get_inner(&RUSTYBARS_ERROR))
|
23
|
+
.unwrap()
|
24
|
+
});
|
25
|
+
|
26
|
+
pub static RUSTYBARS_RENDER_ERROR: Lazy<ExceptionClass> = Lazy::new(|ruby| {
|
27
|
+
ruby
|
28
|
+
.get_inner(&RUSTYBARS_MODULE)
|
29
|
+
.define_error("RenderError", ruby.get_inner(&RUSTYBARS_ERROR))
|
30
|
+
.unwrap()
|
31
|
+
});
|
32
|
+
|
33
|
+
pub fn init(ruby: &Ruby) -> Result<(), Error> {
|
34
|
+
Lazy::force(&RUSTYBARS_ERROR, ruby);
|
35
|
+
Lazy::force(&RUSTYBARS_JSON_ERROR, ruby);
|
36
|
+
Lazy::force(&RUSTYBARS_COMPILE_ERROR, ruby);
|
37
|
+
Lazy::force(&RUSTYBARS_RENDER_ERROR, ruby);
|
38
|
+
Ok(())
|
39
|
+
}
|
data/ext/rustybars/src/lib.rs
CHANGED
@@ -1,61 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
static RUSTYBARS: Lazy<RModule> = Lazy::new(|ruby| {
|
7
|
-
ruby
|
8
|
-
.define_module("Rustybars")
|
9
|
-
.unwrap()
|
10
|
-
});
|
11
|
-
|
12
|
-
static RUSTYBARS_ERROR: Lazy<exception::ExceptionClass> = Lazy::new(|ruby| {
|
13
|
-
ruby
|
14
|
-
.get_inner(&RUSTYBARS)
|
15
|
-
.define_error("Error", ruby.exception_standard_error())
|
16
|
-
.unwrap()
|
17
|
-
});
|
18
|
-
|
19
|
-
static RUSTYBARS_DATA_ERROR: Lazy<exception::ExceptionClass> = Lazy::new(|ruby| {
|
20
|
-
ruby
|
21
|
-
.get_inner(&RUSTYBARS)
|
22
|
-
.define_error("DataError", ruby.get_inner(&RUSTYBARS_ERROR))
|
23
|
-
.unwrap()
|
24
|
-
});
|
25
|
-
|
26
|
-
static RUSTYBARS_TEMPLATE_ERROR: Lazy<exception::ExceptionClass> = Lazy::new(|ruby| {
|
27
|
-
ruby
|
28
|
-
.get_inner(&RUSTYBARS)
|
29
|
-
.define_error("TemplateError", ruby.get_inner(&RUSTYBARS_ERROR))
|
30
|
-
.unwrap()
|
31
|
-
});
|
32
|
-
|
33
|
-
|
34
|
-
fn render(ruby: &Ruby, template: String, data: String) -> Result<String, Error> {
|
35
|
-
let variables: serde_json::Value = serde_json::from_str(&data).map_err(|e| {
|
36
|
-
Error::new(
|
37
|
-
ruby.get_inner(&RUSTYBARS_DATA_ERROR),
|
38
|
-
e.to_string(),
|
39
|
-
)
|
40
|
-
})?;
|
41
|
-
|
42
|
-
let hbs = Handlebars::new();
|
43
|
-
hbs.render_template(&template, &variables).map_err(|e| {
|
44
|
-
Error::new(
|
45
|
-
ruby.get_inner(&RUSTYBARS_TEMPLATE_ERROR),
|
46
|
-
e.to_string()
|
47
|
-
)
|
48
|
-
})
|
49
|
-
}
|
1
|
+
mod namespace;
|
2
|
+
mod errors;
|
3
|
+
mod engine;
|
4
|
+
mod template;
|
50
5
|
|
51
6
|
#[magnus::init]
|
52
|
-
fn init(ruby: &Ruby) -> Result<(), Error> {
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
ruby
|
57
|
-
.get_inner(&RUSTYBARS)
|
58
|
-
.define_singleton_method("render", function!(render, 2))?;
|
59
|
-
|
7
|
+
fn init(ruby: &magnus::Ruby) -> Result<(), magnus::Error> {
|
8
|
+
namespace::init(ruby)?;
|
9
|
+
errors::init(ruby)?;
|
10
|
+
engine::init(ruby)?;
|
11
|
+
template::init(ruby)?;
|
60
12
|
Ok(())
|
61
13
|
}
|
14
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
use magnus::{value::Lazy, Error, RModule, Ruby};
|
2
|
+
|
3
|
+
pub static RUSTYBARS_MODULE: Lazy<RModule> = Lazy::new(|ruby| {
|
4
|
+
ruby
|
5
|
+
.define_module("Rustybars")
|
6
|
+
.unwrap()
|
7
|
+
});
|
8
|
+
|
9
|
+
pub fn init(ruby: &Ruby) -> Result<(), Error> {
|
10
|
+
Lazy::force(&RUSTYBARS_MODULE, ruby);
|
11
|
+
Ok(())
|
12
|
+
}
|
@@ -0,0 +1,54 @@
|
|
1
|
+
use handlebars::{Context, Handlebars, RenderContext, Renderable, Template};
|
2
|
+
use magnus::{method, prelude::*, Class, Error, Ruby};
|
3
|
+
|
4
|
+
use crate::{errors::{RUSTYBARS_COMPILE_ERROR, RUSTYBARS_JSON_ERROR, RUSTYBARS_RENDER_ERROR}, namespace::RUSTYBARS_MODULE};
|
5
|
+
|
6
|
+
#[magnus::wrap(class = "Rustybars::CompiledTemplate")]
|
7
|
+
pub struct CompiledTemplate {
|
8
|
+
pub handlebars: &'static Handlebars<'static>,
|
9
|
+
pub template: Template
|
10
|
+
}
|
11
|
+
|
12
|
+
impl CompiledTemplate {
|
13
|
+
pub fn compile(ruby: &Ruby, handlebars: &'static Handlebars, source: String) -> Result<Self, Error> {
|
14
|
+
let template = Template::compile(&source).map_err(|e| {
|
15
|
+
Error::new(
|
16
|
+
ruby.get_inner(&RUSTYBARS_COMPILE_ERROR),
|
17
|
+
e.to_string()
|
18
|
+
)
|
19
|
+
})?;
|
20
|
+
Ok(Self{
|
21
|
+
handlebars,
|
22
|
+
template
|
23
|
+
})
|
24
|
+
}
|
25
|
+
|
26
|
+
pub fn render(ruby: &Ruby, rb_self: &Self, data: String) -> Result<String, Error> {
|
27
|
+
let hbs = &rb_self.handlebars;
|
28
|
+
|
29
|
+
let data: serde_json::Value = serde_json::from_str(&data).map_err(|e| {
|
30
|
+
Error::new(
|
31
|
+
ruby.get_inner(&RUSTYBARS_JSON_ERROR),
|
32
|
+
e.to_string(),
|
33
|
+
)
|
34
|
+
})?;
|
35
|
+
|
36
|
+
let ctx = Context::from(data);
|
37
|
+
|
38
|
+
let mut render_context = RenderContext::new(None);
|
39
|
+
|
40
|
+
rb_self.template.renders(&hbs, &ctx, &mut render_context).map_err(|e| {
|
41
|
+
Error::new(
|
42
|
+
ruby.get_inner(&RUSTYBARS_RENDER_ERROR),
|
43
|
+
e.to_string()
|
44
|
+
)
|
45
|
+
})
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
pub fn init(ruby: &Ruby) -> Result<(), Error> {
|
50
|
+
let class = ruby.get_inner(&RUSTYBARS_MODULE).define_class("CompiledTemplate", ruby.class_object())?;
|
51
|
+
class.undef_default_alloc_func();
|
52
|
+
class.define_method("render", method!(CompiledTemplate::render, 1))?;
|
53
|
+
Ok(())
|
54
|
+
}
|
data/lib/rustybars/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rustybars
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andriy Yanko
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-01-
|
11
|
+
date: 2024-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -20,7 +20,11 @@ extra_rdoc_files: []
|
|
20
20
|
files:
|
21
21
|
- ext/rustybars/Cargo.toml
|
22
22
|
- ext/rustybars/extconf.rb
|
23
|
+
- ext/rustybars/src/engine.rs
|
24
|
+
- ext/rustybars/src/errors.rs
|
23
25
|
- ext/rustybars/src/lib.rs
|
26
|
+
- ext/rustybars/src/namespace.rs
|
27
|
+
- ext/rustybars/src/template.rs
|
24
28
|
- lib/rustybars.rb
|
25
29
|
- lib/rustybars/version.rb
|
26
30
|
homepage: https://github.com/ayanko/rustybars
|