make-csv 0.1.0

Simple macro's that make writing csv files for plotting purposes easy.
Documentation
/// Creates a csv writer to the given file path.
///
/// ## Usage:
/// ```no_run
/// use make_csv::{csv_start, csv_entry};
///
/// let mut wtr = csv_start!("out.csv");
/// ```
#[macro_export]
macro_rules! csv_start {
    ($filename: expr) => {
        csv::Writer::from_path($filename).unwrap()
    };
}

/// Adds a row to the given csv writer.
///
/// ## Syntax:
/// `csv_entry!(writer <- one, two, three)`
/// Here, `one` etc are expressions that can be formatted to String.
///
/// ## Usage:
/// ```no_run
/// use make_csv::{csv_start, csv_entry, csv_stop};
///
/// let mut wtr = csv_start!("out.csv");
/// csv_entry!(wtr <- "header_0", "header_1");
/// csv_entry!(wtr <- 0.0, 1.0);
/// csv_stop!(wtr);
/// ```
/// This will result in the following `.csv` file:
/// ```csv
/// header_0,header_1
/// 0.0,1.0
/// ```
#[macro_export]
macro_rules! csv_entry {
    ($writer: ident <- $($header:expr),*) => {
        $writer.write_record(&[$(format!("{}", $header).as_str()),*]).expect("ow");
    };
}

/// Flushes the writer and writes to file.
///
/// ## Usage:
/// ```no_run
/// use make_csv::{csv_start, csv_entry, csv_stop};
///
/// let mut wtr = csv_start!("out.csv");
/// csv_entry!(wtr <- "header_0", "header_1");
/// csv_entry!(wtr <- 0.0, 1.0);
/// csv_stop!(wtr);
/// ```
///
#[macro_export]
macro_rules! csv_stop {
    ($writer: ident) => {
        $writer.flush().unwrap();
        drop($writer);
    };
}

/// Runs a python file and blocks until it completes.
///
/// Useful for plotting using a file `plot.py` which
/// reads some `.csv` file and plots the data.
///
/// ## Arguments
/// - `arg0, arg1, arg2, ...`: comma separated arguments to be
///     passed to python3 (python filename for example)
///
/// ## Requirements
/// `python3` needs to be in PATH.
///
/// ## Usage
/// ```no_run
/// make_csv::python!("plot.py", "data.csv");
/// ```
#[macro_export]
macro_rules! python {
    ($($arg: expr),*) => {
        std::process::Command::new("python3")
            $(.arg($arg))*
            .output()
            .unwrap();
    };
}