mirror of
https://github.com/rzmk/czv.git
synced 2025-12-27 03:17:00 +00:00
feat: add czv, czv-wasm, and czv-python (init release)
This commit is contained in:
commit
9799ab694b
40 changed files with 70383 additions and 0 deletions
32
czv-wasm/src/count.rs
Normal file
32
czv-wasm/src/count.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
use crate::Result;
|
||||
use csv::ReaderBuilder;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
/// Returns a count of the total number of rows.
|
||||
///
|
||||
/// @param {string} `file_data` CSV file data.
|
||||
/// @param {boolean | undefined} `include_header_row` Specify whether to include the header row (first row) in the row count. Default is false.
|
||||
/// @returns {number}
|
||||
#[wasm_bindgen(skip_jsdoc, js_name = rowCount)]
|
||||
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
|
||||
///
|
||||
/// @param {string} `file_data` CSV file data.
|
||||
#[wasm_bindgen(skip_jsdoc, js_name = columnCount)]
|
||||
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.");
|
||||
}
|
||||
}
|
||||
30
czv-wasm/src/lib.rs
Normal file
30
czv-wasm/src/lib.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
use wasm_bindgen::JsValue;
|
||||
|
||||
// Error-handling helpers
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
#[error("{0}")]
|
||||
pub struct CzvError(anyhow::Error);
|
||||
|
||||
impl From<csv::Error> for CzvError {
|
||||
fn from(value: csv::Error) -> Self {
|
||||
value.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<JsValue> for CzvError {
|
||||
fn into(self) -> JsValue {
|
||||
JsValue::from_str(self.to_string().as_str())
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue