#![allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum HydrationLevel {
Dehydrated,
Dry,
Normal,
Hydrated,
Overhydrated,
}
#[derive(Debug, Clone)]
pub struct HydrationMorph {
pub level: HydrationLevel,
pub intensity: f32,
pub morph_count: usize,
pub enabled: bool,
}
impl HydrationMorph {
pub fn new(morph_count: usize) -> Self {
HydrationMorph {
level: HydrationLevel::Normal,
intensity: 0.5,
morph_count,
enabled: true,
}
}
}
pub fn new_hydration_morph(morph_count: usize) -> HydrationMorph {
HydrationMorph::new(morph_count)
}
pub fn hym_set_level(morph: &mut HydrationMorph, level: HydrationLevel) {
morph.level = level;
}
pub fn hym_set_intensity(morph: &mut HydrationMorph, intensity: f32) {
morph.intensity = intensity.clamp(0.0, 1.0);
}
pub fn hym_evaluate(morph: &HydrationMorph) -> Vec<f32> {
if !morph.enabled || morph.morph_count == 0 {
return vec![];
}
vec![morph.intensity; morph.morph_count]
}
pub fn hym_set_enabled(morph: &mut HydrationMorph, enabled: bool) {
morph.enabled = enabled;
}
pub fn hym_to_json(morph: &HydrationMorph) -> String {
let lvl = match morph.level {
HydrationLevel::Dehydrated => "dehydrated",
HydrationLevel::Dry => "dry",
HydrationLevel::Normal => "normal",
HydrationLevel::Hydrated => "hydrated",
HydrationLevel::Overhydrated => "overhydrated",
};
format!(
r#"{{"level":"{}","intensity":{},"morph_count":{},"enabled":{}}}"#,
lvl, morph.intensity, morph.morph_count, morph.enabled
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_level() {
let m = new_hydration_morph(4);
assert_eq!(
m.level,
HydrationLevel::Normal
);
}
#[test]
fn test_set_level() {
let mut m = new_hydration_morph(4);
hym_set_level(&mut m, HydrationLevel::Dry);
assert_eq!(m.level, HydrationLevel::Dry );
}
#[test]
fn test_intensity_clamp_high() {
let mut m = new_hydration_morph(4);
hym_set_intensity(&mut m, 2.0);
assert!((m.intensity - 1.0).abs() < 1e-6 );
}
#[test]
fn test_intensity_clamp_low() {
let mut m = new_hydration_morph(4);
hym_set_intensity(&mut m, -0.5);
assert!(m.intensity.abs() < 1e-6 );
}
#[test]
fn test_evaluate_length() {
let m = new_hydration_morph(5);
assert_eq!(
hym_evaluate(&m).len(),
5
);
}
#[test]
fn test_evaluate_disabled() {
let mut m = new_hydration_morph(4);
hym_set_enabled(&mut m, false);
assert!(hym_evaluate(&m).is_empty() );
}
#[test]
fn test_evaluate_zero_count() {
let m = new_hydration_morph(0);
assert!(hym_evaluate(&m).is_empty() );
}
#[test]
fn test_to_json_has_level() {
let m = new_hydration_morph(4);
let j = hym_to_json(&m);
assert!(j.contains("\"level\"") );
}
#[test]
fn test_enabled_default() {
let m = new_hydration_morph(4);
assert!(m.enabled );
}
#[test]
fn test_evaluate_matches_intensity() {
let mut m = new_hydration_morph(3);
hym_set_intensity(&mut m, 0.7);
let out = hym_evaluate(&m);
assert!((out[0] - 0.7).abs() < 1e-5 );
}
}