hdv/io/
mod.rs

1use crate::{
2    format::{AtomScheme, AtomValue, ValueRow},
3    serde::ObjectScheme,
4};
5
6pub mod bin;
7#[cfg(feature = "polars")]
8pub mod polars;
9pub mod text;
10
11#[derive(Debug)]
12struct HdvShiftedHeader {
13    header: Vec<AtomScheme>,
14    column_shifting: Vec<usize>,
15}
16impl HdvShiftedHeader {
17    pub fn new(header: Vec<AtomScheme>, object_scheme: &ObjectScheme) -> Option<Self> {
18        let required = object_scheme.atom_schemes();
19        let mut column_shifting = vec![];
20        for required in &required {
21            let i = header.iter().position(|x| x == required)?;
22            column_shifting.push(i);
23        }
24        Some(Self {
25            header,
26            column_shifting,
27        })
28    }
29
30    pub fn header(&self) -> &Vec<AtomScheme> {
31        &self.header
32    }
33
34    pub fn shift(&self, source: &[Option<AtomValue>], values: &mut Vec<Option<AtomValue>>) {
35        for i in self.column_shifting.iter().copied() {
36            let value = source[i].clone();
37            values.push(value);
38        }
39    }
40}
41
42fn assert_atom_types(header: &[AtomScheme], row: &ValueRow) {
43    assert_eq!(header.len(), row.atoms().len());
44    for (a, b) in header.iter().zip(row.atoms().iter()) {
45        let Some(b) = b else {
46            continue;
47        };
48        assert_eq!(a.r#type, b.into());
49    }
50}