use crate::matrix::matrix::Matrix;
use std::{
fs::File,
io::{BufReader, BufWriter},
path::Path,
};
pub fn to_file<N, P, const R: usize, const C: usize>(m: &Matrix<N, R, C>, path: P)
where
N: std::fmt::Debug,
P: AsRef<Path> + std::fmt::Debug,
{
use std::io::Write;
let file = File::create(&path).expect(&format!(
"Error[matrix::serde::to_file]: Failed to create file ({:?}).",
path
));
let mut writer = BufWriter::new(file);
let data = m
.linear_iter()
.map(|e| format!("{:?}", e))
.collect::<Vec<_>>()
.join(",");
writer
.write_all(data.as_bytes())
.expect("Error[matrix::serde::to_file]: Failed to write the matrix to the file.");
}
pub fn from_file<N, P, const R: usize, const C: usize>(path: P) -> Option<Matrix<N, R, C>>
where
N: Clone + std::str::FromStr,
P: AsRef<Path> + std::fmt::Debug,
{
use std::io::Read;
let file = File::open(&path).expect(&format!(
"Error[matrix::serde::from_file]: Failed to create file ({:?}).",
path
));
let mut reader = BufReader::new(file);
let mut data = String::new();
reader.read_to_string(&mut data).expect(
"Error[matrix::serde::from_file]: Failed to read the file content into the string.",
);
let inner = data
.split(",")
.map(|s| s.trim())
.filter(|s| !s.is_empty())
.map(|s| {
s.parse::<N>().ok().expect(&format!(
"Error[matrix::serde::from_file]: Failed to parse ({}) from the string.",
s
))
})
.collect::<Vec<N>>();
Matrix::of(&inner)
}
pub fn from_stdin<N, const R: usize, const C: usize>() -> Matrix<N, R, C>
where
N: std::str::FromStr,
{
let stdin = std::io::stdin();
let mut buffer = String::new();
let mut inner = Vec::with_capacity(R * C);
println!("Please enter the matrix elements ({}, {}):", R, C);
while inner.len() < R * C {
stdin
.read_line(&mut buffer)
.expect("Error[matrix::serde::from_stdin]: Failed to read data from 'stdin'.");
buffer
.trim()
.split(|c: char| c.is_whitespace() || c == ',')
.filter(|s| !s.is_empty())
.for_each(|s| {
let e = s.parse::<N>().ok().expect(&format!(
"Error[matrix::serde::from_file]: Failed to parse ({}) from the string.",
s
));
inner.push(e);
});
buffer.clear();
}
Matrix { inner }
}