#![allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CyanosisType {
Central,
Peripheral,
Differential,
}
#[derive(Debug, Clone)]
pub struct CyanosisMorph {
pub cyanosis_type: CyanosisType,
pub oxygen_saturation: f32,
pub intensity: f32,
pub morph_count: usize,
pub enabled: bool,
}
impl CyanosisMorph {
pub fn new(morph_count: usize) -> Self {
CyanosisMorph {
cyanosis_type: CyanosisType::Peripheral,
oxygen_saturation: 1.0,
intensity: 0.0,
morph_count,
enabled: true,
}
}
}
pub fn new_cyanosis_morph(morph_count: usize) -> CyanosisMorph {
CyanosisMorph::new(morph_count)
}
pub fn cym_set_type(morph: &mut CyanosisMorph, cyanosis_type: CyanosisType) {
morph.cyanosis_type = cyanosis_type;
}
pub fn cym_set_oxygen_saturation(morph: &mut CyanosisMorph, sat: f32) {
morph.oxygen_saturation = sat.clamp(0.0, 1.0);
}
pub fn cym_set_intensity(morph: &mut CyanosisMorph, intensity: f32) {
morph.intensity = intensity.clamp(0.0, 1.0);
}
pub fn cym_evaluate(morph: &CyanosisMorph) -> Vec<f32> {
if !morph.enabled || morph.morph_count == 0 {
return vec![];
}
vec![morph.intensity; morph.morph_count]
}
pub fn cym_set_enabled(morph: &mut CyanosisMorph, enabled: bool) {
morph.enabled = enabled;
}
pub fn cym_to_json(morph: &CyanosisMorph) -> String {
let t = match morph.cyanosis_type {
CyanosisType::Central => "central",
CyanosisType::Peripheral => "peripheral",
CyanosisType::Differential => "differential",
};
format!(
r#"{{"type":"{}","oxygen_saturation":{},"intensity":{},"morph_count":{},"enabled":{}}}"#,
t, morph.oxygen_saturation, morph.intensity, morph.morph_count, morph.enabled
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_type() {
let m = new_cyanosis_morph(4);
assert_eq!(
m.cyanosis_type,
CyanosisType::Peripheral
);
}
#[test]
fn test_set_type() {
let mut m = new_cyanosis_morph(4);
cym_set_type(&mut m, CyanosisType::Central);
assert_eq!(
m.cyanosis_type,
CyanosisType::Central
);
}
#[test]
fn test_oxygen_saturation_clamp() {
let mut m = new_cyanosis_morph(4);
cym_set_oxygen_saturation(&mut m, 1.5);
assert!((m.oxygen_saturation - 1.0).abs() < 1e-6 );
}
#[test]
fn test_intensity_clamp() {
let mut m = new_cyanosis_morph(4);
cym_set_intensity(&mut m, -0.5);
assert!(m.intensity.abs() < 1e-6 );
}
#[test]
fn test_evaluate_length() {
let mut m = new_cyanosis_morph(5);
cym_set_intensity(&mut m, 0.6);
assert_eq!(
cym_evaluate(&m).len(),
5
);
}
#[test]
fn test_evaluate_disabled() {
let mut m = new_cyanosis_morph(4);
cym_set_enabled(&mut m, false);
assert!(cym_evaluate(&m).is_empty() );
}
#[test]
fn test_to_json_has_type() {
let m = new_cyanosis_morph(4);
let j = cym_to_json(&m);
assert!(j.contains("\"type\"") );
}
#[test]
fn test_enabled_default() {
let m = new_cyanosis_morph(4);
assert!(m.enabled );
}
#[test]
fn test_evaluate_matches_intensity() {
let mut m = new_cyanosis_morph(3);
cym_set_intensity(&mut m, 0.5);
let out = cym_evaluate(&m);
assert!((out[0] - 0.5).abs() < 1e-5 );
}
}