use std::str::FromStr;
use std::error::Error;
use std::fmt;
use std::vec::IntoIter;
use std;
pub struct DataTable {
pub data_cols: Vec<DataColumn>,
}
impl DataTable {
pub fn empty() -> DataTable {
DataTable { data_cols: Vec::new() }
}
pub fn cols(&self) -> usize {
self.data_cols.len()
}
pub fn rows(&self) -> usize {
if self.data_cols.len() > 0 {
return self.data_cols[0].len();
}
0usize
}
pub fn shrink_to_fit(&mut self) {
for col in self.data_cols.iter_mut() {
col.shrink_to_fit();
}
self.data_cols.shrink_to_fit();
}
pub fn into_consistent_data<T: FromStr>(self) -> Result<Vec<T>, DataError> {
let mut table_data = Vec::with_capacity(self.cols() * self.rows());
for d in self.data_cols.into_iter() {
match d.into_vec() {
Ok(x) => table_data.extend(x),
Err(e) => return Err(e),
}
}
Ok(table_data)
}
pub fn consistent_data<T: FromStr>(&self) -> Option<Vec<T>> {
let mut table_data = Vec::with_capacity(self.cols() * self.rows());
for d in self.data_cols.iter() {
match d.cast() {
Some(x) => table_data.extend(x),
None => return None,
}
}
Some(table_data)
}
}
#[derive(Debug)]
pub enum DataError {
DataCastError,
}
impl fmt::Display for DataError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&DataError::DataCastError => write!(f, "DataCastError"),
}
}
}
impl Error for DataError {
fn description(&self) -> &str {
match self {
&DataError::DataCastError => "Failed to cast data.",
}
}
}
pub struct DataColumn {
pub name: Option<String>,
data: Vec<String>,
}
impl DataColumn {
pub fn empty() -> DataColumn {
DataColumn {
name: None,
data: Vec::new(),
}
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn data(&self) -> &Vec<String> {
&self.data
}
pub fn push(&mut self, val: &str) {
self.data.push(val.to_owned());
}
pub fn shrink_to_fit(&mut self) {
self.data.shrink_to_fit();
}
pub fn into_vec<T: FromStr>(self) -> Result<Vec<T>, DataError> {
let mut casted_data = Vec::<T>::with_capacity(self.data.len());
for d in self.data.into_iter() {
match T::from_str(&d[..]) {
Ok(x) => casted_data.push(x),
Err(_) => return Err(DataError::DataCastError),
}
}
Ok(casted_data)
}
pub fn cast<T: FromStr>(&self) -> Option<Vec<T>> {
let mut casted_data = Vec::<T>::with_capacity(self.data.len());
for d in self.data.iter() {
match T::from_str(&d[..]) {
Ok(x) => casted_data.push(x),
Err(_) => return None,
}
}
Some(casted_data)
}
pub fn into_iter_cast<U: FromStr>
(self)
-> std::iter::Map<IntoIter<String>, fn(String) -> Result<U, <U as FromStr>::Err>>
where U: FromStr
{
from_str_iter::<_, U>(self.data.into_iter())
}
}
fn from_str_iter<I, U>
(iter: I)
-> std::iter::Map<I, fn(<I as Iterator>::Item) -> Result<U, <U as FromStr>::Err>>
where I: Iterator,
<I as Iterator>::Item: AsRef<str>,
U: FromStr
{
fn from_str_fn<T, U>(item: T) -> Result<U, <U as FromStr>::Err>
where T: AsRef<str>,
U: FromStr
{
FromStr::from_str(item.as_ref())
}
iter.map(from_str_fn)
}