2022-01-29 00:33:29 +11:00
|
|
|
//! Convenience functions for formatting and parsing parameter values in common formats.
|
|
|
|
|
2022-02-01 06:44:10 +11:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-01-29 00:33:29 +11:00
|
|
|
/// Round an `f32` value to always have a specific number of decimal digits.
|
2022-02-13 03:19:52 +11:00
|
|
|
pub fn f32_rounded(digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
|
|
|
|
Arc::new(move |x| format!("{:.digits$}", x))
|
2022-01-29 00:33:29 +11:00
|
|
|
}
|
2022-03-09 04:30:06 +11:00
|
|
|
|
|
|
|
/// 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))
|
|
|
|
}
|