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