Skip to content

Instantly share code, notes, and snippets.

@justanotherdot
Last active December 22, 2020 03:34
Show Gist options
  • Save justanotherdot/6a2479962f4a400809a65fba2c134dc9 to your computer and use it in GitHub Desktop.
Save justanotherdot/6a2479962f4a400809a65fba2c134dc9 to your computer and use it in GitHub Desktop.
Various ways (and non-ways) to deserialize to byte arrays use serde.
use csv::ReaderBuilder;
use std::error::Error;
use std::path::PathBuf;
use structopt::StructOpt;
//use assert_no_alloc::*;
//#[cfg(debug_assertions)]
//#[global_allocator]
//static A: AllocDisabler = AllocDisabler;
enum Field {
Unknown,
String,
Integer,
Float,
}
fn example(data: PathBuf) -> Result<(), Box<dyn Error>> {
let mut rdr = ReaderBuilder::new().quoting(false).from_path(data)?;
let hs = rdr.headers()?.clone();
let mut vs = Vec::with_capacity(hs.len());
let mut record = csv::ByteRecord::new();
while rdr.read_byte_record(&mut record)? {
for (i, v) in record.iter().enumerate() {
match vs.get(i) {
None => vs.push(parse(&v)),
Some(_) => {
vs[i] = parse(&v); // TODO unification (missing) step.
}
}
}
record.clear();
}
Ok(())
}
fn parse(bytes: &[u8]) -> Option<Field> {
//assert_no_alloc(|| {
let string = match std::str::from_utf8(bytes) {
Ok(v) => v,
Err(_) => return Some(Field::Unknown),
};
if string.parse::<i64>().is_ok() {
return Some(Field::Integer);
};
if string.parse::<f64>().is_ok() {
return Some(Field::Float);
};
Some(Field::String)
//})
}
#[derive(Debug, StructOpt)]
#[structopt(
name = "serde-byte-test",
about = "A demo for byte deserialization via `csv`."
)]
struct Opt {
#[structopt(parse(from_os_str))]
input: PathBuf,
}
#[allow(unused)]
macro_rules! dhat {
() => {
use dhat::{Dhat, DhatAlloc};
#[global_allocator]
static ALLOCATOR: DhatAlloc = DhatAlloc;
let _dhat = Dhat::start_heap_profiling();
};
}
fn main() {
//dhat!();
let opt = Opt::from_args();
example(opt.input).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment