simple_fs/
file.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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 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))
}