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
102
103
104
105
106
107
108
109
110
111
use rlua::prelude::*;
use heck::*;

pub struct LuaHeck(String);

impl LuaUserData for LuaHeck {
    fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
        methods.add_method("to_camel_case", |_, this: &LuaHeck, _:()| {
            Ok(this.0.to_camel_case())
        });
        methods.add_method("to_kebab_case", |_, this: &LuaHeck, _:()| {
            Ok(this.0.to_kebab_case())
        });
        methods.add_method("to_mixed_case", |_, this: &LuaHeck, _:()| {
            Ok(this.0.to_mixed_case())
        });
        methods.add_method("to_shouty_snake_case", |_, this: &LuaHeck, _:()| {
            Ok(this.0.to_shouty_snake_case())
        });
        methods.add_method("to_snake_case", |_, this: &LuaHeck, _:()| {
            Ok(this.0.to_snake_case())
        });
        methods.add_method("to_title_case", |_, this: &LuaHeck, _:()| {
            Ok(this.0.to_title_case())
        });
    }
}

pub fn init(lua: &Lua) -> crate::Result<()> {

    let module = lua.create_table()?;

    module.set("new", lua.create_function(|_, text: String| {
        Ok(LuaHeck(text))
    })?)?;

    lua.globals().set("heck", module)?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lua_heck_cambel () {
        let lua = Lua::new();
        init(&lua).unwrap();

        lua.exec::<_, ()>(r#"
            local val = heck.new("We are not in the least afraid of ruins")
            assert(val:to_camel_case() == "WeAreNotInTheLeastAfraidOfRuins")
        "#, None).unwrap();
    }

    #[test]
    fn lua_heck_kebab () {
        let lua = Lua::new();
        init(&lua).unwrap();

        lua.exec::<_, ()>(r#"
            local val = heck.new("We are going to inherit the earth")
            assert(val:to_kebab_case() == "we-are-going-to-inherit-the-earth")
        "#, None).unwrap();
    }

    #[test]
    fn lua_heck_mixed () {
        let lua = Lua::new();
        init(&lua).unwrap();

        lua.exec::<_, ()>(r#"
            local val = heck.new("It is we who built these palaces and cities")
            assert(val:to_mixed_case() == "itIsWeWhoBuiltThesePalacesAndCities")
        "#, None).unwrap();
    }

    #[test]
    fn lua_heck_shouty() {
        let lua = Lua::new();
        init(&lua).unwrap();

        lua.exec::<_, ()>(r#"
            local val = heck.new("That world is growing in this minute")
            assert(val:to_shouty_snake_case() == "THAT_WORLD_IS_GROWING_IN_THIS_MINUTE")
        "#, None).unwrap();
    }

    #[test]
    fn lua_heck_snake () {
        let lua = Lua::new();
        init(&lua).unwrap();

        lua.exec::<_, ()>(r#"
            local val = heck.new("We carry a new world here, in our hearts")
            assert(val:to_snake_case() == "we_carry_a_new_world_here_in_our_hearts")
        "#, None).unwrap();
    }

    #[test]
    fn lua_heck_title () {
        let lua = Lua::new();
        init(&lua).unwrap();

        lua.exec::<_, ()>(r#"
            local val = heck.new("We have always lived in slums and holes in the wall")
            assert(val:to_title_case() == "We Have Always Lived In Slums And Holes In The Wall")
        "#, None).unwrap();
    }
}