use crate::types::err::{Error, ErrorCode};
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::env;
use std::fs::{self, OpenOptions};
use std::io::{BufReader, BufWriter};
use std::path::{Path, PathBuf};
pub fn read_binary_file<T: DeserializeOwned>(
path: impl AsRef<Path>,
) -> Result<T, Error> {
let file = OpenOptions::new().read(true).open(path)?;
let reader = BufReader::new(file);
let value = bincode::deserialize_from(reader)?;
Ok(value)
}
pub fn write_binary_file<T: Serialize>(
path: impl AsRef<Path>,
data: &T,
) -> Result<(), Error> {
let file_name = parse_file_name(&path)?;
let tmp_dir = get_tmp_dir()?;
let tmp_file = tmp_dir.join(file_name);
let file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&tmp_file)?;
let writer = BufWriter::new(file);
bincode::serialize_into(writer, data)?;
fs::rename(&tmp_file, &path)?;
Ok(())
}
pub fn get_tmp_dir() -> Result<PathBuf, Error> {
let tmp_dir = env::temp_dir().join("oasysdb");
if !tmp_dir.try_exists()? {
fs::create_dir_all(&tmp_dir)?;
}
Ok(tmp_dir)
}
pub fn parse_file_name(path: impl AsRef<Path>) -> Result<String, Error> {
let file_name = path.as_ref().file_name().ok_or_else(|| {
let code = ErrorCode::FileError;
let message = "Unable to parse the file name from the path.";
Error::new(code, message)
})?;
Ok(file_name.to_string_lossy().to_string())
}