[librsvg/rustify-rsvg-convert] rsvg-convert: avoid unwrap panic in input handling



commit 3365731fb19041fc809946101e88daaadbdf7527
Author: Paolo Borelli <pborelli gnome org>
Date:   Sat Jan 9 11:22:48 2021 +0100

    rsvg-convert: avoid unwrap panic in input handling
    
    Instead of unwrapping, stop the iterator.
    This however still silently ignores errors though.

 src/bin/rsvg-convert/input.rs | 33 +++++++++++++--------------------
 1 file changed, 13 insertions(+), 20 deletions(-)
---
diff --git a/src/bin/rsvg-convert/input.rs b/src/bin/rsvg-convert/input.rs
index 2e014288..5f01a9d2 100644
--- a/src/bin/rsvg-convert/input.rs
+++ b/src/bin/rsvg-convert/input.rs
@@ -49,24 +49,6 @@ pub struct Item {
 }
 
 impl Item {
-    fn from_file(file: gio::File) -> Self {
-        Self {
-            // TODO: unwrap
-            stream: Stream::File(file.read(None::<&gio::Cancellable>).unwrap()),
-            file: Some(file),
-        }
-    }
-    fn from_path(path: &PathBuf) -> Self {
-        Self::from_file(gio::File::new_for_path(path))
-    }
-
-    fn from_unix_stream(stream: gio::UnixInputStream) -> Self {
-        Self {
-            stream: Stream::Unix(stream),
-            file: None,
-        }
-    }
-
     pub fn stream(&self) -> &gio::InputStream {
         self.stream.deref()
     }
@@ -90,8 +72,19 @@ impl Iterator for Input<'_> {
 
     fn next(&mut self) -> Option<Self::Item> {
         match self {
-            Input::Paths(paths) => paths.next().map(Item::from_path),
-            Input::Stdin(iter) => iter.next().map(Item::from_unix_stream),
+            Input::Paths(paths) => paths.next().and_then(|p| {
+                let file = gio::File::new_for_path(p);
+                let stream = file.read(None::<&gio::Cancellable>).ok()?;
+                Some(Item {
+                    stream: Stream::File(stream),
+                    file: Some(file),
+                })
+            }),
+
+            Input::Stdin(iter) => iter.next().map(|s| Item {
+                stream: Stream::Unix(s),
+                file: None,
+            }),
         }
     }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]