From 24f3593de05faacd37343beae5bcaca4fc34d59a Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 8 Mar 2022 18:30:06 +0100 Subject: [PATCH] Move order conversion to formatters module --- plugins/puberty_simulator/src/lib.rs | 5 ++--- src/formatters.rs | 12 ++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/plugins/puberty_simulator/src/lib.rs b/plugins/puberty_simulator/src/lib.rs index 638a0089..5f96c33e 100644 --- a/plugins/puberty_simulator/src/lib.rs +++ b/plugins/puberty_simulator/src/lib.rs @@ -99,9 +99,8 @@ impl Default for PubertySimulator { impl Default for PubertySimulatorParams { fn default() -> Self { - let power_of_two_val2str = Arc::new(|value| format!("{}", 1 << value)); - let power_of_two_str2val = - Arc::new(|string: &str| string.parse().ok().map(|n: i32| (n as f32).log2() as i32)); + let power_of_two_val2str = formatters::i32_power_of_two(); + let power_of_two_str2val = formatters::from_i32_power_of_two(); Self { pitch_octaves: FloatParam::new( diff --git a/src/formatters.rs b/src/formatters.rs index 74db3fe1..5973cfce 100644 --- a/src/formatters.rs +++ b/src/formatters.rs @@ -6,3 +6,15 @@ use std::sync::Arc; pub fn f32_rounded(digits: usize) -> Arc String + Send + Sync> { Arc::new(move |x| format!("{:.digits$}", x)) } + +/// Format an order/power of two. Useful in conjunction with [`from_power_of_two()`] to limit +/// integer parameter ranges to be only powers of two. +pub fn i32_power_of_two() -> Arc String + Send + Sync> { + Arc::new(|value| format!("{}", 1 << value)) +} + +/// Parse a parameter input string to a power of two. Useful in conjunction with [`power_of_two()`] +/// to limit integer parameter ranges to be only powers of two. +pub fn from_i32_power_of_two() -> Arc Option + Send + Sync> { + Arc::new(|string| string.parse().ok().map(|n: i32| (n as f32).log2() as i32)) +}