7 releases (4 breaking)

new 0.5.0 Nov 2, 2024
0.4.1 Sep 19, 2024
0.3.0 Aug 20, 2024
0.2.0 Jun 28, 2024
0.1.1 May 23, 2024

#1290 in Encoding

Download history 9/week @ 2024-07-13 2/week @ 2024-07-20 17/week @ 2024-07-27 1/week @ 2024-08-03 11/week @ 2024-08-10 163/week @ 2024-08-17 28/week @ 2024-08-24 38/week @ 2024-08-31 8/week @ 2024-09-07 165/week @ 2024-09-14 114/week @ 2024-09-21 39/week @ 2024-09-28 11/week @ 2024-10-05 17/week @ 2024-10-12 14/week @ 2024-10-19 6/week @ 2024-10-26

53 downloads per month
Used in 3 crates (2 directly)

MIT license

52KB
1.5K SLoC

hdv

Header determined values.

CSV but can be parsed in a multi-layer struct.

Usage

Import dependencies

use std::sync::Arc;

use hdv::{
    format::AtomValue,
    io::{
        bin::{HdvBinReader, HdvBinWriter},
        text::{HdvTextReader, HdvTextWriter, HdvTextWriterOptions},
    },
    serde::{HdvDeserialize, HdvSerialize},
};
use hdv_derive::HdvSerde;

Write and read data in binary format

#[derive(Debug, HdvSerde, PartialEq)]
pub struct A {
    a: u16,
    b: Option<B>,
    c: Option<f64>,
    d: B,
}
#[derive(Debug, HdvSerde, PartialEq)]
struct B {
    a: Arc<[u8]>,
    b: i64,
    c: Arc<str>,
    d: Option<Arc<[u8]>>,
}

let a = A {
    a: 1,
    b: None,
    c: Some(3.),
    d: B {
        a: b"hello".as_ref().into(),
        b: 2,
        c: "world".into(),
        d: None,
    },
};

let mut buf = vec![];
let mut writer = HdvBinWriter::new(&mut buf);
writer.write(&a).unwrap();
writer.flush().unwrap();

let mut reader = HdvBinReader::new(std::io::Cursor::new(&buf));
let a_: A = reader.read().unwrap();
assert_eq!(a, a_);

#[derive(Debug, HdvSerde, PartialEq)]
pub struct PartialA {
    c: Option<f64>,
    a: u16,
}

let mut reader = HdvBinReader::new(std::io::Cursor::new(&buf));
let partial_a: PartialA = reader.read().unwrap();
assert_eq!(a.a, partial_a.a);
assert_eq!(a.c, partial_a.c);

Write and read data in text format

Currently the text format does not accept:

  • bytes (Vec<u8>);
  • strings containing any of the chars ,, ", and \n or starting with whitespace characters.
#[derive(Debug, HdvSerde, PartialEq)]
pub struct A {
    a: u16,
    b: Option<B>,
    c: Option<f64>,
    d: B,
}
#[derive(Debug, HdvSerde, PartialEq)]
struct B {
    b: i64,
    c: Arc<str>,
}

let a = A {
    a: 1,
    b: None,
    c: Some(3.),
    d: B {
        b: 2,
        c: "world".into(),
    },
};

let mut buf = vec![];
let options = HdvTextWriterOptions {
    is_csv_header: false,
};
let mut writer = HdvTextWriter::new(&mut buf, options);
writer.write(&a).unwrap();
writer.flush().unwrap();

let mut reader = HdvTextReader::new(std::io::Cursor::new(&buf));
let a_: A = reader.read().unwrap();
assert_eq!(a, a_);

#[derive(Debug, HdvSerde, PartialEq)]
pub struct PartialA {
    c: Option<f64>,
    a: u16,
}

let mut reader = HdvTextReader::new(std::io::Cursor::new(&buf));
let partial_a: PartialA = reader.read().unwrap();
assert_eq!(a.a, partial_a.a);
assert_eq!(a.c, partial_a.c);

Dependencies

~1–11MB
~126K SLoC