use std::{fs::File, io::{BufReader, BufWriter, Read, Write}};
pub fn read_file(path: &str) -> Result<String, String> {
let f = match std::fs::File::open(path) {
Ok(f) => f,
Err(e) => {
return Err(e.to_string())
}
};
let mut reader = BufReader::new(f);
let mut buf = String::new();
match reader.read_to_string(&mut buf) {
Ok(_) => Ok(buf),
Err(e) => Err(e.to_string())
}
}
pub fn write_file(path: &str, content: &str) -> Result<(), String> {
let f = match File::open(path) {
Ok(f) => f,
Err(e) => return Err(e.to_string())
};
let mut writer = BufWriter::new(f);
match writer.write_all(content.as_bytes()) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string())
}
}
pub fn exchange_file_content(path_a: &str, path_b: &str) -> Result<(), String> {
let a = match read_file(path_a) {
Ok(value) => value,
Err(e) => return Err(e)
};
let b = match read_file(path_b) {
Ok(value) => value,
Err(e) => return Err(e)
};
match write_file(path_a, &b) {
Ok(_) => (),
Err(e) => return Err(e)
}
match write_file(path_b, &a) {
Ok(_) => (),
Err(e) => return Err(e)
}
Ok(())
}