use std::{
vec,
collections::HashMap,
};
use crate::{
add, col, add_with, error, reduce,
Add, ColSpec, Flush, Headers, Inspect, Reduce, Row, RowResult, AddWith,
Del, Group, AdjacentGroup, MockStream, Rename, GroupBuildError, MapRow, MapCol,
aggregate::Aggregate,
flush::FlushTarget,
};
pub trait RowStream: IntoIterator<Item = RowResult> {
fn headers(&self) -> &Headers;
fn add(self, column: ColSpec) -> Result<Add<Self>, add::BuildError>
where
Self: Sized,
{
Add::new(self, column)
}
fn del(self, columns: Vec<&str>) -> Del<Self>
where
Self: Sized,
{
Del::new(self, columns)
}
fn add_with<F>(self, colname: &str, f: F) -> Result<AddWith<Self, F>, add_with::BuildError>
where
Self: Sized,
F: FnMut(&Headers, &Row) -> Result<String, col::BuildError>,
{
AddWith::new(self, colname, f)
}
fn reduce(
self,
columns: Vec<Box<dyn Aggregate>>,
) -> Result<Reduce<Self>, reduce::BuildError>
where
Self: Sized,
{
Reduce::new(self, columns)
}
fn adjacent_group<H, F, R>(
self,
header_map: H,
f: F,
grouping: &[&str],
) -> Result<AdjacentGroup<Self, F>, GroupBuildError>
where
H: FnMut(Headers) -> Headers,
F: FnMut(MockStream<vec::IntoIter<RowResult>>) -> R,
R: RowStream,
Self: Sized,
{
AdjacentGroup::new(self, header_map, f, grouping)
}
fn group<H, F, R>(
self,
header_map: H,
f: F,
grouping: &[&str],
) -> Result<Group<Self, F>, GroupBuildError>
where
H: FnMut(Headers) -> Headers,
F: FnMut(MockStream<vec::IntoIter<RowResult>>) -> R,
R: RowStream,
Self: Sized,
{
Group::new(self, header_map, f, grouping)
}
fn flush(self, target: FlushTarget) -> error::Result<Flush<Self>>
where
Self: Sized,
{
Ok(Flush::new(self, target)?)
}
fn inspect<F>(self, f: F) -> Inspect<Self, F>
where
Self: Sized,
F: FnMut(&Headers, &RowResult),
{
Inspect::new(self, f)
}
fn rename(self, name_map: &HashMap<&str, &str>) -> Rename<Self>
where
Self: Sized,
{
Rename::new(self, name_map)
}
fn map_row<F>(self, f: F) -> MapRow<Self, F>
where
Self: Sized,
F: Fn(&Headers, &Row) -> RowResult,
{
MapRow::new(self, f)
}
fn map_col<F>(self, col: String, f: F) -> MapCol<Self, F>
where
Self: Sized,
F: Fn(&str) -> Result<String, col::BuildError>,
{
MapCol::new(self, col, f)
}
}