1use config::get_or_create_sub_module;
2use mlua::Lua;
3
4pub fn register(lua: &Lua) -> anyhow::Result<()> {
5 let string_mod = get_or_create_sub_module(lua, "string")?;
6
7 string_mod.set(
8 "replace",
9 lua.create_function(move |_, (s, from, to): (String, String, String)| {
10 Ok(s.replace(&from, &to))
11 })?,
12 )?;
13
14 string_mod.set(
15 "replacen",
16 lua.create_function(
17 move |_, (s, from, to, count): (String, String, String, usize)| {
18 Ok(s.replacen(&from, &to, count))
19 },
20 )?,
21 )?;
22
23 string_mod.set(
24 "rsplit",
25 lua.create_function(move |_, (s, pattern): (String, String)| {
26 Ok(s.rsplit(&pattern)
27 .map(|s| s.to_string())
28 .collect::<Vec<String>>())
29 })?,
30 )?;
31
32 string_mod.set(
33 "rsplitn",
34 lua.create_function(move |_, (s, limit, pattern): (String, usize, String)| {
35 Ok(s.rsplitn(limit, &pattern)
36 .map(|s| s.to_string())
37 .collect::<Vec<String>>())
38 })?,
39 )?;
40
41 string_mod.set(
42 "split",
43 lua.create_function(move |_, (s, pattern): (String, String)| {
44 Ok(s.split(&pattern)
45 .map(|s| s.to_string())
46 .collect::<Vec<String>>())
47 })?,
48 )?;
49
50 string_mod.set(
51 "splitn",
52 lua.create_function(move |_, (s, limit, pattern): (String, usize, String)| {
53 Ok(s.splitn(limit, &pattern)
54 .map(|s| s.to_string())
55 .collect::<Vec<String>>())
56 })?,
57 )?;
58
59 string_mod.set(
60 "split_whitespace",
61 lua.create_function(move |_, s: String| {
62 Ok(s.split_whitespace()
63 .map(|s| s.to_string())
64 .collect::<Vec<String>>())
65 })?,
66 )?;
67
68 string_mod.set(
69 "split_ascii_whitespace",
70 lua.create_function(move |_, s: String| {
71 Ok(s.split_ascii_whitespace()
72 .map(|s| s.to_string())
73 .collect::<Vec<String>>())
74 })?,
75 )?;
76
77 string_mod.set(
78 "trim",
79 lua.create_function(move |_, s: String| Ok(s.trim().to_string()))?,
80 )?;
81 string_mod.set(
82 "trim_end",
83 lua.create_function(move |_, s: String| Ok(s.trim_end().to_string()))?,
84 )?;
85 string_mod.set(
86 "trim_start",
87 lua.create_function(move |_, s: String| Ok(s.trim_start().to_string()))?,
88 )?;
89
90 string_mod.set(
91 "psl_domain",
92 lua.create_function(move |_, s: String| Ok(psl::domain_str(&s).map(|s| s.to_string())))?,
93 )?;
94
95 string_mod.set(
96 "psl_suffix",
97 lua.create_function(move |_, s: String| Ok(psl::suffix_str(&s).map(|s| s.to_string())))?,
98 )?;
99
100 string_mod.set(
101 "eval_template",
102 lua.create_function(
103 move |_, (name, template, context): (String, String, mlua::Value)| {
104 let engine = kumo_template::TemplateEngine::new();
105 engine
106 .render(&name, &template, context)
107 .map_err(config::any_err)
108 },
109 )?,
110 )?;
111
112 Ok(())
113}