use std::fs;
use std::io::{Read, Write};
use std::path::Path;
use std::sync::{Arc, Mutex};
use facet::Facet;
use xur_runtime::{Registry, lock};
use crate::error_text;
#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct File(Arc<Mutex<fs::File>>);
impl File {
fn new(file: fs::File) -> Self {
Self(Arc::new(Mutex::new(file)))
}
}
pub(crate) fn register(registry: &Registry) {
registry
.module("std::fs")
.named_function("read_to_string", &["path"], |path: String| {
fs::read_to_string(path).map_err(error_text)
})
.named_function("read", &["path"], |path: String| fs::read(path).map_err(error_text))
.named_function("write_bytes", &["path", "data"], |path: String, data: Vec<u8>| {
fs::write(path, data).map_err(error_text)
})
.named_function("write", &["path", "contents"], |path: String, contents: String| {
fs::write(path, contents).map_err(error_text)
})
.named_function(
"append", &["path", "contents"],
|path: String, contents: String| -> Result<(), String> {
let mut file = fs::OpenOptions::new()
.append(true)
.create(true)
.open(path)
.map_err(error_text)?;
file.write_all(contents.as_bytes()).map_err(error_text)
},
)
.named_function("exists", &["path"], |path: String| Path::new(&path).exists())
.named_function("remove", &["path"], |path: String| {
fs::remove_file(path).map_err(error_text)
})
.named_function("create_dir_all", &["path"], |path: String| {
fs::create_dir_all(path).map_err(error_text)
})
.named_function("read_dir", &["path"], |path: String| -> Result<Vec<String>, String> {
let mut names = fs::read_dir(path)
.map_err(error_text)?
.map(|entry| entry.map(|e| e.file_name().to_string_lossy().into_owned()))
.collect::<Result<Vec<_>, _>>()
.map_err(error_text)?;
names.sort();
Ok(names)
})
.ty::<File>("File")
.function("open", |path: String| {
fs::File::open(path).map(File::new).map_err(error_text)
})
.function("create", |path: String| {
fs::File::create(path).map(File::new).map_err(error_text)
})
.method("read_to_string", |file: &File| {
let mut contents = String::new();
lock(&file.0)
.read_to_string(&mut contents)
.map(|_| contents)
.map_err(error_text)
})
.method("write", |file: &File, contents: String| {
lock(&file.0)
.write_all(contents.as_bytes())
.map_err(error_text)
});
}