xur-std 0.1.6

The standard library of the Xur language
Documentation
//! `std::fs`: files.

use std::fs;
use std::io::{Read, Write};
use std::path::Path;
use std::sync::{Arc, Mutex};

use facet::Facet;
use xur_runtime::{Registry, XurBytes, XurStr, lock};

use crate::io::Error as IoError;

/// An open file, shared by every Xur value referring to it.
#[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: XurStr| {
            fs::read_to_string(&*path).map_err(IoError::from)
        })
        .named_function("read", &["path"], |path: XurStr| {
            fs::read(&*path).map_err(IoError::from)
        })
        .named_function(
            "write_bytes",
            &["path", "data"],
            |path: XurStr, data: XurBytes| {
                fs::write(&*path, lock(&data.0).as_slice()).map_err(IoError::from)
            },
        )
        .named_function(
            "write",
            &["path", "contents"],
            |path: XurStr, contents: XurStr| fs::write(&*path, &*contents).map_err(IoError::from),
        )
        .named_function(
            "append",
            &["path", "contents"],
            |path: XurStr, contents: XurStr| -> Result<(), IoError> {
                let mut file = fs::OpenOptions::new()
                    .append(true)
                    .create(true)
                    .open(&*path)
                    .map_err(IoError::from)?;
                file.write_all(contents.as_bytes()).map_err(IoError::from)
            },
        )
        .named_function("exists", &["path"], |path: XurStr| {
            Path::new(&*path).exists()
        })
        .named_function("remove", &["path"], |path: XurStr| {
            fs::remove_file(&*path).map_err(IoError::from)
        })
        .named_function("create_dir_all", &["path"], |path: XurStr| {
            fs::create_dir_all(&*path).map_err(IoError::from)
        })
        .named_function(
            "read_dir",
            &["path"],
            |path: XurStr| -> Result<Vec<String>, IoError> {
                let mut names = fs::read_dir(&*path)
                    .map_err(IoError::from)?
                    .map(|entry| entry.map(|e| e.file_name().to_string_lossy().into_owned()))
                    .collect::<Result<Vec<_>, _>>()
                    .map_err(IoError::from)?;
                names.sort();
                Ok(names)
            },
        )
        .ty::<File>("File")
        .function("open", |path: XurStr| {
            fs::File::open(&*path).map(File::new).map_err(IoError::from)
        })
        .function("create", |path: XurStr| {
            fs::File::create(&*path)
                .map(File::new)
                .map_err(IoError::from)
        })
        .method("read_to_string", |file: &File| {
            let mut contents = String::new();
            lock(&file.0)
                .read_to_string(&mut contents)
                .map(|_| contents)
                .map_err(IoError::from)
        })
        .method("write", |file: &File, contents: XurStr| {
            lock(&file.0)
                .write_all(contents.as_bytes())
                .map_err(IoError::from)
        });
}