Skip to content

Commit db94330

Browse files
committed
Add IntoLua/FromLua for OsString/OsStr and PathBuf/Path
1 parent 640d276 commit db94330

File tree

2 files changed

+111
-3
lines changed

2 files changed

+111
-3
lines changed

src/conversion.rs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::borrow::Cow;
22
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
3-
use std::ffi::{CStr, CString};
3+
use std::ffi::{CStr, CString, OsStr, OsString};
44
use std::hash::{BuildHasher, Hash};
55
use std::os::raw::c_int;
6+
use std::path::{Path, PathBuf};
67
use std::string::String as StdString;
78
use std::{slice, str};
89

9-
use bstr::{BStr, BString};
10+
use bstr::{BStr, BString, ByteVec};
1011
use num_traits::cast;
1112

1213
use crate::error::{Error, Result};
@@ -563,6 +564,63 @@ impl IntoLua for &BStr {
563564
}
564565
}
565566

567+
impl IntoLua for OsString {
568+
#[inline]
569+
fn into_lua(self, lua: &Lua) -> Result<Value> {
570+
BString::from(
571+
Vec::from_os_string(self).map_err(|val| Error::ToLuaConversionError {
572+
from: "OsString",
573+
to: "string",
574+
message: Some(format!("Invalid encoding: {:?}", val)),
575+
})?,
576+
)
577+
.into_lua(lua)
578+
}
579+
}
580+
581+
impl FromLua for OsString {
582+
#[inline]
583+
fn from_lua(value: Value, lua: &Lua) -> Result<Self> {
584+
let ty = value.type_name();
585+
let bs = BString::from_lua(value, lua)?;
586+
Vec::from(bs)
587+
.into_os_string()
588+
.map_err(|err| Error::FromLuaConversionError {
589+
from: ty,
590+
to: "OsString",
591+
message: Some(format!("{}", err)),
592+
})
593+
}
594+
}
595+
596+
impl IntoLua for &OsStr {
597+
#[inline]
598+
fn into_lua(self, lua: &Lua) -> Result<Value> {
599+
OsString::from(self).into_lua(lua)
600+
}
601+
}
602+
603+
impl IntoLua for PathBuf {
604+
#[inline]
605+
fn into_lua(self, lua: &Lua) -> Result<Value> {
606+
self.into_os_string().into_lua(lua)
607+
}
608+
}
609+
610+
impl FromLua for PathBuf {
611+
#[inline]
612+
fn from_lua(value: Value, lua: &Lua) -> Result<Self> {
613+
OsString::from_lua(value, lua).map(PathBuf::from)
614+
}
615+
}
616+
617+
impl IntoLua for &Path {
618+
#[inline]
619+
fn into_lua(self, lua: &Lua) -> Result<Value> {
620+
PathBuf::from(self).into_lua(lua)
621+
}
622+
}
623+
566624
#[inline]
567625
unsafe fn push_bytes_into_stack<T>(this: T, lua: &RawLua) -> Result<()>
568626
where

tests/conversion.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
3-
use std::ffi::{CStr, CString};
3+
use std::ffi::{CStr, CString, OsStr, OsString};
4+
use std::path::{Path, PathBuf};
45

56
use bstr::BString;
67
use maplit::{btreemap, btreeset, hashmap, hashset};
@@ -396,6 +397,55 @@ fn test_bstring_from_lua_buffer() -> Result<()> {
396397
Ok(())
397398
}
398399

400+
#[test]
401+
fn test_osstring_into_from_lua() -> Result<()> {
402+
let lua = Lua::new();
403+
404+
let s = OsString::from("hello, world");
405+
406+
let v = lua.pack(s.as_os_str())?;
407+
assert!(v.is_string());
408+
assert_eq!(v.as_str().unwrap(), "hello, world");
409+
410+
let v = lua.pack(s)?;
411+
assert!(v.is_string());
412+
assert_eq!(v.as_str().unwrap(), "hello, world");
413+
414+
let s = lua.create_string("hello, world")?;
415+
let bstr = lua.unpack::<OsString>(Value::String(s))?;
416+
assert_eq!(bstr, "hello, world");
417+
418+
let bstr = lua.unpack::<OsString>(Value::Integer(123))?;
419+
assert_eq!(bstr, "123");
420+
421+
let bstr = lua.unpack::<OsString>(Value::Number(-123.55))?;
422+
assert_eq!(bstr, "-123.55");
423+
424+
Ok(())
425+
}
426+
427+
#[test]
428+
fn test_pathbuf_into_from_lua() -> Result<()> {
429+
let lua = Lua::new();
430+
431+
let pb = PathBuf::from(env!("CARGO_TARGET_TMPDIR"));
432+
let pb_str = pb.to_str().unwrap();
433+
434+
let v = lua.pack(pb.as_path())?;
435+
assert!(v.is_string());
436+
assert_eq!(v.as_str().unwrap(), pb_str);
437+
438+
let v = lua.pack(pb.clone())?;
439+
assert!(v.is_string());
440+
assert_eq!(v.as_str().unwrap(), pb_str);
441+
442+
let s = lua.create_string(pb_str)?;
443+
let bstr = lua.unpack::<PathBuf>(Value::String(s))?;
444+
assert_eq!(bstr, pb);
445+
446+
Ok(())
447+
}
448+
399449
#[test]
400450
fn test_option_into_from_lua() -> Result<()> {
401451
let lua = Lua::new();

0 commit comments

Comments
 (0)