#![allow(dead_code)]
#[derive(Debug, Clone, Default)]
pub struct PalmSplayState {
pub index: f32,
pub middle: f32,
pub ring: f32,
pub pinky: f32,
pub thumb_ab: f32,
}
pub fn ps_set_all(state: &mut PalmSplayState, amount: f32) {
let a = amount.clamp(0.0, 1.0);
state.index = a;
state.middle = a;
state.ring = a;
state.pinky = a;
state.thumb_ab = a;
}
pub fn ps_set_finger(state: &mut PalmSplayState, finger: usize, amount: f32) {
let a = amount.clamp(0.0, 1.0);
match finger {
0 => state.index = a,
1 => state.middle = a,
2 => state.ring = a,
3 => state.pinky = a,
4 => state.thumb_ab = a,
_ => {}
}
}
pub fn ps_mean(state: &PalmSplayState) -> f32 {
(state.index + state.middle + state.ring + state.pinky + state.thumb_ab) / 5.0
}
pub fn ps_get_finger(state: &PalmSplayState, finger: usize) -> f32 {
match finger {
0 => state.index,
1 => state.middle,
2 => state.ring,
3 => state.pinky,
4 => state.thumb_ab,
_ => 0.0,
}
}
pub fn ps_reset(state: &mut PalmSplayState) {
*state = PalmSplayState::default();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_zero() {
let s = PalmSplayState::default();
assert_eq!(ps_mean(&s), 0.0);
}
#[test]
fn test_set_all() {
let mut s = PalmSplayState::default();
ps_set_all(&mut s, 0.5);
assert!((s.index - 0.5).abs() < 1e-6);
assert!((s.pinky - 0.5).abs() < 1e-6);
}
#[test]
fn test_set_finger_index() {
let mut s = PalmSplayState::default();
ps_set_finger(&mut s, 0, 0.8);
assert!((s.index - 0.8).abs() < 1e-6);
assert_eq!(s.middle, 0.0);
}
#[test]
fn test_set_finger_thumb() {
let mut s = PalmSplayState::default();
ps_set_finger(&mut s, 4, 0.6);
assert!((s.thumb_ab - 0.6).abs() < 1e-6);
}
#[test]
fn test_mean() {
let mut s = PalmSplayState::default();
ps_set_all(&mut s, 0.6);
assert!((ps_mean(&s) - 0.6).abs() < 1e-6);
}
#[test]
fn test_get_finger() {
let mut s = PalmSplayState::default();
ps_set_finger(&mut s, 2, 0.9);
assert!((ps_get_finger(&s, 2) - 0.9).abs() < 1e-6);
}
#[test]
fn test_invalid_finger_get() {
let s = PalmSplayState::default();
assert_eq!(ps_get_finger(&s, 99), 0.0);
}
#[test]
fn test_reset() {
let mut s = PalmSplayState::default();
ps_set_all(&mut s, 1.0);
ps_reset(&mut s);
assert_eq!(ps_mean(&s), 0.0);
}
#[test]
fn test_clamp() {
let mut s = PalmSplayState::default();
ps_set_all(&mut s, 5.0);
assert!((s.index - 1.0).abs() < 1e-6);
}
}