Skip to content

Commit

Permalink
[NFY] impl FromLua for out types
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Oct 24, 2024
1 parent 65d29fe commit 14688e6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum Segment {
Word(String),
}

#[derive(Debug, Clone)]
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Chunk {
pub segments: Vec<Segment>,
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ pub fn to_case(
}
}

pub fn testf(_chunk: impl Into<Chunk>) -> Result<String> {
Ok("some pseudo return data".into())
}

/// Convert a string to title case following typesetting conventions for a target locale
pub fn to_titlecase(
chunk: impl Into<Chunk>,
Expand Down
10 changes: 10 additions & 0 deletions src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ use mlua::prelude::*;

pub use crate::types::{Case, Locale, Result, StyleGuide};

impl FromLua for Chunk {
fn from_lua(value: LuaValue, _: &Lua) -> LuaResult<Self> {
match value {
LuaValue::String(s) => Ok(s.to_string_lossy().into()),
_ => todo!(),
}
}
}

#[mlua::lua_module]
fn decasify(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table().unwrap();
Expand All @@ -22,6 +31,7 @@ fn decasify(lua: &Lua) -> LuaResult<LuaTable> {
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("testf", LuaFunction::wrap_raw::<_, (Chunk,)>(testf))?;
Ok(exports)
}

Expand Down

0 comments on commit 14688e6

Please sign in to comment.