[librsvg/rustify-rsvg-convert: 22/78] rsvg-convert: Improve the error handling




commit 3054f96a1f1a51b17af7e137d899df281d55c06d
Author: Sven Neumann <sven svenfoo org>
Date:   Mon Nov 2 11:10:39 2020 +0100

    rsvg-convert: Improve the error handling

 src/bin/rsvg-convert/cli.rs   |  6 +++++-
 src/bin/rsvg-convert/input.rs | 10 ++++++++++
 src/bin/rsvg-convert/main.rs  | 44 ++++++++++++++++++++++++++++++++++---------
 3 files changed, 50 insertions(+), 10 deletions(-)
---
diff --git a/src/bin/rsvg-convert/cli.rs b/src/bin/rsvg-convert/cli.rs
index 27a4f439..5567dd43 100644
--- a/src/bin/rsvg-convert/cli.rs
+++ b/src/bin/rsvg-convert/cli.rs
@@ -25,7 +25,7 @@ pub struct Args {
     pub width: Option<u32>,
     pub height: Option<u32>,
     pub format: Format,
-    pub export_id: Option<String>,
+    export_id: Option<String>,
     pub keep_aspect_ratio: bool,
     pub background_color: Option<Color>,
     pub stylesheet: Option<PathBuf>,
@@ -241,6 +241,10 @@ impl Args {
         Ok(args)
     }
 
+    pub fn export_id(&self) -> Option<&str> {
+        self.export_id.as_deref()
+    }
+
     pub fn output(&self) -> Option<&Path> {
         self.output.as_deref()
     }
diff --git a/src/bin/rsvg-convert/input.rs b/src/bin/rsvg-convert/input.rs
index 0e7637f4..468dc4f3 100644
--- a/src/bin/rsvg-convert/input.rs
+++ b/src/bin/rsvg-convert/input.rs
@@ -2,6 +2,7 @@
 
 use core::ops::Deref;
 use gio::FileExt;
+use std::fmt;
 use std::os::unix::io::RawFd;
 use std::path::PathBuf;
 
@@ -82,6 +83,15 @@ impl Item {
     }
 }
 
+impl fmt::Display for Item {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match &self.file {
+            Some(file) => file.get_path().unwrap().display().fmt(f),
+            None => "stdin".fmt(f),
+        }
+    }
+}
+
 impl Iterator for Input<'_> {
     type Item = Item;
 
diff --git a/src/bin/rsvg-convert/main.rs b/src/bin/rsvg-convert/main.rs
index 1c12fb55..27997250 100644
--- a/src/bin/rsvg-convert/main.rs
+++ b/src/bin/rsvg-convert/main.rs
@@ -6,12 +6,20 @@ mod input;
 mod output;
 mod surface;
 
-use librsvg::{CairoRenderer, Loader};
+use librsvg::{CairoRenderer, Loader, RenderingError};
 
 use crate::cli::Args;
 use crate::output::Stream;
 use crate::surface::Surface;
 
+macro_rules! exit {
+    () => (exit!("Error"));
+    ($($arg:tt)*) => ({
+        std::eprintln!("{}", std::format_args!($($arg)*));
+        std::process::exit(1);
+    })
+}
+
 fn load_stylesheet(args: &Args) -> std::io::Result<Option<String>> {
     match args.stylesheet {
         Some(ref filename) => std::fs::read_to_string(filename).map(Some),
@@ -22,7 +30,9 @@ fn load_stylesheet(args: &Args) -> std::io::Result<Option<String>> {
 fn main() {
     let args = Args::new().unwrap_or_else(|e| e.exit());
 
-    let stylesheet = load_stylesheet(&args).expect("could not load stylesheet");
+    let stylesheet =
+        load_stylesheet(&args).unwrap_or_else(|e| exit!("Error reading stylesheet: {}", e));
+
     let mut target = None;
 
     for input in args.input() {
@@ -30,10 +40,12 @@ fn main() {
             .with_unlimited_size(args.unlimited)
             .keep_image_data(args.keep_image_data)
             .read_stream(input.stream(), input.file(), None::<&gio::Cancellable>)
-            .expect("error loading SVG file");
+            .unwrap_or_else(|e| exit!("Error reading SVG {}: {}", input, e));
 
         if let Some(ref css) = stylesheet {
-            handle.set_stylesheet(&css).unwrap();
+            handle
+                .set_stylesheet(&css)
+                .unwrap_or_else(|e| exit!("Error applying stylesheet: {}", e));
         }
 
         let renderer = CairoRenderer::new(&handle).with_dpi(args.dpi_x, args.dpi_y);
@@ -41,8 +53,13 @@ fn main() {
         if target.is_none() {
             target = match renderer.intrinsic_size_in_pixels() {
                 Some((width, height)) => {
-                    let output = Stream::new(args.output()).unwrap();
-                    Some(Surface::new(args.format, width, height, output).unwrap())
+                    let output = Stream::new(args.output())
+                        .unwrap_or_else(|e| exit!("Error opening output: {}", e));
+
+                    Some(
+                        Surface::new(args.format, width, height, output)
+                            .unwrap_or_else(|e| exit!("Error creating output surface: {}", e)),
+                    )
                 }
                 None => None,
             };
@@ -50,12 +67,21 @@ fn main() {
 
         if let Some(ref surface) = target {
             surface
-                .render(&renderer, args.export_id.as_deref())
-                .unwrap();
+                .render(&renderer, args.export_id())
+                .unwrap_or_else(|e| match e {
+                    RenderingError::InvalidId(_) => exit!(
+                        "File {} does not have an object with id \"{}\")",
+                        input,
+                        args.export_id().unwrap()
+                    ),
+                    _ => exit!("Error rendering SVG {}: {}", input, e),
+                });
         }
     }
 
     if let Some(ref mut surface) = target {
-        surface.finish().unwrap();
+        surface
+            .finish()
+            .unwrap_or_else(|e| exit!("Error saving output: {}", e));
     }
 }


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