#![allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ErythemaPattern {
Diffuse,
Macular,
Papular,
Reticular,
}
#[derive(Debug, Clone)]
pub struct ErythemaMorph {
pub pattern: ErythemaPattern,
pub intensity: f32,
pub affected_area: f32,
pub morph_count: usize,
pub enabled: bool,
}
impl ErythemaMorph {
pub fn new(morph_count: usize) -> Self {
ErythemaMorph {
pattern: ErythemaPattern::Diffuse,
intensity: 0.0,
affected_area: 0.5,
morph_count,
enabled: true,
}
}
}
pub fn new_erythema_morph(morph_count: usize) -> ErythemaMorph {
ErythemaMorph::new(morph_count)
}
pub fn erm_set_pattern(morph: &mut ErythemaMorph, pattern: ErythemaPattern) {
morph.pattern = pattern;
}
pub fn erm_set_intensity(morph: &mut ErythemaMorph, intensity: f32) {
morph.intensity = intensity.clamp(0.0, 1.0);
}
pub fn erm_set_affected_area(morph: &mut ErythemaMorph, area: f32) {
morph.affected_area = area.clamp(0.0, 1.0);
}
pub fn erm_evaluate(morph: &ErythemaMorph) -> Vec<f32> {
if !morph.enabled || morph.morph_count == 0 {
return vec![];
}
vec![morph.intensity; morph.morph_count]
}
pub fn erm_set_enabled(morph: &mut ErythemaMorph, enabled: bool) {
morph.enabled = enabled;
}
pub fn erm_to_json(morph: &ErythemaMorph) -> String {
let pat = match morph.pattern {
ErythemaPattern::Diffuse => "diffuse",
ErythemaPattern::Macular => "macular",
ErythemaPattern::Papular => "papular",
ErythemaPattern::Reticular => "reticular",
};
format!(
r#"{{"pattern":"{}","intensity":{},"affected_area":{},"morph_count":{},"enabled":{}}}"#,
pat, morph.intensity, morph.affected_area, morph.morph_count, morph.enabled
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_pattern() {
let m = new_erythema_morph(4);
assert_eq!(
m.pattern,
ErythemaPattern::Diffuse
);
}
#[test]
fn test_set_pattern() {
let mut m = new_erythema_morph(4);
erm_set_pattern(&mut m, ErythemaPattern::Macular);
assert_eq!(
m.pattern,
ErythemaPattern::Macular
);
}
#[test]
fn test_intensity_clamp() {
let mut m = new_erythema_morph(4);
erm_set_intensity(&mut m, 1.5);
assert!((m.intensity - 1.0).abs() < 1e-6 );
}
#[test]
fn test_area_clamp() {
let mut m = new_erythema_morph(4);
erm_set_affected_area(&mut m, -0.2);
assert!(m.affected_area.abs() < 1e-6 );
}
#[test]
fn test_evaluate_length() {
let mut m = new_erythema_morph(7);
erm_set_intensity(&mut m, 0.5);
assert_eq!(
erm_evaluate(&m).len(),
7
);
}
#[test]
fn test_evaluate_disabled() {
let mut m = new_erythema_morph(4);
erm_set_enabled(&mut m, false);
assert!(erm_evaluate(&m).is_empty() );
}
#[test]
fn test_to_json_has_pattern() {
let m = new_erythema_morph(4);
let j = erm_to_json(&m);
assert!(j.contains("\"pattern\"") );
}
#[test]
fn test_enabled_default() {
let m = new_erythema_morph(4);
assert!(m.enabled );
}
#[test]
fn test_evaluate_matches_intensity() {
let mut m = new_erythema_morph(4);
erm_set_intensity(&mut m, 0.7);
let out = erm_evaluate(&m);
assert!((out[0] - 0.7).abs() < 1e-5 );
}
}