1
0
Fork 0

Trim whitespace and units when parsing parameters

This commit is contained in:
Robbert van der Helm 2022-03-08 18:53:15 +01:00
parent c3767831cd
commit 1a7ad0a7a0
4 changed files with 18 additions and 14 deletions

View file

@ -95,6 +95,7 @@ impl Param for BoolParam {
} }
fn string_to_normalized_value(&self, string: &str) -> Option<f32> { fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
let string = string.trim();
let value = match &self.string_to_value { let value = match &self.string_to_value {
Some(f) => f(string), Some(f) => f(string),
None => Some(string.eq_ignore_ascii_case("true") || string.eq_ignore_ascii_case("on")), 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 { fn set_from_string(&mut self, string: &str) -> bool {
let string = string.trim();
let value = match &self.string_to_value { let value = match &self.string_to_value {
Some(f) => f(string), Some(f) => f(string),
None => Some(string.eq_ignore_ascii_case("true") || string.eq_ignore_ascii_case("on")), None => Some(string.eq_ignore_ascii_case("true") || string.eq_ignore_ascii_case("on")),

View file

@ -200,9 +200,10 @@ impl Param for EnumParamInner {
} }
fn string_to_normalized_value(&self, string: &str) -> Option<f32> { fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
let string = string.trim();
self.variants self.variants
.iter() .iter()
.position(|n| n == &string) .position(|variant| variant == &string)
.map(|idx| self.preview_normalized(idx as i32)) .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 { 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) => { Some(idx) => {
self.inner.set_plain_value(idx as i32); self.inner.set_plain_value(idx as i32);
true true

View file

@ -146,9 +146,9 @@ impl Param for FloatParam {
fn string_to_normalized_value(&self, string: &str) -> Option<f32> { fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
let value = match &self.string_to_value { let value = match &self.string_to_value {
Some(f) => f(string), Some(f) => f(string.trim()),
// TODO: Check how Rust's parse function handles trailing garbage // In the CLAP wrapper the unit will be included, so make sure to handle that
None => string.parse().ok(), None => string.trim().trim_end_matches(self.unit).parse().ok(),
}?; }?;
Some(self.preview_normalized(value)) Some(self.preview_normalized(value))
@ -168,9 +168,9 @@ impl Param for FloatParam {
fn set_from_string(&mut self, string: &str) -> bool { fn set_from_string(&mut self, string: &str) -> bool {
let value = match &self.string_to_value { let value = match &self.string_to_value {
Some(f) => f(string), Some(f) => f(string.trim()),
// TODO: Check how Rust's parse function handles trailing garbage // In the CLAP wrapper the unit will be included, so make sure to handle that
None => string.parse().ok(), None => string.trim().trim_end_matches(self.unit).parse().ok(),
}; };
match value { match value {

View file

@ -129,9 +129,9 @@ impl Param for IntParam {
fn string_to_normalized_value(&self, string: &str) -> Option<f32> { fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
let value = match &self.string_to_value { let value = match &self.string_to_value {
Some(f) => f(string), Some(f) => f(string.trim()),
// TODO: Check how Rust's parse function handles trailing garbage // In the CLAP wrapper the unit will be included, so make sure to handle that
None => string.parse().ok(), None => string.trim().trim_end_matches(self.unit).parse().ok(),
}?; }?;
Some(self.preview_normalized(value)) Some(self.preview_normalized(value))
@ -147,9 +147,9 @@ impl Param for IntParam {
fn set_from_string(&mut self, string: &str) -> bool { fn set_from_string(&mut self, string: &str) -> bool {
let value = match &self.string_to_value { let value = match &self.string_to_value {
Some(f) => f(string), Some(f) => f(string.trim()),
// TODO: Check how Rust's parse function handles trailing garbage // In the CLAP wrapper the unit will be included, so make sure to handle that
None => string.parse().ok(), None => string.trim().trim_end_matches(self.unit).parse().ok(),
}; };
match value { match value {