#![allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PallorCause {
Anemia,
Shock,
Fear,
Cold,
Illness,
}
#[derive(Debug, Clone)]
pub struct PallorMorph {
pub cause: PallorCause,
pub intensity: f32,
pub morph_count: usize,
pub enabled: bool,
}
impl PallorMorph {
pub fn new(morph_count: usize) -> Self {
PallorMorph {
cause: PallorCause::Anemia,
intensity: 0.0,
morph_count,
enabled: true,
}
}
}
pub fn new_pallor_morph(morph_count: usize) -> PallorMorph {
PallorMorph::new(morph_count)
}
pub fn plm_set_cause(morph: &mut PallorMorph, cause: PallorCause) {
morph.cause = cause;
}
pub fn plm_set_intensity(morph: &mut PallorMorph, intensity: f32) {
morph.intensity = intensity.clamp(0.0, 1.0);
}
pub fn plm_evaluate(morph: &PallorMorph) -> Vec<f32> {
if !morph.enabled || morph.morph_count == 0 {
return vec![];
}
vec![morph.intensity; morph.morph_count]
}
pub fn plm_set_enabled(morph: &mut PallorMorph, enabled: bool) {
morph.enabled = enabled;
}
pub fn plm_to_json(morph: &PallorMorph) -> String {
let cause = match morph.cause {
PallorCause::Anemia => "anemia",
PallorCause::Shock => "shock",
PallorCause::Fear => "fear",
PallorCause::Cold => "cold",
PallorCause::Illness => "illness",
};
format!(
r#"{{"cause":"{}","intensity":{},"morph_count":{},"enabled":{}}}"#,
cause, morph.intensity, morph.morph_count, morph.enabled
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_cause() {
let m = new_pallor_morph(4);
assert_eq!(
m.cause,
PallorCause::Anemia
);
}
#[test]
fn test_set_cause() {
let mut m = new_pallor_morph(4);
plm_set_cause(&mut m, PallorCause::Fear);
assert_eq!(m.cause, PallorCause::Fear );
}
#[test]
fn test_intensity_clamp_high() {
let mut m = new_pallor_morph(4);
plm_set_intensity(&mut m, 2.0);
assert!((m.intensity - 1.0).abs() < 1e-6 );
}
#[test]
fn test_intensity_clamp_low() {
let mut m = new_pallor_morph(4);
plm_set_intensity(&mut m, -1.0);
assert!(m.intensity.abs() < 1e-6 );
}
#[test]
fn test_evaluate_length() {
let mut m = new_pallor_morph(5);
plm_set_intensity(&mut m, 0.5);
assert_eq!(
plm_evaluate(&m).len(),
5
);
}
#[test]
fn test_evaluate_disabled() {
let mut m = new_pallor_morph(4);
plm_set_enabled(&mut m, false);
assert!(plm_evaluate(&m).is_empty() );
}
#[test]
fn test_to_json_has_cause() {
let m = new_pallor_morph(4);
let j = plm_to_json(&m);
assert!(j.contains("\"cause\"") );
}
#[test]
fn test_enabled_default() {
let m = new_pallor_morph(4);
assert!(m.enabled );
}
#[test]
fn test_evaluate_matches_intensity() {
let mut m = new_pallor_morph(3);
plm_set_intensity(&mut m, 0.9);
let out = plm_evaluate(&m);
assert!((out[0] - 0.9).abs() < 1e-5 );
}
}