feat: add czv, czv-wasm, and czv-python (init release)

This commit is contained in:
rzmk 2024-06-19 22:37:33 -04:00
commit 9799ab694b
No known key found for this signature in database
40 changed files with 70383 additions and 0 deletions

33
czv-python/src/count.rs Normal file
View file

@ -0,0 +1,33 @@
use crate::Result;
use csv::ReaderBuilder;
use pyo3::pyfunction;
/// Returns a count of the total number of rows.
///
/// ## Arguments
///
/// * `file_data` - CSV file data.
/// * `include_header_row` - Specify whether to include the header row (first row) in the row count.
#[pyfunction]
pub fn row_count(file_data: String, include_header_row: Option<bool>) -> Result<usize> {
let mut rdr = ReaderBuilder::new();
rdr.has_headers(!include_header_row.unwrap_or(false));
return Ok(rdr.from_reader(file_data.as_bytes()).records().count());
}
/// Returns a count of the total number of columns (fields).
///
/// ## Arguments
///
/// * `file_data` - CSV file data.
#[pyfunction]
pub fn column_count(file_data: Option<String>) -> Result<usize> {
let rdr = ReaderBuilder::new();
if let Some(file_data) = file_data {
return Ok(rdr.from_reader(file_data.as_bytes()).headers()?.len());
} else {
bail!("Could not determine a file path or file data for column_count_builder.");
}
}

43
czv-python/src/lib.rs Normal file
View file

@ -0,0 +1,43 @@
use pyo3::prelude::*;
// Error-handling helpers
#[derive(thiserror::Error, Debug)]
#[error("{0}")]
pub struct CzvError(anyhow::Error);
impl From<pyo3::PyErr> for CzvError {
fn from(value: pyo3::PyErr) -> Self {
value.into()
}
}
impl From<csv::Error> for CzvError {
fn from(value: csv::Error) -> Self {
value.into()
}
}
impl From<CzvError> for pyo3::PyErr {
fn from(value: CzvError) -> Self {
value.into()
}
}
pub type Result<T> = anyhow::Result<T, CzvError>;
#[allow(unused_macros)]
macro_rules! bail {
($err:expr $(,)?) => {
return Err(crate::CzvError(anyhow::anyhow!($err)))
};
}
// Command imports
pub mod count;
#[pymodule]
fn czv(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(count::row_count, m)?)?;
m.add_function(wrap_pyfunction!(count::column_count, m)?)?;
Ok(())
}