[librsvg: 1/2] Return a meaningful error instead of using unit as an error type
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 1/2] Return a meaningful error instead of using unit as an error type
- Date: Wed, 3 Feb 2021 02:42:01 +0000 (UTC)
commit 5f76e38afaf0762fe73ba07a035473a21bccf369
Author: Sven Neumann <sven svenfoo org>
Date: Tue Feb 2 19:58:46 2021 +0100
Return a meaningful error instead of using unit as an error type
https://rust-lang.github.io/rust-clippy/master/index.html#result_unit_err
src/aspect_ratio.rs | 18 +++++++++++++++---
src/drawing_ctx.rs | 2 +-
2 files changed, 16 insertions(+), 4 deletions(-)
---
diff --git a/src/aspect_ratio.rs b/src/aspect_ratio.rs
index b5f5be68..b2e9fa64 100644
--- a/src/aspect_ratio.rs
+++ b/src/aspect_ratio.rs
@@ -16,6 +16,7 @@
//! [spec]: https://www.w3.org/TR/SVG/coords.html#PreserveAspectRatioAttribute
use cssparser::{BasicParseError, Parser};
+use std::fmt;
use std::ops::Deref;
use crate::error::*;
@@ -79,6 +80,17 @@ struct Align {
fit: FitMode,
}
+#[derive(Debug, PartialEq, Eq)]
+pub struct NonInvertibleTransform;
+
+impl fmt::Display for NonInvertibleTransform {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "Not invertible")
+ }
+}
+
+impl std::error::Error for NonInvertibleTransform {}
+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct AspectRatio {
defer: bool,
@@ -148,7 +160,7 @@ impl AspectRatio {
&self,
vbox: Option<ViewBox>,
viewport: &Rect,
- ) -> Result<Option<Transform>, ()> {
+ ) -> Result<Option<Transform>, NonInvertibleTransform> {
// width or height set to 0 disables rendering of the element
// https://www.w3.org/TR/SVG/struct.html#SVGElementWidthAttribute
// https://www.w3.org/TR/SVG/struct.html#UseElementWidthAttribute
@@ -179,7 +191,7 @@ impl AspectRatio {
if transform.is_invertible() {
Ok(Some(transform))
} else {
- Err(())
+ Err(NonInvertibleTransform)
}
}
}
@@ -447,6 +459,6 @@ mod tests {
&Rect::new(1.0, 1.0, 2.0, 2.0),
);
- assert_eq!(t, Err(()));
+ assert_eq!(t, Err(NonInvertibleTransform));
}
}
diff --git a/src/drawing_ctx.rs b/src/drawing_ctx.rs
index 5a2d7a3f..7d9c01e8 100644
--- a/src/drawing_ctx.rs
+++ b/src/drawing_ctx.rs
@@ -409,7 +409,7 @@ impl DrawingCtx {
preserve_aspect_ratio
.viewport_to_viewbox_transform(vbox, &viewport)
- .unwrap_or_else(|_e: ()| {
+ .unwrap_or_else(|_e| {
match vbox {
None => unreachable!(
"viewport_to_viewbox_transform only returns errors when vbox != None"
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]