use super::funejson::Value; /// Clamp `value` to the inclusive range `min..=max`. /// /// Swapped bounds panic rather than answering `min` for everything, which is /// what `value.max(min).min(max)` would quietly do. /// /// # Panics /// Panics if `min` is greater than `max`. pub fn clamp(value: i64, min: i64, max: i64) -> i64 { if min > max { panic!("min must not be greater than max (min {}, max {})", min, max); } if value < min { min } else if value > max { max } else { value } } pub fn fune_vector(args: &[Value]) -> Value { // Refuse what the typed signature cannot hold, with the wording TypeScript // and Python use, rather than let the conversion below quietly change it. if (0..3).any(|i| matches!(args[i], Value::Float(f) if f.fract() != 0.0)) { panic!("clamp operates on integers only"); } Value::Int(clamp(args[0].as_i64(), args[1].as_i64(), args[2].as_i64())) }