Trait csvsc::RowStream[][src]

pub trait RowStream: IntoIterator<Item = RowResult> {
    fn headers(&self) -> &Headers;

    fn add(self, column: ColSpec) -> Result<Add<Self>>
    where
        Self: Sized
, { ... }
fn del(self, columns: Vec<&str>) -> Del<'_, Self>
    where
        Self: Sized
, { ... }
fn select(self, columns: Vec<&str>) -> Select<'_, Self>
    where
        Self: Sized
, { ... }
fn add_with<F>(
        self,
        colname: &str,
        f: F
    ) -> Result<AddWith<Self, F>, BuildError>
    where
        Self: Sized,
        F: FnMut(&Headers, &Row) -> Result<String>
, { ... }
fn reduce(self, columns: Vec<Box<dyn Aggregate>>) -> Result<Reduce<Self>>
    where
        Self: Sized
, { ... }
fn adjacent_group<F, R, G>(
        self,
        grouping: G,
        f: F
    ) -> AdjacentGroup<Self, F, G>
    where
        F: Fn(MockStream<IntoIter<RowResult>>) -> R,
        R: RowStream,
        Self: Sized
, { ... }
fn group<F, R, G>(self, grouping: G, f: F) -> Group<Self, F, G>
    where
        F: Fn(MockStream<IntoIter<RowResult>>) -> R,
        R: RowStream,
        Self: Sized
, { ... }
fn flush<T>(self, target: T) -> Result<Flush<Self, T>>
    where
        Self: Sized
, { ... }
fn review<F>(self, f: F) -> Inspect<Self, F>
    where
        Self: Sized,
        F: FnMut(&Headers, &RowResult)
, { ... }
fn rename(self, old_name: &str, new_name: &str) -> Rename<Self>
    where
        Self: Sized
, { ... }
fn map_row<F, H, R>(self, f: F, header_map: H) -> MapRow<Self, F>
    where
        Self: Sized,
        F: Fn(&Headers, &Row) -> Result<R>,
        H: Fn(&Headers) -> Headers,
        R: Iterator<Item = RowResult>
, { ... }
fn map_col<F>(self, col: &str, f: F) -> MapCol<Self, F>
    where
        Self: Sized,
        F: Fn(&str) -> Result<String>
, { ... }
fn filter_col<F>(self, col: &str, f: F) -> Result<FilterCol<Self, F>>
    where
        Self: Sized,
        F: Fn(&str) -> bool
, { ... }
fn filter_row<F>(self, f: F) -> FilterRow<Self, F>
    where
        Self: Sized,
        F: Fn(&Headers, &Row) -> bool
, { ... } }

This trait describes de behaviour of every component in the CSV transformation chain. Functions provided by this trait help construct the chain and can be chained.

Implement this trait to extend csvsc with your own processors.

Required methods

fn headers(&self) -> &Headers[src]

Must return headers as they are in this point of the chain. For example if implementor adds a column, its headers() function must return the new headers including the one just added.

Loading content...

Provided methods

fn add(self, column: ColSpec) -> Result<Add<Self>> where
    Self: Sized
[src]

Add a column to each row of the stream.

New columns can be build arbitrarily from previous columns or from a specific column using a regular expression.

use csvsc::prelude::*;
use encoding::all::UTF_8;

let mut chain = InputStreamBuilder::from_paths(&["test/assets/1.csv"])
    .unwrap().build().unwrap()
    .add(
        Column::with_name("new column")
            .from_all_previous()
            .definition("{old col1} - {old col2}")
    ).unwrap();

See Column for options.

If you want to add a constant value or have some other requirement take a look at .add_with().

fn del(self, columns: Vec<&str>) -> Del<'_, Self> where
    Self: Sized
[src]

Deletes the specified columns from each row of the stream. If you have too many columns to delete perhaps instead use .select().

fn select(self, columns: Vec<&str>) -> Select<'_, Self> where
    Self: Sized
[src]

Outputs only the selected columns, ignoring the rest.

If you only want do delete specific columns take a look at .del().

fn add_with<F>(
    self,
    colname: &str,
    f: F
) -> Result<AddWith<Self, F>, BuildError> where
    Self: Sized,
    F: FnMut(&Headers, &Row) -> Result<String>, 
[src]

Adds a column to each row of the stream using a closure to compute its value.

