[librsvg: 9/19] Avoid using match for destructuring a single pattern
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 9/19] Avoid using match for destructuring a single pattern
- Date: Fri, 21 Aug 2020 18:30:17 +0000 (UTC)
commit c75eb46707618a9304c877664fbfb3d293bd0234
Author: Dunja Lalic <dunja lalic gmail com>
Date: Thu Aug 20 23:42:45 2020 +0200
Avoid using match for destructuring a single pattern
See https://rust-lang.github.io/rust-clippy/master/index.html#single_match
rsvg_internals/src/element.rs | 30 ++++++++++++++------------
rsvg_internals/src/filter.rs | 11 +++++-----
rsvg_internals/src/filters/gaussian_blur.rs | 32 +++++++++++++---------------
rsvg_internals/src/filters/lighting.rs | 33 +++++++++++++++--------------
rsvg_internals/src/filters/merge.rs | 10 ++++-----
rsvg_internals/src/filters/mod.rs | 10 ++++-----
rsvg_internals/src/gradient.rs | 16 ++++++--------
rsvg_internals/src/handle.rs | 11 ++++------
rsvg_internals/src/structure.rs | 11 +++++-----
rsvg_internals/src/text.rs | 18 ++++++----------
10 files changed, 86 insertions(+), 96 deletions(-)
---
diff --git a/rsvg_internals/src/element.rs b/rsvg_internals/src/element.rs
index 1f87202a5..809d1c360 100644
--- a/rsvg_internals/src/element.rs
+++ b/rsvg_internals/src/element.rs
@@ -143,24 +143,26 @@ impl<T: SetAttributes + Draw> ElementInner<T> {
}
fn save_style_attribute(&mut self, pbag: &PropertyBag<'_>) {
- for (attr, value) in pbag.iter() {
- match attr.expanded() {
- expanded_name!("", "style") => self.style_attr.push_str(value),
- _ => (),
- }
+ let result = pbag
+ .iter()
+ .find(|(attr, _)| attr.expanded() == expanded_name!("", "style"))
+ .map(|(_, value)| value);
+ if let Some(value) = result {
+ self.style_attr.push_str(value);
}
}
fn set_transform_attribute(&mut self, pbag: &PropertyBag<'_>) -> Result<(), ElementError> {
- for (attr, value) in pbag.iter() {
- match attr.expanded() {
- expanded_name!("", "transform") => {
- return Transform::parse_str(value).attribute(attr).map(|affine| {
- self.transform = affine;
- });
- }
- _ => (),
- }
+ let result = pbag
+ .iter()
+ .find(|(attr, _)| attr.expanded() == expanded_name!("", "transform"))
+ .map(|(attr, value)| {
+ Transform::parse_str(value).attribute(attr).map(|affine| {
+ self.transform = affine;
+ })
+ });
+ if let Some(transform_result) = result {
+ return transform_result;
}
Ok(())
diff --git a/rsvg_internals/src/filter.rs b/rsvg_internals/src/filter.rs
index 9d4034fb4..74d6b1312 100644
--- a/rsvg_internals/src/filter.rs
+++ b/rsvg_internals/src/filter.rs
@@ -113,11 +113,12 @@ impl Filter {
impl SetAttributes for Filter {
fn set_attributes(&mut self, pbag: &PropertyBag<'_>) -> ElementResult {
// Parse filterUnits first as it affects x, y, width, height checks.
- for (attr, value) in pbag.iter() {
- match attr.expanded() {
- expanded_name!("", "filterUnits") => self.filterunits = attr.parse(value)?,
- _ => (),
- }
+ let result = pbag
+ .iter()
+ .find(|(attr, _)| attr.expanded() == expanded_name!("", "filterUnits"))
+ .and_then(|(attr, value)| attr.parse(value).ok());
+ if let Some(filter_units) = result {
+ self.filterunits = filter_units
}
// With ObjectBoundingBox, only fractions and percents are allowed.
diff --git a/rsvg_internals/src/filters/gaussian_blur.rs b/rsvg_internals/src/filters/gaussian_blur.rs
index de03dcc96..877141a72 100644
--- a/rsvg_internals/src/filters/gaussian_blur.rs
+++ b/rsvg_internals/src/filters/gaussian_blur.rs
@@ -45,23 +45,21 @@ impl Default for FeGaussianBlur {
impl SetAttributes for FeGaussianBlur {
fn set_attributes(&mut self, pbag: &PropertyBag<'_>) -> ElementResult {
self.base.set_attributes(pbag)?;
-
- for (attr, value) in pbag.iter() {
- match attr.expanded() {
- expanded_name!("", "stdDeviation") => {
- let NumberOptionalNumber(x, y) =
- attr.parse_and_validate(value, |v: NumberOptionalNumber<f64>| {
- if v.0 >= 0.0 && v.1 >= 0.0 {
- Ok(v)
- } else {
- Err(ValueErrorKind::value_error("values can't be negative"))
- }
- })?;
-
- self.std_deviation = (x, y);
- }
- _ => (),
- }
+ let result = pbag
+ .iter()
+ .find(|(attr, _)| attr.expanded() == expanded_name!("", "stdDeviation"))
+ .and_then(|(attr, value)| {
+ attr.parse_and_validate(value, |v: NumberOptionalNumber<f64>| {
+ if v.0 >= 0.0 && v.1 >= 0.0 {
+ Ok(v)
+ } else {
+ Err(ValueErrorKind::value_error("values can't be negative"))
+ }
+ })
+ .ok()
+ });
+ if let Some(tuple) = result {
+ self.std_deviation = (tuple.0, tuple.1);
}
Ok(())
diff --git a/rsvg_internals/src/filters/lighting.rs b/rsvg_internals/src/filters/lighting.rs
index 848246548..80a7f2a86 100644
--- a/rsvg_internals/src/filters/lighting.rs
+++ b/rsvg_internals/src/filters/lighting.rs
@@ -303,22 +303,23 @@ impl Default for FeDiffuseLighting {
impl SetAttributes for FeDiffuseLighting {
fn set_attributes(&mut self, pbag: &PropertyBag<'_>) -> ElementResult {
self.common.set_attributes(pbag)?;
-
- for (attr, value) in pbag.iter() {
- match attr.expanded() {
- expanded_name!("", "diffuseConstant") => {
- self.diffuse_constant = attr.parse_and_validate(value, |x| {
- if x >= 0.0 {
- Ok(x)
- } else {
- Err(ValueErrorKind::value_error(
- "diffuseConstant can't be negative",
- ))
- }
- })?;
- }
- _ => (),
- }
+ let result = pbag
+ .iter()
+ .find(|(attr, _)| attr.expanded() == expanded_name!("", "diffuseConstant"))
+ .and_then(|(attr, value)| {
+ attr.parse_and_validate(value, |x| {
+ if x >= 0.0 {
+ Ok(x)
+ } else {
+ Err(ValueErrorKind::value_error(
+ "diffuseConstant can't be negative",
+ ))
+ }
+ })
+ .ok()
+ });
+ if let Some(diffuse_constant) = result {
+ self.diffuse_constant = diffuse_constant;
}
Ok(())
diff --git a/rsvg_internals/src/filters/merge.rs b/rsvg_internals/src/filters/merge.rs
index 39c221d26..915fa1999 100644
--- a/rsvg_internals/src/filters/merge.rs
+++ b/rsvg_internals/src/filters/merge.rs
@@ -42,12 +42,10 @@ impl SetAttributes for FeMerge {
impl SetAttributes for FeMergeNode {
#[inline]
fn set_attributes(&mut self, pbag: &PropertyBag<'_>) -> ElementResult {
- for (attr, value) in pbag.iter() {
- match attr.expanded() {
- expanded_name!("", "in") => self.in_ = Some(attr.parse(value)?),
- _ => (),
- }
- }
+ self.in_ = pbag
+ .iter()
+ .find(|(attr, _)| attr.expanded() == expanded_name!("", "in"))
+ .and_then(|(attr, value)| attr.parse(value).ok());
Ok(())
}
diff --git a/rsvg_internals/src/filters/mod.rs b/rsvg_internals/src/filters/mod.rs
index faccb899f..d59a661a3 100644
--- a/rsvg_internals/src/filters/mod.rs
+++ b/rsvg_internals/src/filters/mod.rs
@@ -248,12 +248,10 @@ impl SetAttributes for PrimitiveWithInput {
fn set_attributes(&mut self, pbag: &PropertyBag<'_>) -> ElementResult {
self.base.set_attributes(pbag)?;
- for (attr, value) in pbag.iter() {
- match attr.expanded() {
- expanded_name!("", "in") => self.in_ = Some(attr.parse(value)?),
- _ => (),
- }
- }
+ self.in_ = pbag
+ .iter()
+ .find(|(attr, _)| attr.expanded() == expanded_name!("", "in"))
+ .and_then(|(attr, value)| attr.parse(value).ok());
Ok(())
}
diff --git a/rsvg_internals/src/gradient.rs b/rsvg_internals/src/gradient.rs
index 9e6fe03c4..b133bf555 100644
--- a/rsvg_internals/src/gradient.rs
+++ b/rsvg_internals/src/gradient.rs
@@ -96,15 +96,13 @@ fn validate_offset(length: Length<Both>) -> Result<Length<Both>, ValueErrorKind>
impl SetAttributes for Stop {
fn set_attributes(&mut self, pbag: &PropertyBag<'_>) -> ElementResult {
- for (attr, value) in pbag.iter() {
- match attr.expanded() {
- expanded_name!("", "offset") => {
- self.offset = attr
- .parse_and_validate(value, validate_offset)
- .map(|l| UnitInterval::clamp(l.length))?
- }
- _ => (),
- }
+ let result = pbag
+ .iter()
+ .find(|(attr, _)| attr.expanded() == expanded_name!("", "offset"))
+ .and_then(|(attr, value)| attr.parse_and_validate(value, validate_offset).ok())
+ .map(|l| UnitInterval::clamp(l.length));
+ if let Some(offset) = result {
+ self.offset = offset
}
Ok(())
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index 42b24de13..d776cb964 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -148,14 +148,11 @@ impl Handle {
let cascaded = CascadedValues::new_from_node(&node);
let values = cascaded.get();
- match *node.borrow_element() {
- Element::Svg(ref svg) => {
- if let Some((w, h)) = svg.get_size(&values, dpi) {
- let rect = Rect::from_size(w, h);
- return Ok((rect, rect));
- }
+ if let Element::Svg(ref svg) = *node.borrow_element() {
+ if let Some((w, h)) = svg.get_size(&values, dpi) {
+ let rect = Rect::from_size(w, h);
+ return Ok((rect, rect));
}
- _ => (),
}
}
diff --git a/rsvg_internals/src/structure.rs b/rsvg_internals/src/structure.rs
index b34cc2f3b..b984d38c3 100644
--- a/rsvg_internals/src/structure.rs
+++ b/rsvg_internals/src/structure.rs
@@ -396,11 +396,12 @@ impl ClipPath {
impl SetAttributes for ClipPath {
fn set_attributes(&mut self, pbag: &PropertyBag<'_>) -> ElementResult {
- for (attr, value) in pbag.iter() {
- match attr.expanded() {
- expanded_name!("", "clipPathUnits") => self.units = attr.parse(value)?,
- _ => (),
- }
+ let result = pbag
+ .iter()
+ .find(|(attr, _)| attr.expanded() == expanded_name!("", "clipPathUnits"))
+ .and_then(|(attr, value)| attr.parse(value).ok());
+ if let Some(units) = result {
+ self.units = units
}
Ok(())
diff --git a/rsvg_internals/src/text.rs b/rsvg_internals/src/text.rs
index 165a0adfa..61d1e0f11 100644
--- a/rsvg_internals/src/text.rs
+++ b/rsvg_internals/src/text.rs
@@ -725,17 +725,13 @@ fn extract_chars_children_to_chunks_recursively(
impl SetAttributes for TRef {
fn set_attributes(&mut self, pbag: &PropertyBag<'_>) -> ElementResult {
- for (attr, value) in pbag.iter() {
- match attr.expanded() {
- // Unlike other elements which use `href` in SVG2 versus `xlink:href` in SVG1.1,
- // the <tref> element got removed in SVG2. So, here we still use a match
- // against the full namespaced version of the attribute.
- expanded_name!(xlink "href") => {
- self.link = Some(Fragment::parse(value).attribute(attr)?)
- }
- _ => (),
- }
- }
+ self.link = pbag
+ .iter()
+ .find(|(attr, _)| attr.expanded() == expanded_name!(xlink "href"))
+ // Unlike other elements which use `href` in SVG2 versus `xlink:href` in SVG1.1,
+ // the <tref> element got removed in SVG2. So, here we still use a match
+ // against the full namespaced version of the attribute.
+ .and_then(|(attr, value)| Fragment::parse(value).attribute(attr).ok());
Ok(())
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]