simple-fs 0.12.1

Simple and convenient API for File System access
Documentation
use crate::{Error, Result};
use std::fs::{self, File};
use std::io::{BufReader, BufWriter};
use std::path::Path;

pub fn create_file(file_path: impl AsRef<Path>) -> Result<File> {
	let file_path = file_path.as_ref();
	File::create(file_path).map_err(|e| Error::FileCantCreate((file_path, e).into()))
}

pub fn read_to_string(file_path: impl AsRef<Path>) -> Result<String> {
	let file_path = file_path.as_ref();

	if !file_path.is_file() {
		return Err(Error::FileNotFound(file_path.to_string_lossy().to_string()));
	}

	let content = fs::read_to_string(file_path).map_err(|e| Error::FileCantRead((file_path, e).into()))?;

	Ok(content)
}

pub fn open_file(path: impl AsRef<Path>) -> Result<File> {
	let path = path.as_ref();
	let f = File::open(path).map_err(|e| Error::FileCantOpen((path, e).into()))?;
	Ok(f)
}

pub fn get_buf_reader(file: impl AsRef<Path>) -> Result<BufReader<File>> {
	let file = file.as_ref();

	let file = File::open(file).map_err(|e| Error::FileCantOpen((file, e).into()))?;

	Ok(BufReader::new(file))
}

pub fn get_buf_writer(file_path: impl AsRef<Path>) -> Result<BufWriter<File>> {
	let file_path = file_path.as_ref();

	let file = create_file(file_path)?;

	Ok(BufWriter::new(file))
}