Trim whitespace and units when parsing parameters
This commit is contained in:
parent
c3767831cd
commit
1a7ad0a7a0
|
@ -95,6 +95,7 @@ impl Param for BoolParam {
|
|||
}
|
||||
|
||||
fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
|
||||
let string = string.trim();
|
||||
let value = match &self.string_to_value {
|
||||
Some(f) => f(string),
|
||||
None => Some(string.eq_ignore_ascii_case("true") || string.eq_ignore_ascii_case("on")),
|
||||
|
@ -116,6 +117,7 @@ impl Param for BoolParam {
|
|||
}
|
||||
|
||||
fn set_from_string(&mut self, string: &str) -> bool {
|
||||
let string = string.trim();
|
||||
let value = match &self.string_to_value {
|
||||
Some(f) => f(string),
|
||||
None => Some(string.eq_ignore_ascii_case("true") || string.eq_ignore_ascii_case("on")),
|
||||
|
|
|
@ -200,9 +200,10 @@ impl Param for EnumParamInner {
|
|||
}
|
||||
|
||||
fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
|
||||
let string = string.trim();
|
||||
self.variants
|
||||
.iter()
|
||||
.position(|n| n == &string)
|
||||
.position(|variant| variant == &string)
|
||||
.map(|idx| self.preview_normalized(idx as i32))
|
||||
}
|
||||
|
||||
|
@ -215,7 +216,8 @@ impl Param for EnumParamInner {
|
|||
}
|
||||
|
||||
fn set_from_string(&mut self, string: &str) -> bool {
|
||||
match self.variants.iter().position(|n| n == &string) {
|
||||
let string = string.trim();
|
||||
match self.variants.iter().position(|variant| variant == &string) {
|
||||
Some(idx) => {
|
||||
self.inner.set_plain_value(idx as i32);
|
||||
true
|
||||
|
|
|
@ -146,9 +146,9 @@ impl Param for FloatParam {
|
|||
|
||||
fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
|
||||
let value = match &self.string_to_value {
|
||||
Some(f) => f(string),
|
||||
// TODO: Check how Rust's parse function handles trailing garbage
|
||||
None => string.parse().ok(),
|
||||
Some(f) => f(string.trim()),
|
||||
// In the CLAP wrapper the unit will be included, so make sure to handle that
|
||||
None => string.trim().trim_end_matches(self.unit).parse().ok(),
|
||||
}?;
|
||||
|
||||
Some(self.preview_normalized(value))
|
||||
|
@ -168,9 +168,9 @@ impl Param for FloatParam {
|
|||
|
||||
fn set_from_string(&mut self, string: &str) -> bool {
|
||||
let value = match &self.string_to_value {
|
||||
Some(f) => f(string),
|
||||
// TODO: Check how Rust's parse function handles trailing garbage
|
||||
None => string.parse().ok(),
|
||||
Some(f) => f(string.trim()),
|
||||
// In the CLAP wrapper the unit will be included, so make sure to handle that
|
||||
None => string.trim().trim_end_matches(self.unit).parse().ok(),
|
||||
};
|
||||
|
||||
match value {
|
||||
|
|
|
@ -129,9 +129,9 @@ impl Param for IntParam {
|
|||
|
||||
fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
|
||||
let value = match &self.string_to_value {
|
||||
Some(f) => f(string),
|
||||
// TODO: Check how Rust's parse function handles trailing garbage
|
||||
None => string.parse().ok(),
|
||||
Some(f) => f(string.trim()),
|
||||
// In the CLAP wrapper the unit will be included, so make sure to handle that
|
||||
None => string.trim().trim_end_matches(self.unit).parse().ok(),
|
||||
}?;
|
||||
|
||||
Some(self.preview_normalized(value))
|
||||
|
@ -147,9 +147,9 @@ impl Param for IntParam {
|
|||
|
||||
fn set_from_string(&mut self, string: &str) -> bool {
|
||||
let value = match &self.string_to_value {
|
||||
Some(f) => f(string),
|
||||
// TODO: Check how Rust's parse function handles trailing garbage
|
||||
None => string.parse().ok(),
|
||||
Some(f) => f(string.trim()),
|
||||
// In the CLAP wrapper the unit will be included, so make sure to handle that
|
||||
None => string.trim().trim_end_matches(self.unit).parse().ok(),
|
||||
};
|
||||
|
||||
match value {
|
||||
|
|
Loading…
Reference in a new issue