Skip to content

Commit

Permalink
feat: new fs.create() Lua API (#2068)
Browse files Browse the repository at this point in the history
  • Loading branch information
hankertrix authored Dec 20, 2024
1 parent 87bfd8e commit f017699
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions yazi-plugin/src/fs/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn compose(lua: &Lua) -> mlua::Result<Table> {
b"cwd" => cwd(lua)?,
b"cha" => cha(lua)?,
b"write" => write(lua)?,
b"create" => create(lua)?,
b"remove" => remove(lua)?,
b"read_dir" => read_dir(lua)?,
b"unique_name" => unique_name(lua)?,
Expand Down Expand Up @@ -53,7 +54,22 @@ fn cha(lua: &Lua) -> mlua::Result<Function> {
fn write(lua: &Lua) -> mlua::Result<Function> {
lua.create_async_function(|lua, (url, data): (UrlRef, mlua::String)| async move {
match fs::write(&*url, data.as_bytes()).await {
Ok(_) => (true, Value::Nil).into_lua_multi(&lua),
Ok(()) => (true, Value::Nil).into_lua_multi(&lua),
Err(e) => (false, Error::Io(e)).into_lua_multi(&lua),
}
})
}

fn create(lua: &Lua) -> mlua::Result<Function> {
lua.create_async_function(|lua, (type_, url): (mlua::String, UrlRef)| async move {
let result = match type_.as_bytes().as_ref() {
b"dir" => fs::create_dir(&*url).await,
b"dir_all" => fs::create_dir_all(&*url).await,
_ => Err("Creation type must be 'dir' or 'dir_all'".into_lua_err())?,
};

match result {
Ok(()) => (true, Value::Nil).into_lua_multi(&lua),
Err(e) => (false, Error::Io(e)).into_lua_multi(&lua),
}
})
Expand All @@ -70,7 +86,7 @@ fn remove(lua: &Lua) -> mlua::Result<Function> {
};

match result {
Ok(_) => (true, Value::Nil).into_lua_multi(&lua),
Ok(()) => (true, Value::Nil).into_lua_multi(&lua),
Err(e) => (false, Error::Io(e)).into_lua_multi(&lua),
}
})
Expand Down

0 comments on commit f017699

Please sign in to comment.