feat: commit codee

This commit is contained in:
mrxiaozhuox committed 2022-07-28 23:07:55 +08:00
1 parent 59acefadc9
commit 3630224659
5 files changed
+49 -6

No files matched your search

+34 -2
View File
@@ -1,4 +1,7 @@
use std::path::PathBuf;
use std::{
fs::{create_dir, create_dir_all},
path::PathBuf, io::{Read, Write},
};
use mlua::UserData;
@@ -17,5 +20,34 @@ impl UserData for PluginFileSystem {
let path = PathBuf::from(path);
Ok(path.is_file())
});
methods.add_function("create_dir", |_, args: (String, bool)| {
let path = args.0;
let recursive = args.1;
let path = PathBuf::from(path);
if !path.exists() {
let v = if recursive {
create_dir_all(path)
} else {
create_dir(path)
};
return Ok(v.is_ok());
}
Ok(true)
});
methods.add_function("file_get_content", |_, path: String| {
let path = PathBuf::from(path);
let mut file = std::fs::File::open(path)?;
let mut buffer = String::new();
file.read_to_string(&mut buffer)?;
Ok(buffer)
});
methods.add_function("file_set_content", |_, args: (String, String)| {
let path = args.0;
let content = args.1;
let path = PathBuf::from(path);
let mut file = std::fs::File::create(path)?;
file.write_all(content.as_bytes())?;
Ok(())
});
}
}
}