ommui_data 0.12.3

OMMUI data structures
use crate::ommui::{
    CalibrationDescription, LoadableDevice, SamplingSettings,
};
use crate::{file_load_json as load_json, Result};
use indexmap::IndexSet;
use std::path::{Path, PathBuf};
use uuid::Uuid;

#[derive(Debug)]
pub struct Device<'a, P: AsRef<Path>> {
    inner: &'a crate::filesystem::Device<P>,
}

impl<'a, P: AsRef<Path>> Device<'a, P> {
    pub fn new(inner: &'a crate::filesystem::Device<P>) -> Self {
        Device { inner }
    }

    fn product_id(&self) -> String {
        self.inner.product_id.to_string()
    }

    pub fn device_dir(&self) -> PathBuf {
        self.inner.device_dir().join(self.product_id())
    }

    pub fn config_dir(&self) -> PathBuf {
        self.inner.config_dir().join(self.product_id())
    }

    pub fn system_profiles_file(&self) -> PathBuf {
        self.device_dir().join("profiles.json")
    }

    pub fn user_profiles_file(&self) -> PathBuf {
        self.config_dir().join("profiles.json")
    }

    pub fn description_calibration_dir(
        &self,
        calibration_id: &str,
    ) -> PathBuf {
        self.inner
            .description_calibration_dir(calibration_id)
            .join(self.product_id())
    }

    pub fn calibration_description_file(
        &self,
        calibration_id: &str,
    ) -> PathBuf {
        self.description_calibration_dir(calibration_id)
            .join("calibration.json")
    }

    pub fn sampling_dir(&self, sampling_id: &str) -> PathBuf {
        self.inner.sampling_dir(sampling_id).join(self.product_id())
    }

    pub fn sampling_settings_file(&self, sampling_id: &str) -> PathBuf {
        self.sampling_dir(sampling_id).join("settings.json")
    }
}

impl<'a, P: AsRef<Path>> LoadableDevice for Device<'a, P> {
    fn load_system_profile_index(&self) -> Result<IndexSet<Uuid>> {
        load_json(self.system_profiles_file())
    }

    fn load_user_profile_index(&self) -> Result<IndexSet<Uuid>> {
        load_json(self.user_profiles_file())
    }

    fn load_calibration_description(
        &self,
        calibration_id: &str,
    ) -> Result<CalibrationDescription> {
        load_json(self.calibration_description_file(calibration_id))
    }

    fn load_sampling_settings(
        &self,
        sampling_id: &str,
    ) -> Result<SamplingSettings> {
        load_json(self.sampling_settings_file(sampling_id))
    }
}