|
| 1 | +use rlua::prelude::*; |
| 2 | +use std::path; |
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +pub struct LuaPath(path::PathBuf); |
| 6 | +//TODO: Clean up metadata for `Path::metadata` (Internal #1b8b1ed373cd1901b2) |
| 7 | +//pub struct LuaMetadata(Metadata); |
| 8 | + |
| 9 | +impl LuaUserData for LuaPath { |
| 10 | + fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { |
| 11 | + methods.add_method("file_stem", |_, this: &LuaPath, _:() |{ |
| 12 | + Ok(this.0.file_stem().map(|p| p.to_str().map(|s| s.to_string()))) |
| 13 | + }); |
| 14 | + methods.add_method("file_name", |_, this: &LuaPath, _:() |{ |
| 15 | + Ok(this.0.file_name().map(|p| p.to_str().map(|s| s.to_string()))) |
| 16 | + }); |
| 17 | + methods.add_method("ext", |_, this: &LuaPath, _:() |{ |
| 18 | + Ok(this.0.extension().map(|p| p.to_str().map(|s| s.to_string()))) |
| 19 | + }); |
| 20 | + methods.add_method("exists", |_, this: &LuaPath, _:() |{ |
| 21 | + Ok(this.0.exists()) |
| 22 | + }); |
| 23 | + methods.add_method("is_dir", |_, this: &LuaPath, _:() |{ |
| 24 | + Ok(this.0.is_dir()) |
| 25 | + }); |
| 26 | + methods.add_method("is_file", |_, this: &LuaPath, _:() |{ |
| 27 | + Ok(this.0.is_file()) |
| 28 | + }); |
| 29 | + methods.add_method("is_relative", |_, this: &LuaPath, _:() |{ |
| 30 | + Ok(this.0.is_relative()) |
| 31 | + }); |
| 32 | + methods.add_method("is_absolute", |_, this: &LuaPath, _:() |{ |
| 33 | + Ok(this.0.is_absolute()) |
| 34 | + }); |
| 35 | + methods.add_method("has_root", |_, this: &LuaPath, _:() |{ |
| 36 | + Ok(this.0.has_root()) |
| 37 | + }); |
| 38 | + methods.add_method("parent", |_, this: &LuaPath, _:() |{ |
| 39 | + Ok(this.0.parent().map(|p| LuaPath(p.to_path_buf()))) |
| 40 | + }); |
| 41 | + methods.add_method_mut("push", |_, this: &mut LuaPath, val: String |{ |
| 42 | + this.0.push(&val); |
| 43 | + Ok(()) |
| 44 | + }); |
| 45 | + methods.add_method("join", |_, this: &LuaPath, path: String |{ |
| 46 | + Ok(LuaPath(this.0.join(path))) |
| 47 | + }); |
| 48 | + methods.add_method("read_dir", |lua, this: &LuaPath, _: ()| { |
| 49 | + match this.0.read_dir() { |
| 50 | + Ok(iter) => { |
| 51 | + let mut arc_iter = Arc::new(Some(iter)); |
| 52 | + let mut f = move |_, _: ()| { |
| 53 | + let result = match Arc::get_mut(&mut arc_iter).expect("entries iterator is mutably borrowed") { |
| 54 | + Some(iter) => match iter.next() { |
| 55 | + Some(Ok(entry)) => Some(entry.file_name().into_string().unwrap()), |
| 56 | + _ => None |
| 57 | + }, |
| 58 | + None => None |
| 59 | + }; |
| 60 | + if result.is_none() { *Arc::get_mut(&mut arc_iter).unwrap() = None; } |
| 61 | + Ok(result) |
| 62 | + }; |
| 63 | + Ok(lua.create_function_mut(f)?) |
| 64 | + }, Err(err) => Err(LuaError::ExternalError(Arc::new(::failure::Error::from_boxed_compat(Box::new(err))))) |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +pub fn init(lua: &Lua) -> ::Result<()> { |
| 72 | + let module = lua.create_table()?; |
| 73 | + |
| 74 | + module.set("empty", lua.create_function( |_, _: ()| { |
| 75 | + Ok(LuaPath(path::PathBuf::new())) |
| 76 | + })? )?; |
| 77 | + |
| 78 | + module.set("new", lua.create_function( |_, path: String| { |
| 79 | + Ok(LuaPath(path::Path::new(&path).to_path_buf())) |
| 80 | + })? )?; |
| 81 | + |
| 82 | + lua.globals().set("path", module)?; |
| 83 | + |
| 84 | + Ok(()) |
| 85 | +} |
0 commit comments