-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bbmustache.gleam
67 lines (50 loc) 路 1.65 KB
/
bbmustache.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import gleam/string_builder.{type StringBuilder}
// Template compilation
pub type Template
pub type CompileError {
FileNotFound
IncorrectSection(String)
InvalidDelimiters
UnclosedSection(String)
UnclosedTag
UnsupportedTag(String)
}
@external(erlang, "gleam_bbmustache_native", "try_catch")
fn try_catch(a: fn() -> a) -> Result(Template, CompileError)
@external(erlang, "bbmustache", "parse_binary")
fn throwing_compile(a: String) -> Template
@external(erlang, "bbmustache", "parse_file")
fn throwing_compile_file(a: String) -> Template
pub fn compile(tmpl: String) -> Result(Template, CompileError) {
try_catch(fn() { throwing_compile(tmpl) })
}
pub fn compile_file(path: String) -> Result(Template, CompileError) {
try_catch(fn() { throwing_compile_file(path) })
}
// Template rendering
pub type Argument
type KeyType {
Binary
}
type RenderOption {
KeyType(KeyType)
}
@external(erlang, "bbmustache", "compile")
fn do_render(
a: Template,
b: List(#(String, Argument)),
c: List(RenderOption),
) -> String
pub fn render(template, injecting args: List(#(String, Argument))) -> String {
do_render(template, args, [KeyType(Binary)])
}
@external(erlang, "gleam_bbmustache_native", "argument")
pub fn int(of of: Int) -> Argument
@external(erlang, "gleam_bbmustache_native", "argument")
pub fn string(of of: String) -> Argument
@external(erlang, "gleam_bbmustache_native", "argument")
pub fn builder(of of: StringBuilder) -> Argument
@external(erlang, "gleam_bbmustache_native", "argument")
pub fn object(of of: List(#(String, Argument))) -> Argument
@external(erlang, "gleam_bbmustache_native", "argument")
pub fn list(of of: List(Argument)) -> Argument