This you can use to add a constant value also.

Example

use csvsc::prelude::*;
use encoding::all::UTF_8;

let mut chain = InputStreamBuilder::from_paths(&["test/assets/1.csv"])
    .unwrap().build().unwrap()
    .add_with("new col", |headers, row| {
        Ok("value".into())
    }).unwrap();

fn reduce(self, columns: Vec<Box<dyn Aggregate>>) -> Result<Reduce<Self>> where
    Self: Sized
[src]

Reduce all the incoming stream into one row, computing some aggregates in the way. All the stream collapses into one row.

The final row contains only the result of reducers and no other column but you might preserve a column using the .last() aggregate.

You’ll likely be using this inside a .group() or .adjacent_group().

Example

use csvsc::prelude::*;
use encoding::all::UTF_8;

let mut chain = InputStreamBuilder::from_paths(&["test/assets/chicken_north.csv"])
    .unwrap().build().unwrap()
    .group(["month"], |row_stream| {
        row_stream
            .reduce(vec![
                Reducer::with_name("avg").of_column("eggs per week").average().unwrap(),
            ]).unwrap()
    });

See Reducer for built-in aggregates.

fn adjacent_group<F, R, G>(self, grouping: G, f: F) -> AdjacentGroup<Self, F, G> where
    F: Fn(MockStream<IntoIter<RowResult>>) -> R,
    R: RowStream,
    Self: Sized
[src]

fn group<F, R, G>(self, grouping: G, f: F) -> Group<Self, F, G> where
    F: Fn(MockStream<IntoIter<RowResult>>) -> R,
    R: RowStream,
    Self: Sized
[src]

fn flush<T>(self, target: T) -> Result<Flush<Self, T>> where
    Self: Sized
[src]

When consumed, writes to destination specified by the column given in the first argument. Other than that this behaves like an id(x) function so you can specify more links in the chain and even more flushers.

fn review<F>(self, f: F) -> Inspect<Self, F> where
    Self: Sized,
    F: FnMut(&Headers, &RowResult), 
[src]

Mostly for debugging, calls a closure on each element. Behaves like the identity function on the stream returning each row untouched.

fn rename(self, old_name: &str, new_name: &str) -> Rename<Self> where
    Self: Sized
[src]

Renames some columns

fn map_row<F, H, R>(self, f: F, header_map: H) -> MapRow<Self, F> where
    Self: Sized,
    F: Fn(&Headers, &Row) -> Result<R>,
    H: Fn(&Headers) -> Headers,
    R: Iterator<Item = RowResult>, 
[src]

Apply a function to every row and use the return values to build the row stream.

This method accepts a closure that must return an iterator over RowResult values, this means that you can create more rows out of a single one.

You’re responsible of providing the new headers and for that you need to use the second closure, that maps the old headers to the new ones.

Example

use csvsc::prelude::*;
use encoding::all::UTF_8;

InputStreamBuilder::from_paths(&["test/assets/1.csv"])
    .unwrap().build().unwrap()
    .map_row(|_headers, row| {
        // Go creative here in the creation of your new row(s)
        Ok(vec![
            Ok(row.clone())
        ].into_iter())
    }, |old_headers| {
        // be responsible and provide proper headers from the old ones
        old_headers.clone()
    })
    .into_iter();

fn map_col<F>(self, col: &str, f: F) -> MapCol<Self, F> where
    Self: Sized,
    F: Fn(&str) -> Result<String>, 
[src]

Apply a function to a single column of the stream, this function dones’t fail if the column dones’t exist.

fn filter_col<F>(self, col: &str, f: F) -> Result<FilterCol<Self, F>> where
    Self: Sized,
    F: Fn(&str) -> bool
[src]

filter entire rows out depending on one column’s value and a condition, leaving errored rows untouched.

fn filter_row<F>(self, f: F) -> FilterRow<Self, F> where
    Self: Sized,
    F: Fn(&Headers, &Row) -> bool
[src]

filter entire rows out depending on one column’s value and a condition, leaving errored rows untouched.

Loading content...

Implementors

impl RowStream for InputStream[src]

impl<I> RowStream for Add<I> where
    I: RowStream
[src]

impl<I> RowStream for MockStream<I> where
    MockStream<I>: IntoIterator<Item = RowResult>, 
[src]

Loading content...