med64 0.0.2

Headless MED64 electrophysiology toolkit: .modat I/O, spike/burst analysis, assays, spike-rate maps, and CSV/HTML/SVG/HDF5 export
Documentation
//! Power spectrum analysis chart display prefs (`conf_powerspectrumanalysis_pref.properties`).

use std::path::Path;

use crate::error::{Med64Error, Result};

/// All 22 keys from `conf_powerspectrumanalysis_pref.properties`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PsaChartPrefs {
    pub chobgcolor: String,
    pub chocolumncolor: String,
    pub chocolumnwidth: String,
    pub cholinecolor: String,
    pub cholinewidth: String,
    pub choxaxisfont: String,
    pub choxaxisfont_t: String,
    pub choxaxisfont_t_spect: String,
    pub choxaxisfontsize: String,
    pub choxaxisfontsize_t: String,
    pub choxaxisfontsize_t_spect: String,
    pub choxaxisfontsizespect: String,
    pub choxaxisfontspect: String,
    pub choyaxisfont: String,
    pub choyaxisfont_t: String,
    pub choyaxisfont_t_spect: String,
    pub choyaxisfontsize: String,
    pub choyaxisfontsize_t: String,
    pub choyaxisfontsize_t_spect: String,
    pub choyaxisfontsizespect: String,
    pub choyaxisfontspect: String,
    pub gridonoff: String,
}

impl Default for PsaChartPrefs {
    fn default() -> Self {
        Self {
            chobgcolor: "White".into(),
            chocolumncolor: "Black".into(),
            chocolumnwidth: "1".into(),
            cholinecolor: "Black".into(),
            cholinewidth: "1".into(),
            choxaxisfont: "Microsoft Sans Serif".into(),
            choxaxisfont_t: "Microsoft Sans Serif".into(),
            choxaxisfont_t_spect: "Microsoft Sans Serif".into(),
            choxaxisfontsize: "11".into(),
            choxaxisfontsize_t: "13".into(),
            choxaxisfontsize_t_spect: "13".into(),
            choxaxisfontsizespect: "11".into(),
            choxaxisfontspect: "Microsoft Sans Serif".into(),
            choyaxisfont: "Microsoft Sans Serif".into(),
            choyaxisfont_t: "Microsoft Sans Serif".into(),
            choyaxisfont_t_spect: "Microsoft Sans Serif".into(),
            choyaxisfontsize: "11".into(),
            choyaxisfontsize_t: "13".into(),
            choyaxisfontsize_t_spect: "13".into(),
            choyaxisfontsizespect: "11".into(),
            choyaxisfontspect: "Microsoft Sans Serif".into(),
            gridonoff: "On".into(),
        }
    }
}

impl PsaChartPrefs {
    /// Optional CSS custom-property block for spectrum HTML wrappers.
    pub fn apply_to_css_vars(&self) -> String {
        format!(
            ":root {{\n\
               --psa-bg: {bg};\n\
               --psa-column: {col};\n\
               --psa-column-width: {cw}px;\n\
               --psa-line: {line};\n\
               --psa-line-width: {lw}px;\n\
               --psa-x-font: {xf};\n\
               --psa-x-font-size: {xs}px;\n\
               --psa-y-font: {yf};\n\
               --psa-y-font-size: {ys}px;\n\
               --psa-grid: {grid};\n\
             }}\n",
            bg = css_color(&self.chobgcolor),
            col = css_color(&self.chocolumncolor),
            cw = self.chocolumnwidth,
            line = css_color(&self.cholinecolor),
            lw = self.cholinewidth,
            xf = self.choxaxisfontspect,
            xs = self.choxaxisfontsizespect,
            yf = self.choyaxisfontspect,
            ys = self.choyaxisfontsizespect,
            grid = if self.gridonoff.eq_ignore_ascii_case("on") { "1" } else { "0" },
        )
    }
}

fn css_color(name: &str) -> String {
    match name.trim().to_ascii_lowercase().as_str() {
        "white" => "#ffffff".into(),
        "black" => "#000000".into(),
        other if other.starts_with('#') => other.to_string(),
        other => other.to_string(),
    }
}

