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