[librsvg: 2/9] Replace duplicated num-or-percentage parse functions with helpers
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 2/9] Replace duplicated num-or-percentage parse functions with helpers
- Date: Wed, 26 May 2021 15:04:44 +0000 (UTC)
commit ed210a28f26fa512d04c9aeae93f6a71db133595
Author: John Ledbetter <john ledbetter gmail com>
Date: Wed May 19 15:32:01 2021 -0400
Replace duplicated num-or-percentage parse functions with helpers
Introduces `parse_num_or_percentage{,_clamped}` helpers, which are
used by many different filter function parser functions.
src/filter_func.rs | 40 ++++++++++++++++++++--------------------
1 file changed, 20 insertions(+), 20 deletions(-)
---
diff --git a/src/filter_func.rs b/src/filter_func.rs
index 8ce3725e..4ac29065 100644
--- a/src/filter_func.rs
+++ b/src/filter_func.rs
@@ -91,46 +91,46 @@ fn parse_blur<'i>(parser: &mut Parser<'i, '_>) -> Result<FilterFunction, ParseEr
}))
}
-#[allow(clippy::unnecessary_wraps)]
-fn parse_grayscale<'i>(parser: &mut Parser<'i, '_>) -> Result<FilterFunction, ParseError<'i>> {
- let proportion = match parser.try_parse(|p| NumberOrPercentage::parse(p)) {
+/// Reads an optional number or percentage from the parser.
+/// Negative numbers are not allowed.
+fn parse_num_or_percentage<'i>(parser: &mut Parser<'i, '_>) -> Option<f64> {
+ match parser.try_parse(|p| NumberOrPercentage::parse(p)) {
Ok(NumberOrPercentage { value }) if value < 0.0 => None,
- Ok(NumberOrPercentage { value }) => Some(value.clamp(0.0, 1.0)),
+ Ok(NumberOrPercentage { value }) => Some(value),
Err(_) => None,
- };
+ }
+}
+
+/// Reads an optional number or percentage from the parser, returning a value clamped to [0, 1].
+/// Negative numbers are not allowed.
+fn parse_num_or_percentage_clamped<'i>(parser: &mut Parser<'i, '_>) -> Option<f64> {
+ parse_num_or_percentage(parser).map(|value| value.clamp(0.0, 1.0))
+}
+
+#[allow(clippy::unnecessary_wraps)]
+fn parse_grayscale<'i>(parser: &mut Parser<'i, '_>) -> Result<FilterFunction, ParseError<'i>> {
+ let proportion = parse_num_or_percentage_clamped(parser);
Ok(FilterFunction::Grayscale(Grayscale { proportion }))
}
#[allow(clippy::unnecessary_wraps)]
fn parse_opacity<'i>(parser: &mut Parser<'i, '_>) -> Result<FilterFunction, ParseError<'i>> {
- let proportion = match parser.try_parse(|p| NumberOrPercentage::parse(p)) {
- Ok(NumberOrPercentage { value }) if value < 0.0 => None,
- Ok(NumberOrPercentage { value }) => Some(value.clamp(0.0, 1.0)),
- Err(_) => None,
- };
+ let proportion = parse_num_or_percentage_clamped(parser);
Ok(FilterFunction::Opacity(Opacity { proportion }))
}
#[allow(clippy::unnecessary_wraps)]
fn parse_saturate<'i>(parser: &mut Parser<'i, '_>) -> Result<FilterFunction, ParseError<'i>> {
- let proportion = match parser.try_parse(|p| NumberOrPercentage::parse(p)) {
- Ok(NumberOrPercentage { value }) if value < 0.0 => None,
- Ok(NumberOrPercentage { value }) => Some(value),
- Err(_) => None,
- };
+ let proportion = parse_num_or_percentage(parser);
Ok(FilterFunction::Saturate(Saturate { proportion }))
}
#[allow(clippy::unnecessary_wraps)]
fn parse_sepia<'i>(parser: &mut Parser<'i, '_>) -> Result<FilterFunction, ParseError<'i>> {
- let proportion = match parser.try_parse(|p| NumberOrPercentage::parse(p)) {
- Ok(NumberOrPercentage { value }) if value < 0.0 => None,
- Ok(NumberOrPercentage { value }) => Some(value.clamp(0.0, 1.0)),
- Err(_) => None,
- };
+ let proportion = parse_num_or_percentage_clamped(parser);
Ok(FilterFunction::Sepia(Sepia { proportion }))
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]