/// Load `conf_powerspectrumanalysis_pref.properties`.
pub fn load_psa_chart_prefs(path: impl AsRef<Path>) -> Result<PsaChartPrefs> {
    let path = path.as_ref();
    let text = std::fs::read_to_string(path)
        .map_err(|source| Med64Error::Io { path: path.to_path_buf(), source })?;
    let mut prefs = PsaChartPrefs::default();
    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        let Some((k, v)) = line.split_once('=') else {
            continue;
        };
        let v = v.trim().to_string();
        match k.trim() {
            "chobgcolor" => prefs.chobgcolor = v,
            "chocolumncolor" => prefs.chocolumncolor = v,
            "chocolumnwidth" => prefs.chocolumnwidth = v,
            "cholinecolor" => prefs.cholinecolor = v,
            "cholinewidth" => prefs.cholinewidth = v,
            "choxaxisfont" => prefs.choxaxisfont = v,
            "choxaxisfontT" => prefs.choxaxisfont_t = v,
            "choxaxisfontTspect" => prefs.choxaxisfont_t_spect = v,
            "choxaxisfontsize" => prefs.choxaxisfontsize = v,
            "choxaxisfontsizeT" => prefs.choxaxisfontsize_t = v,
            "choxaxisfontsizeTspect" => prefs.choxaxisfontsize_t_spect = v,
            "choxaxisfontsizespect" => prefs.choxaxisfontsizespect = v,
            "choxaxisfontspect" => prefs.choxaxisfontspect = v,
            "choyaxisfont" => prefs.choyaxisfont = v,
            "choyaxisfontT" => prefs.choyaxisfont_t = v,
            "choyaxisfontTspect" => prefs.choyaxisfont_t_spect = v,
            "choyaxisfontsize" => prefs.choyaxisfontsize = v,
            "choyaxisfontsizeT" => prefs.choyaxisfontsize_t = v,
            "choyaxisfontsizeTspect" => prefs.choyaxisfontsize_t_spect = v,
            "choyaxisfontsizespect" => prefs.choyaxisfontsizespect = v,
            "choyaxisfontspect" => prefs.choyaxisfontspect = v,
            "gridonoff" => prefs.gridonoff = v,
            _ => {}
        }
    }
    Ok(prefs)
}

/// Write `conf_powerspectrumanalysis_pref.properties`.
pub fn write_psa_chart_prefs(prefs: &PsaChartPrefs, path: impl AsRef<Path>) -> Result<()> {
    let path = path.as_ref();
    let body = format!(
        "# med64 power spectrum chart prefs\n\
         chobgcolor={}\n\
         chocolumncolor={}\n\
         chocolumnwidth={}\n\
         cholinecolor={}\n\
         cholinewidth={}\n\
         choxaxisfont={}\n\
         choxaxisfontT={}\n\
         choxaxisfontTspect={}\n\
         choxaxisfontsize={}\n\
         choxaxisfontsizeT={}\n\
         choxaxisfontsizeTspect={}\n\
         choxaxisfontsizespect={}\n\
         choxaxisfontspect={}\n\
         choyaxisfont={}\n\
         choyaxisfontT={}\n\
         choyaxisfontTspect={}\n\
         choyaxisfontsize={}\n\
         choyaxisfontsizeT={}\n\
         choyaxisfontsizeTspect={}\n\
         choyaxisfontsizespect={}\n\
         choyaxisfontspect={}\n\
         gridonoff={}\n",
        prefs.chobgcolor,
        prefs.chocolumncolor,
        prefs.chocolumnwidth,
        prefs.cholinecolor,
        prefs.cholinewidth,
        prefs.choxaxisfont,
        prefs.choxaxisfont_t,
        prefs.choxaxisfont_t_spect,
        prefs.choxaxisfontsize,
        prefs.choxaxisfontsize_t,
        prefs.choxaxisfontsize_t_spect,
        prefs.choxaxisfontsizespect,
        prefs.choxaxisfontspect,
        prefs.choyaxisfont,
        prefs.choyaxisfont_t,
        prefs.choyaxisfont_t_spect,
        prefs.choyaxisfontsize,
        prefs.choyaxisfontsize_t,
        prefs.choyaxisfontsize_t_spect,
        prefs.choyaxisfontsizespect,
        prefs.choyaxisfontspect,
        prefs.gridonoff,
    );
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).ok();
    }
    std::fs::write(path, body)
        .map_err(|source| Med64Error::WriteIo { path: path.to_path_buf(), source })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn psa_chart_prefs_roundtrip() {
        let dir = std::env::temp_dir().join(format!("med64-psa-chart-{}", std::process::id()));
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("conf_powerspectrumanalysis_pref.properties");
        let mut prefs = PsaChartPrefs::default();
        prefs.chobgcolor = "Black".into();
        prefs.gridonoff = "Off".into();
        prefs.choxaxisfontsizespect = "14".into();
        write_psa_chart_prefs(&prefs, &path).unwrap();
        let loaded = load_psa_chart_prefs(&path).unwrap();
        assert_eq!(loaded, prefs);
        let css = loaded.apply_to_css_vars();
        assert!(css.contains("--psa-bg:"));
        assert!(css.contains("--psa-grid: 0"));
        let _ = std::fs::remove_dir_all(dir);
    }
}