flow_fcs/
datatype.rs

1use anyhow::{Result, anyhow};
2use serde::{Deserialize, Serialize};
3use strum_macros::Display;
4
5/// The data type of the FCS file, which determines how event data is stored
6///
7/// FCS files can store data in different numeric formats. The most common is
8/// single-precision floating point (F), which is also the default.
9#[derive(Default, Display, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
10pub enum FcsDataType {
11    /// Unsigned binary integer
12    I,
13    /// Single-precision floating point (f32)
14    #[default]
15    F,
16    /// Double-precision floating point (f64)
17    D,
18    /// ASCII-encoded string (not supported)
19    A,
20}
21impl FcsDataType {
22    /// Matches the string pattern and returns the corresponding data type
23    /// # Errors
24    /// Will return `Err` if `data_type` is not a valid data type (ASCII-encoded strings are not supported, but binary integers, single-precision floating point, and double-precision floating point are supported)
25    pub fn from_keyword_str(data_type: &str) -> Result<Self> {
26        match data_type {
27            "I" => Ok(Self::I),
28            "F" => Ok(Self::F),
29            "D" => Ok(Self::D),
30            "A" => Err(anyhow!("ASCII-encoded string data type not supported")),
31            _ => Err(anyhow!("Invalid data type")),
32        }
33    }
34
35    /// Returns the keyword string representation of the data type
36    pub fn to_keyword_str(&self) -> &str {
37        match self {
38            Self::I => "I (unsigned binary integer)",
39            Self::F => "F (single-precision floating point)",
40            Self::D => "D (double-precision floating point)",
41            Self::A => "A (ASCII-encoded string)",
42        }
43    }
44
45    /// Returns the number of bytes per event for the data type as an unsigned integer
46    #[must_use]
47    pub const fn get_bytes_per_event(&self) -> usize {
48        match self {
49            Self::I | Self::F => 4,
50            Self::D => 8,
51            Self::A => 0,
52        }
53    }
54}