sfs_core/spectrum/
io.rs

1//! Utilities for reading and writing spectrum.
2
3pub mod read;
4mod text;
5pub mod write;
6
7use crate::array::npy;
8
9/// Supported formats.
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum Format {
12    /// Numpy binary npy format.
13    Npy,
14    /// Plain text format.
15    Text,
16}
17
18impl Format {
19    fn detect(bytes: &[u8]) -> Option<Self> {
20        Self::detect_npy(bytes).xor(Self::detect_plain_text(bytes))
21    }
22
23    fn detect_npy(bytes: &[u8]) -> Option<Self> {
24        (bytes[..npy::MAGIC.len()] == npy::MAGIC).then_some(Self::Npy)
25    }
26
27    fn detect_plain_text(bytes: &[u8]) -> Option<Self> {
28        (bytes[..text::START.len()] == text::START).then_some(Self::Text)
29    }
30}