[librsvg/rustify-rsvg-convert: 28/78] rsvg-convert: Validate resolution and zoom values




commit 705b0d319df80f62d2a2a3e81beab23cf694d995
Author: Sven Neumann <sven svenfoo org>
Date:   Thu Nov 5 19:28:09 2020 +0100

    rsvg-convert: Validate resolution and zoom values

 src/bin/rsvg-convert/cli.rs | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
---
diff --git a/src/bin/rsvg-convert/cli.rs b/src/bin/rsvg-convert/cli.rs
index a5f21b94..e59822ed 100644
--- a/src/bin/rsvg-convert/cli.rs
+++ b/src/bin/rsvg-convert/cli.rs
@@ -50,6 +50,7 @@ impl Args {
                     .takes_value(true)
                     .value_name("float")
                     .default_value("90")
+                    .validator(is_valid_resolution)
                     .help("Pixels per inch"),
             )
             .arg(
@@ -59,6 +60,7 @@ impl Args {
                     .takes_value(true)
                     .value_name("float")
                     .default_value("90")
+                    .validator(is_valid_resolution)
                     .help("Pixels per inch"),
             )
             .arg(
@@ -68,6 +70,7 @@ impl Args {
                     .takes_value(true)
                     .value_name("float")
                     .conflicts_with("zoom")
+                    .validator(is_valid_zoom_factor)
                     .help("Horizontal zoom factor"),
             )
             .arg(
@@ -77,6 +80,7 @@ impl Args {
                     .takes_value(true)
                     .value_name("float")
                     .conflicts_with("zoom")
+                    .validator(is_valid_zoom_factor)
                     .help("Vertical zoom factor"),
             )
             .arg(
@@ -85,6 +89,7 @@ impl Args {
                     .long("zoom")
                     .takes_value(true)
                     .value_name("float")
+                    .validator(is_valid_zoom_factor)
                     .help("Zoom factor"),
             )
             .arg(
@@ -261,6 +266,22 @@ impl Args {
     }
 }
 
+fn is_valid_resolution(v: String) -> Result<(), String> {
+    match v.parse::<f64>() {
+        Ok(res) if res > 0.0 => Ok(()),
+        Ok(_) => Err(String::from("Invalid resolution")),
+        Err(e) => Err(format!("{}", e)),
+    }
+}
+
+fn is_valid_zoom_factor(v: String) -> Result<(), String> {
+    match v.parse::<f64>() {
+        Ok(res) if res > 0.0 => Ok(()),
+        Ok(_) => Err(String::from("Invalid zoom factor")),
+        Err(e) => Err(format!("{}", e)),
+    }
+}
+
 trait NotFound {
     type Ok;
     type Error;


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