granges/data/
mod.rs

1//! Data container implementations.
2//!
3
4use crate::{
5    io::TsvConfig,
6    traits::{DataContainer, IntoDatumType},
7};
8use serde::ser::Serializer;
9use serde::Serialize;
10
11#[cfg(feature = "ndarray")]
12pub mod ndarray;
13pub mod operations;
14pub mod vec;
15
16impl<U> DataContainer for Vec<U> {}
17impl DataContainer for () {}
18
19/// These are core supported data types stored in an `enum`, to
20/// unify the types that come out of standard operations of
21/// heterogeneous output types.
22#[derive(Debug, Clone, Serialize)]
23pub enum DatumType {
24    Float32(f32),
25    Float64(f64),
26    String(String),
27    Integer32(i32),
28    Integer64(i64),
29    Unsigned32(u32),
30    Unsigned64(u64),
31    NoValue,
32}
33
34impl DatumType {
35    pub fn into_serializable(self, config: &TsvConfig) -> SerializableDatumType {
36        SerializableDatumType {
37            datum: self,
38            config,
39        }
40    }
41}
42
43#[derive(Debug, Clone)]
44pub struct SerializableDatumType<'a> {
45    pub datum: DatumType,
46    pub config: &'a TsvConfig,
47}
48
49impl<'a> Serialize for SerializableDatumType<'a> {
50    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
51    where
52        S: Serializer,
53    {
54        match &self.datum {
55            DatumType::NoValue => serializer.serialize_str(&self.config.no_value_string),
56            DatumType::Float32(value) => serializer.serialize_str(&value.to_string()),
57            DatumType::Float64(value) => serializer.serialize_str(&value.to_string()),
58            DatumType::String(value) => serializer.serialize_str(value),
59            DatumType::Integer32(value) => serializer.serialize_str(&value.to_string()),
60            DatumType::Integer64(value) => serializer.serialize_str(&value.to_string()),
61            DatumType::Unsigned32(value) => serializer.serialize_str(&value.to_string()),
62            DatumType::Unsigned64(value) => serializer.serialize_str(&value.to_string()),
63        }
64    }
65}
66
67impl IntoDatumType for f64 {
68    fn into_data_type(self) -> DatumType {
69        DatumType::Float64(self)
70    }
71}
72impl IntoDatumType for i64 {
73    fn into_data_type(self) -> DatumType {
74        DatumType::Integer64(self)
75    }
76}
77impl IntoDatumType for String {
78    fn into_data_type(self) -> DatumType {
79        DatumType::String(self)
80    }
81}
82
83impl IntoDatumType for f32 {
84    fn into_data_type(self) -> DatumType {
85        DatumType::Float32(self)
86    }
87}
88
89impl IntoDatumType for i32 {
90    fn into_data_type(self) -> DatumType {
91        DatumType::Integer32(self)
92    }
93}
94
95impl IntoDatumType for u32 {
96    fn into_data_type(self) -> DatumType {
97        DatumType::Unsigned32(self)
98    }
99}
100
101impl IntoDatumType for u64 {
102    fn into_data_type(self) -> DatumType {
103        DatumType::Unsigned64(self)
104    }
105}
106
107// Conversion from field types to `DatumType`
108impl<T: IntoDatumType> From<T> for DatumType {
109    fn from(item: T) -> Self {
110        item.into_data_type()
111    }
112}