1+ use rlua:: prelude:: * ;
2+ use heck:: * ;
3+
4+ pub struct LuaCase ( String ) ;
5+
6+ impl LuaUserData for LuaCase {
7+ fn add_methods < ' lua , M : LuaUserDataMethods < ' lua , Self > > ( methods : & mut M ) {
8+ methods. add_method ( "to_camel" , |_, this : & LuaCase , _: ( ) | {
9+ Ok ( this. 0 . to_camel_case ( ) )
10+ } ) ;
11+ methods. add_method ( "to_kebab" , |_, this : & LuaCase , _: ( ) | {
12+ Ok ( this. 0 . to_kebab_case ( ) )
13+ } ) ;
14+ methods. add_method ( "to_mixed" , |_, this : & LuaCase , _: ( ) | {
15+ Ok ( this. 0 . to_mixed_case ( ) )
16+ } ) ;
17+ methods. add_method ( "to_shouty_snake" , |_, this : & LuaCase , _: ( ) | {
18+ Ok ( this. 0 . to_shouty_snake_case ( ) )
19+ } ) ;
20+ methods. add_method ( "to_snake" , |_, this : & LuaCase , _: ( ) | {
21+ Ok ( this. 0 . to_snake_case ( ) )
22+ } ) ;
23+ methods. add_method ( "to_title" , |_, this : & LuaCase , _: ( ) | {
24+ Ok ( this. 0 . to_title_case ( ) )
25+ } ) ;
26+ }
27+ }
28+
29+ pub fn init ( lua : & Lua ) -> crate :: Result < ( ) > {
30+
31+ let module = lua. create_table ( ) ?;
32+
33+ module. set ( "new" , lua. create_function ( |_, text : String | {
34+ Ok ( LuaCase ( text) )
35+ } ) ?) ?;
36+
37+ lua. globals ( ) . set ( "case" , module) ?;
38+
39+ Ok ( ( ) )
40+ }
41+
42+ #[ cfg( test) ]
43+ mod tests {
44+ use super :: * ;
45+
46+ #[ test]
47+ fn lua_cambel ( ) {
48+ let lua = Lua :: new ( ) ;
49+ init ( & lua) . unwrap ( ) ;
50+
51+ lua. exec :: < _ , ( ) > ( r#"
52+ local val = case.new("We are not in the least afraid of ruins")
53+ assert(val:to_camel() == "WeAreNotInTheLeastAfraidOfRuins")
54+ "# , None ) . unwrap ( ) ;
55+ }
56+
57+ #[ test]
58+ fn lua_kebab ( ) {
59+ let lua = Lua :: new ( ) ;
60+ init ( & lua) . unwrap ( ) ;
61+
62+ lua. exec :: < _ , ( ) > ( r#"
63+ local val = case.new("We are going to inherit the earth")
64+ assert(val:to_kebab() == "we-are-going-to-inherit-the-earth")
65+ "# , None ) . unwrap ( ) ;
66+ }
67+
68+ #[ test]
69+ fn lua_mixed ( ) {
70+ let lua = Lua :: new ( ) ;
71+ init ( & lua) . unwrap ( ) ;
72+
73+ lua. exec :: < _ , ( ) > ( r#"
74+ local val = case.new("It is we who built these palaces and cities")
75+ assert(val:to_mixed() == "itIsWeWhoBuiltThesePalacesAndCities")
76+ "# , None ) . unwrap ( ) ;
77+ }
78+
79+ #[ test]
80+ fn lua_shouty ( ) {
81+ let lua = Lua :: new ( ) ;
82+ init ( & lua) . unwrap ( ) ;
83+
84+ lua. exec :: < _ , ( ) > ( r#"
85+ local val = case.new("That world is growing in this minute")
86+ assert(val:to_shouty_snake() == "THAT_WORLD_IS_GROWING_IN_THIS_MINUTE")
87+ "# , None ) . unwrap ( ) ;
88+ }
89+
90+ #[ test]
91+ fn lua_snake ( ) {
92+ let lua = Lua :: new ( ) ;
93+ init ( & lua) . unwrap ( ) ;
94+
95+ lua. exec :: < _ , ( ) > ( r#"
96+ local val = case.new("We carry a new world here, in our hearts")
97+ assert(val:to_snake() == "we_carry_a_new_world_here_in_our_hearts")
98+ "# , None ) . unwrap ( ) ;
99+ }
100+
101+ #[ test]
102+ fn lua_title ( ) {
103+ let lua = Lua :: new ( ) ;
104+ init ( & lua) . unwrap ( ) ;
105+
106+ lua. exec :: < _ , ( ) > ( r#"
107+ local val = case.new("We have always lived in slums and holes in the wall")
108+ assert(val:to_title() == "We Have Always Lived In Slums And Holes In The Wall")
109+ "# , None ) . unwrap ( ) ;
110+ }
111+ }
0 commit comments