From 572aff40fe32c6ec09fa6eb04b884c66868773d0 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 24 Oct 2024 02:05:25 +0300 Subject: [PATCH 1/8] chore(deps): Update mlua to v0.10 series --- Cargo.lock | 10 +++++----- Cargo.toml | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cb42bc4..2b8e7b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1103,15 +1103,15 @@ dependencies = [ [[package]] name = "mlua" -version = "0.9.9" +version = "0.10.0-rc.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d111deb18a9c9bd33e1541309f4742523bfab01d276bfa9a27519f6de9c11dc7" +checksum = "7928e85ce39d4ea4d11dc850fde2acb503fc114a76f53e8606dd9ed237a491e9" dependencies = [ "bstr", "mlua-sys", "mlua_derive", "num-traits", - "once_cell", + "parking_lot", "rustc-hash", ] @@ -1128,9 +1128,9 @@ dependencies = [ [[package]] name = "mlua_derive" -version = "0.9.3" +version = "0.10.0-rc.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09697a6cec88e7f58a02c7ab5c18c611c6907c8654613df9cc0192658a4fb859" +checksum = "e110f0addea6b8d2f2378717e94f63b36a74274760b4895943b3a5650fa7513e" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 55cd855..9a6cc26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,7 +55,7 @@ unicode_titlecase = "2.3" features = ["derive", "wrap_help"] [dependencies.mlua] - version = "0.9" + version = "0.10.0-rc.1" optional = true features = ["module"] From 5fd3612b04791a445d6b4cbbdfd1434a355f041f Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 24 Oct 2024 02:24:45 +0300 Subject: [PATCH 2/8] refactor(lua): Simplify lifetimes with new API --- src/lua.rs | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index 5707c26..5bb6554 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -25,10 +25,10 @@ fn decasify(lua: &Lua) -> LuaResult { Ok(exports) } -fn case<'a>( - lua: &'a Lua, - (input, case, locale, style): (LuaString<'a>, LuaValue<'a>, LuaValue<'a>, LuaValue<'a>), -) -> LuaResult> { +fn case( + lua: &Lua, + (input, case, locale, style): (LuaString, LuaValue, LuaValue, LuaValue), +) -> LuaResult { let input = input.to_string_lossy(); let case: Case = match case { LuaValue::String(s) => s.to_string_lossy().parse().unwrap_or(Case::Title), @@ -49,10 +49,10 @@ fn case<'a>( lua.create_string(output) } -fn titlecase<'a>( - lua: &'a Lua, - (input, locale, style): (LuaString<'a>, LuaValue<'a>, LuaValue<'a>), -) -> LuaResult> { +fn titlecase( + lua: &Lua, + (input, locale, style): (LuaString, LuaValue, LuaValue), +) -> LuaResult { let input = input.to_string_lossy(); let locale: Locale = match locale { LuaValue::String(s) => s.to_string_lossy().parse().unwrap_or(Locale::EN), @@ -69,10 +69,7 @@ fn titlecase<'a>( lua.create_string(output) } -fn lowercase<'a>( - lua: &'a Lua, - (input, locale): (LuaString<'a>, LuaValue<'a>), -) -> LuaResult> { +fn lowercase(lua: &Lua, (input, locale): (LuaString, LuaValue)) -> LuaResult { let input = input.to_string_lossy(); let locale: Locale = match locale { LuaValue::String(s) => s.to_string_lossy().parse().unwrap_or(Locale::EN), @@ -82,10 +79,7 @@ fn lowercase<'a>( lua.create_string(output) } -fn uppercase<'a>( - lua: &'a Lua, - (input, locale): (LuaString<'a>, LuaValue<'a>), -) -> LuaResult> { +fn uppercase(lua: &Lua, (input, locale): (LuaString, LuaValue)) -> LuaResult { let input = input.to_string_lossy(); let locale: Locale = match locale { LuaValue::String(s) => s.to_string_lossy().parse().unwrap_or(Locale::EN), @@ -95,10 +89,7 @@ fn uppercase<'a>( lua.create_string(output) } -fn sentencecase<'a>( - lua: &'a Lua, - (input, locale): (LuaString<'a>, LuaValue<'a>), -) -> LuaResult> { +fn sentencecase(lua: &Lua, (input, locale): (LuaString, LuaValue)) -> LuaResult { let input = input.to_string_lossy(); let locale: Locale = match locale { LuaValue::String(s) => s.to_string_lossy().parse().unwrap_or(Locale::EN), From 969af573bd80173a74ad2749f32ec668f65f87de Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 24 Oct 2024 02:47:31 +0300 Subject: [PATCH 3/8] chore(deps): Depend on anyhow for error type handling --- Cargo.lock | 1 + Cargo.toml | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 2b8e7b9..e93b5b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1107,6 +1107,7 @@ version = "0.10.0-rc.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7928e85ce39d4ea4d11dc850fde2acb503fc114a76f53e8606dd9ed237a491e9" dependencies = [ + "anyhow", "bstr", "mlua-sys", "mlua_derive", diff --git a/Cargo.toml b/Cargo.toml index 9a6cc26..864da44 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,9 @@ strum = "0.26" strum_macros = "0.26" unicode_titlecase = "2.3" + [dependencies.anyhow] + version = "1.0" + [dependencies.clap] version = "4.5" optional = true @@ -57,7 +60,7 @@ unicode_titlecase = "2.3" [dependencies.mlua] version = "0.10.0-rc.1" optional = true - features = ["module"] + features = ["module", "anyhow"] [dependencies.pyo3] version = "0.22" From 66a663cee2bec1a9430022963d7527431ceb0011 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 24 Oct 2024 02:47:52 +0300 Subject: [PATCH 4/8] refactor: Use anyhow for error types --- src/content.rs | 4 ++-- src/types.rs | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/content.rs b/src/content.rs index 653b575..2663e10 100644 --- a/src/content.rs +++ b/src/content.rs @@ -3,7 +3,7 @@ use crate::types::Result; use regex::Regex; -use std::{borrow::Cow, error, fmt, fmt::Display, str::FromStr}; +use std::{borrow::Cow, fmt, fmt::Display, str::FromStr}; #[derive(Clone, Debug, PartialEq)] #[non_exhaustive] @@ -56,7 +56,7 @@ impl From<&Cow<'_, str>> for Chunk { } impl FromStr for Chunk { - type Err = Box; + type Err = anyhow::Error; fn from_str(s: &str) -> Result { Ok(split_chunk(s)) } diff --git a/src/types.rs b/src/types.rs index c819a90..a20ba5e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: © 2023 Caleb Maclennan // SPDX-License-Identifier: LGPL-3.0-only -use std::{error, fmt, fmt::Display, result, str::FromStr}; +use std::{error, fmt, fmt::Display, str::FromStr}; use strum_macros::{Display, VariantNames}; #[cfg(feature = "pythonmodule")] @@ -10,7 +10,7 @@ use pyo3::prelude::*; #[cfg(feature = "wasm")] use wasm_bindgen::prelude::*; -pub type Result = result::Result>; +pub type Result = anyhow::Result; #[derive(Debug)] pub struct Error(pub String); @@ -68,12 +68,12 @@ pub enum StyleGuide { } impl FromStr for Locale { - type Err = Box; - fn from_str(s: &str) -> Result { + type Err = anyhow::Error; + fn from_str(s: &str) -> crate::Result { match s.to_ascii_lowercase().as_str() { "en" | "English" | "en_en" => Ok(Locale::EN), "tr" | "Turkish" | "tr_tr" | "türkçe" => Ok(Locale::TR), - _ => Err(Box::new(Error("Invalid input language".into()))), + _ => Err(anyhow::Error::new(Error("Invalid input language".into()))), } } } @@ -97,14 +97,14 @@ impl From<&String> for Locale { } impl FromStr for Case { - type Err = Box; - fn from_str(s: &str) -> Result { + type Err = anyhow::Error; + fn from_str(s: &str) -> crate::Result { match s.to_ascii_lowercase().as_str().trim_end_matches("case") { "lower" => Ok(Case::Lower), "sentence" => Ok(Case::Sentence), "title" => Ok(Case::Title), "upper" => Ok(Case::Upper), - _ => Err(Box::new(Error("Unknown target case".into()))), + _ => Err(anyhow::Error::new(Error("Unknown target case".into()))), } } } @@ -128,8 +128,8 @@ impl From<&String> for Case { } impl FromStr for StyleGuide { - type Err = Box; - fn from_str(s: &str) -> Result { + type Err = anyhow::Error; + fn from_str(s: &str) -> crate::Result { match s.to_ascii_lowercase().as_str() { "daringfireball" | "gruber" | "fireball" => Ok(StyleGuide::DaringFireball), "associatedpress" | "ap" => Ok(StyleGuide::AssociatedPress), @@ -137,7 +137,7 @@ impl FromStr for StyleGuide { "default" | "languagedefault" | "language" | "none" | "" => { Ok(StyleGuide::LanguageDefault) } - _ => Err(Box::new(Error("Invalid style guide".into()))), + _ => Err(anyhow::Error::new(Error("Invalid style guide".into()))), } } } From 4a66f5f2edf2f29aecdfeb757bead4aab26488f8 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 24 Oct 2024 03:25:17 +0300 Subject: [PATCH 5/8] feat(lua): Implement `FromLua` for relevant crate types --- src/content.rs | 2 +- src/lua.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/content.rs b/src/content.rs index 2663e10..e652414 100644 --- a/src/content.rs +++ b/src/content.rs @@ -12,7 +12,7 @@ pub enum Segment { Word(String), } -#[derive(Debug, Clone)] +#[derive(Clone, Debug)] #[non_exhaustive] pub struct Chunk { pub segments: Vec, diff --git a/src/lua.rs b/src/lua.rs index 5bb6554..7e225bf 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -6,6 +6,45 @@ use mlua::prelude::*; pub use crate::types::{Case, Locale, Result, StyleGuide}; +impl FromLua for Chunk { + fn from_lua(value: LuaValue, _: &Lua) -> LuaResult { + match value { + LuaValue::String(s) => Ok(s.to_string_lossy().into()), + _ => Ok("".into()), + } + } +} + +impl FromLua for Locale { + fn from_lua(value: LuaValue, _: &Lua) -> LuaResult { + match value { + LuaValue::String(s) => Ok(s.to_string_lossy().into()), + LuaValue::Nil => Ok(Self::default()), + _ => unimplemented!(), + } + } +} + +impl FromLua for Case { + fn from_lua(value: LuaValue, _: &Lua) -> LuaResult { + match value { + LuaValue::String(s) => Ok(s.to_string_lossy().into()), + LuaValue::Nil => Ok(Self::default()), + _ => unimplemented!(), + } + } +} + +impl FromLua for StyleGuide { + fn from_lua(value: LuaValue, _: &Lua) -> LuaResult { + match value { + LuaValue::String(s) => Ok(s.to_string_lossy().into()), + LuaValue::Nil => Ok(Self::default()), + _ => unimplemented!(), + } + } +} + #[mlua::lua_module] fn decasify(lua: &Lua) -> LuaResult { let exports = lua.create_table().unwrap(); From 1282b543c9d9d3db91702059e928dedb0e9be3bb Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 24 Oct 2024 20:06:45 +0300 Subject: [PATCH 6/8] refactor(lua): Use `LuaFunction::wrap()` and `Into<>` traits to avoid wrapper functions --- src/lua.rs | 108 +++++++++++------------------------------------------ 1 file changed, 22 insertions(+), 86 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index 7e225bf..c068e30 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -47,93 +47,29 @@ impl FromLua for StyleGuide { #[mlua::lua_module] fn decasify(lua: &Lua) -> LuaResult { - 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 { - 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 { - 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 { - 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 { - 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 { - 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) -} From b658d0c9ad8fc7e677cad3f6f8f6966c923f8888 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Fri, 25 Oct 2024 11:10:02 +0300 Subject: [PATCH 7/8] style(lua): Move module exports to top of file --- src/lua.rs | 58 +++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index c068e30..21dc4c7 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -6,6 +6,35 @@ use mlua::prelude::*; pub use crate::types::{Case, Locale, Result, StyleGuide}; +#[mlua::lua_module] +fn decasify(lua: &Lua) -> LuaResult { + 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)?; + Ok(exports) +} + impl FromLua for Chunk { fn from_lua(value: LuaValue, _: &Lua) -> LuaResult { match value { @@ -44,32 +73,3 @@ impl FromLua for StyleGuide { } } } - -#[mlua::lua_module] -fn decasify(lua: &Lua) -> LuaResult { - 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)?; - Ok(exports) -} From 369d139d78807b9c26c15ddc0c0ead4c185e6863 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Sat, 26 Oct 2024 00:41:40 +0300 Subject: [PATCH 8/8] chore(deps): Update mlua pre-release to stable --- Cargo.lock | 19 +++++++++++++------ Cargo.toml | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e93b5b8..c6c2c1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -383,6 +383,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + [[package]] name = "errno" version = "0.3.9" @@ -1103,12 +1109,13 @@ dependencies = [ [[package]] name = "mlua" -version = "0.10.0-rc.1" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7928e85ce39d4ea4d11dc850fde2acb503fc114a76f53e8606dd9ed237a491e9" +checksum = "0f6ddbd668297c46be4bdea6c599dcc1f001a129586272d53170b7ac0a62961e" dependencies = [ "anyhow", "bstr", + "either", "mlua-sys", "mlua_derive", "num-traits", @@ -1118,9 +1125,9 @@ dependencies = [ [[package]] name = "mlua-sys" -version = "0.6.3" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebe026d6bd1583a9cf9080e189030ddaea7e6f5f0deb366a8e26f8a26c4135b8" +checksum = "e9eebac25c35a13285456c88ee2fde93d9aee8bcfdaf03f9d6d12be3391351ec" dependencies = [ "cc", "cfg-if", @@ -1129,9 +1136,9 @@ dependencies = [ [[package]] name = "mlua_derive" -version = "0.10.0-rc.1" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e110f0addea6b8d2f2378717e94f63b36a74274760b4895943b3a5650fa7513e" +checksum = "2cfc5faa2e0d044b3f5f0879be2920e0a711c97744c42cf1c295cb183668933e" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 864da44..7308be3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,7 +58,7 @@ unicode_titlecase = "2.3" features = ["derive", "wrap_help"] [dependencies.mlua] - version = "0.10.0-rc.1" + version = "0.10.0" optional = true features = ["module", "anyhow"]