1
0
Fork 0

Move order conversion to formatters module

This commit is contained in:
Robbert van der Helm 2022-03-08 18:30:06 +01:00
parent bb341fdf50
commit 24f3593de0
2 changed files with 14 additions and 3 deletions

View file

@ -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(

View file

@ -6,3 +6,15 @@ use std::sync::Arc;
pub fn f32_rounded(digits: usize) -> Arc<dyn Fn(f32) -> 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<dyn Fn(i32) -> 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<dyn Fn(&str) -> Option<i32> + Send + Sync> {
Arc::new(|string| string.parse().ok().map(|n: i32| (n as f32).log2() as i32))
}