1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use serde::{Deserialize, Serialize};
use std::{
    ops::{Deref, DerefMut},
    path::Path,
};

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Nodes {
    pub sid: u8,
    pub xyz: Vec<Vec<f64>>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct AsmsNodes(Vec<Nodes>);
impl Deref for AsmsNodes {
    type Target = [Nodes];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl DerefMut for AsmsNodes {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

#[derive(Debug, thiserror::Error)]
pub enum NodesError {
    #[error("failed to create file the nodes file")]
    IO(#[from] std::io::Error),
    #[error("failed to encode nodes")]
    Encode(#[from] bincode::error::EncodeError),
    #[cfg(feature = "polars")]
    #[error("failed to write nodes to parquet")]
    Parquet(#[from] polars::error::PolarsError),
}
type Result<T> = std::result::Result<T, NodesError>;

impl AsmsNodes {
    pub fn push(&mut self, nodes: Nodes) {
        self.0.push(nodes)
    }
    pub fn into_bin(&self, path: impl AsRef<Path>) -> Result<()> {
        let mut file = std::fs::File::create(path.as_ref())?;
        bincode::serde::encode_into_std_write(self, &mut file, bincode::config::standard())?;
        Ok(())
    }
    #[cfg(feature = "polars")]
    pub fn into_parquet(&self, path: impl AsRef<Path>) -> Result<()> {
        use polars::prelude::*;
        let mut series = vec![];
        for (i, nodes) in self.0.iter().enumerate() {
            let s: Vec<_> = nodes
                .xyz
                .iter()
                .map(|xyz| {
                    let s: Series = xyz.iter().collect();
                    s
                })
                .collect();
            series.push(Series::new(&format!("S{}", i + 1), s));
        }

        let mut df = DataFrame::new(series)?;

        let mut file = std::fs::File::create(path.as_ref())?;
        ParquetWriter::new(&mut file).finish(&mut df)?;
        Ok(())
    }
}