mod_string/
lib.rs

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use config::get_or_create_sub_module;
use mlua::Lua;

pub fn register(lua: &Lua) -> anyhow::Result<()> {
    let string_mod = get_or_create_sub_module(lua, "string")?;

    string_mod.set(
        "replace",
        lua.create_function(move |_, (s, from, to): (String, String, String)| {
            Ok(s.replace(&from, &to))
        })?,
    )?;

    string_mod.set(
        "replacen",
        lua.create_function(
            move |_, (s, from, to, count): (String, String, String, usize)| {
                Ok(s.replacen(&from, &to, count))
            },
        )?,
    )?;

    string_mod.set(
        "rsplit",
        lua.create_function(move |_, (s, pattern): (String, String)| {
            Ok(s.rsplit(&pattern)
                .map(|s| s.to_string())
                .collect::<Vec<String>>())
        })?,
    )?;

    string_mod.set(
        "rsplitn",
        lua.create_function(move |_, (s, limit, pattern): (String, usize, String)| {
            Ok(s.rsplitn(limit, &pattern)
                .map(|s| s.to_string())
                .collect::<Vec<String>>())
        })?,
    )?;

    string_mod.set(
        "split",
        lua.create_function(move |_, (s, pattern): (String, String)| {
            Ok(s.split(&pattern)
                .map(|s| s.to_string())
                .collect::<Vec<String>>())
        })?,
    )?;

    string_mod.set(
        "splitn",
        lua.create_function(move |_, (s, limit, pattern): (String, usize, String)| {
            Ok(s.splitn(limit, &pattern)
                .map(|s| s.to_string())
                .collect::<Vec<String>>())
        })?,
    )?;

    string_mod.set(
        "split_whitespace",
        lua.create_function(move |_, s: String| {
            Ok(s.split_whitespace()
                .map(|s| s.to_string())
                .collect::<Vec<String>>())
        })?,
    )?;

    string_mod.set(
        "split_ascii_whitespace",
        lua.create_function(move |_, s: String| {
            Ok(s.split_ascii_whitespace()
                .map(|s| s.to_string())
                .collect::<Vec<String>>())
        })?,
    )?;

    string_mod.set(
        "trim",
        lua.create_function(move |_, s: String| Ok(s.trim().to_string()))?,
    )?;
    string_mod.set(
        "trim_end",
        lua.create_function(move |_, s: String| Ok(s.trim_end().to_string()))?,
    )?;
    string_mod.set(
        "trim_start",
        lua.create_function(move |_, s: String| Ok(s.trim_start().to_string()))?,
    )?;

    string_mod.set(
        "psl_domain",
        lua.create_function(move |_, s: String| Ok(psl::domain_str(&s).map(|s| s.to_string())))?,
    )?;

    string_mod.set(
        "psl_suffix",
        lua.create_function(move |_, s: String| Ok(psl::suffix_str(&s).map(|s| s.to_string())))?,
    )?;

    Ok(())
}