Skip to main content

med64/
format.rs

1use std::path::Path;
2
3use crate::reader::{is_modat_file, is_moflo_file};
4
5/// Supported MED64 file formats.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum FileFormat {
8    /// Binary 64-channel electrophysiology recording.
9    Modat,
10    /// Legacy / alias MED64 binary (same header as `.modat`).
11    Dat,
12    /// Extended / compressed MED64 stream (`FileReaderDax`).
13    Dax,
14    /// PRESTO / XML-augmented recording (`FileReaderPmodax1`).
15    Modax,
16    /// workflow XML protocol.
17    Moflo,
18    /// Comma-separated waveform export (`time_ms,channel,counts,millivolts`).
19    Csv,
20    /// JSON export (metadata + optional samples).
21    Json,
22}
23
24impl FileFormat {
25    pub fn from_path(path: &Path) -> Option<Self> {
26        if is_modat_file(path) {
27            Some(Self::Modat)
28        } else if is_moflo_file(path) {
29            Some(Self::Moflo)
30        } else {
31            path.extension().and_then(|ext| ext.to_str()).and_then(|ext| {
32                match ext.to_ascii_lowercase().as_str() {
33                    "dat" => Some(Self::Dat),
34                    "dax" => Some(Self::Dax),
35                    "modax" => Some(Self::Modax),
36                    "csv" => Some(Self::Csv),
37                    "json" => Some(Self::Json),
38                    _ => None,
39                }
40            })
41        }
42    }
43
44    pub fn extension(self) -> &'static str {
45        match self {
46            Self::Modat => "modat",
47            Self::Dat => "dat",
48            Self::Dax => "dax",
49            Self::Modax => "modax",
50            Self::Moflo => "moflo",
51            Self::Csv => "csv",
52            Self::Json => "json",
53        }
54    }
55
56    pub fn is_readable(self) -> bool {
57        matches!(self, Self::Modat | Self::Dat | Self::Dax | Self::Modax | Self::Moflo)
58    }
59
60    pub fn is_writable(self) -> bool {
61        matches!(self, Self::Modat | Self::Csv | Self::Json)
62    }
63
64    pub fn is_waveform(self) -> bool {
65        matches!(self, Self::Modat | Self::Dat | Self::Dax | Self::Modax)
66    }
67}