Skip to content

Commit

Permalink
refactor(lua): Use LuaFunction::wrap() and Into<> traits to avoid…
Browse files Browse the repository at this point in the history
… wrapper functions
  • Loading branch information
alerque committed Oct 24, 2024
1 parent 4a66f5f commit 1282b54
Showing 1 changed file with 22 additions and 86 deletions.
108 changes: 22 additions & 86 deletions src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,93 +47,29 @@ impl FromLua for StyleGuide {

#[mlua::lua_module]
fn decasify(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table().unwrap();
let case = lua.create_function(case)?;
exports.set("case", case).unwrap();
let titlecase = lua.create_function(titlecase)?;
exports.set("titlecase", titlecase).unwrap();
let lowercase = lua.create_function(lowercase)?;
exports.set("lowercase", lowercase).unwrap();
let uppercase = lua.create_function(uppercase)?;
exports.set("uppercase", uppercase).unwrap();
let sentencecase = lua.create_function(sentencecase)?;
exports.set("sentencecase", sentencecase).unwrap();
let exports = lua.create_table()?;
exports.set(
"case",
LuaFunction::wrap_raw::<_, (Chunk, Case, Locale, StyleGuide)>(to_case),
)?;
exports.set(
"titlecase",
LuaFunction::wrap_raw::<_, (Chunk, Locale, StyleGuide)>(to_titlecase),
)?;
exports.set(
"lowercase",
LuaFunction::wrap_raw::<_, (Chunk, Locale)>(to_lowercase),
)?;
exports.set(
"uppercase",
LuaFunction::wrap_raw::<_, (Chunk, Locale)>(to_uppercase),
)?;
exports.set(
"sentencecase",
LuaFunction::wrap_raw::<_, (Chunk, Locale)>(to_sentencecase),
)?;
let version = option_env!("VERGEN_GIT_DESCRIBE").unwrap_or_else(|| env!("CARGO_PKG_VERSION"));
let version = lua.create_string(version)?;
exports.set("version", version).unwrap();
exports.set("version", version)?;
Ok(exports)
}

fn case(
lua: &Lua,
(input, case, locale, style): (LuaString, LuaValue, LuaValue, LuaValue),
) -> LuaResult<LuaString> {
let input = input.to_string_lossy();
let case: Case = match case {
LuaValue::String(s) => s.to_string_lossy().parse().unwrap_or(Case::Title),
_ => Case::Title,
};
let locale: Locale = match locale {
LuaValue::String(s) => s.to_string_lossy().parse().unwrap_or(Locale::EN),
_ => Locale::EN,
};
let style: StyleGuide = match style {
LuaValue::String(s) => s
.to_string_lossy()
.parse()
.unwrap_or(StyleGuide::LanguageDefault),
_ => StyleGuide::LanguageDefault,
};
let output = to_case(&input, case, locale, style);
lua.create_string(output)
}

fn titlecase(
lua: &Lua,
(input, locale, style): (LuaString, LuaValue, LuaValue),
) -> LuaResult<LuaString> {
let input = input.to_string_lossy();
let locale: Locale = match locale {
LuaValue::String(s) => s.to_string_lossy().parse().unwrap_or(Locale::EN),
_ => Locale::EN,
};
let style: StyleGuide = match style {
LuaValue::String(s) => s
.to_string_lossy()
.parse()
.unwrap_or(StyleGuide::LanguageDefault),
_ => StyleGuide::LanguageDefault,
};
let output = to_titlecase(&input, locale, style);
lua.create_string(output)
}

fn lowercase(lua: &Lua, (input, locale): (LuaString, LuaValue)) -> LuaResult<LuaString> {
let input = input.to_string_lossy();
let locale: Locale = match locale {
LuaValue::String(s) => s.to_string_lossy().parse().unwrap_or(Locale::EN),
_ => Locale::EN,
};
let output = to_lowercase(&input, locale);
lua.create_string(output)
}

fn uppercase(lua: &Lua, (input, locale): (LuaString, LuaValue)) -> LuaResult<LuaString> {
let input = input.to_string_lossy();
let locale: Locale = match locale {
LuaValue::String(s) => s.to_string_lossy().parse().unwrap_or(Locale::EN),
_ => Locale::EN,
};
let output = to_uppercase(&input, locale);
lua.create_string(output)
}

fn sentencecase(lua: &Lua, (input, locale): (LuaString, LuaValue)) -> LuaResult<LuaString> {
let input = input.to_string_lossy();
let locale: Locale = match locale {
LuaValue::String(s) => s.to_string_lossy().parse().unwrap_or(Locale::EN),
_ => Locale::EN,
};
let output = to_sentencecase(&input, locale);
lua.create_string(output)
}

0 comments on commit 1282b54

Please sign in to comment.