use crate::error::Error;
use crate::error::Result;
use crate::matrix::Matrix;
use std::io::Read;
use std::io::Write;
#[cfg(feature = "serde_mat")]
#[doc(cfg(feature = "serde_mat"))]
pub fn read<T, const ROW: usize, const COL: usize>(
path: &std::path::Path,
index: usize,
) -> Result<Matrix<T, ROW, COL>>
where
T: Clone + for<'a> serde::Deserialize<'a>,
{
let full_path = match path.canonicalize() {
Ok(full_path) => Ok(full_path),
Err(_) => Err(Error::Message(format!("path {:?} is wrong", path))),
}?;
let mut file = match std::fs::File::open(&full_path) {
Ok(handle) => Ok(handle),
Err(_) => Err(Error::Message(format!("file {:?} is not exist", full_path))),
}?;
let mut mats_string = String::new();
match file.read_to_string(&mut mats_string) {
Ok(_) => Ok(()),
Err(_) => Err(Error::Message(format!(
"read file {:?} to string failed",
full_path
))),
}?;
let mats: Vec<Matrix<T, ROW, COL>> = match serde_json::from_str(&mats_string) {
Ok(matrices) => Ok(matrices),
Err(_) => Err(Error::Message(format!(
"file {:?} contents wrong format",
path
))),
}?;
match mats.get(index - 1) {
Some(mat) => Ok(mat.to_owned()),
None => Err(Error::Message(format!(
"read index {} out of boundary",
index
))),
}
}
#[cfg(feature = "serde_mat")]
#[doc(cfg(feature = "serde_mat"))]
pub fn write<T, const ROW: usize, const COL: usize>(
mats: &[&Matrix<T, ROW, COL>],
path: &std::path::Path,
) -> Result<()>
where
T: std::fmt::Debug + serde::Serialize,
{
let full_path = match path.canonicalize() {
Ok(full_path) => Ok(full_path),
Err(_) => Err(Error::Message(format!("path {:?} is wrong", path))),
}?;
let mut file = match std::fs::File::create(&full_path) {
Ok(handle) => Ok(handle),
Err(_) => Err(Error::Message(format!(
"create file {:?} failed",
full_path
))),
}?;
let mats_string = match serde_json::to_string(&mats) {
Ok(mats_string) => Ok(mats_string),
Err(_) => Err(Error::Message(format!(
"serialize {:?} to string failed",
mats
))),
}?;
match file.write(mats_string.as_bytes()) {
Ok(_) => Ok(()),
Err(_) => Err(Error::Message(format!(
"write to file {:?} failed",
full_path
))),
}
}