[librsvg/update-clap] Fix parsing of --background=none



commit c6bf690df08f5db032cf3193875a2cda6a3a4dcd
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Sep 20 11:06:37 2022 -0500

    Fix parsing of --background=none

 src/bin/rsvg-convert.rs | 31 ++++++++++++++++++++-----------
 src/lib.rs              |  3 ++-
 2 files changed, 22 insertions(+), 12 deletions(-)
---
diff --git a/src/bin/rsvg-convert.rs b/src/bin/rsvg-convert.rs
index cc8220155..81139e8b7 100644
--- a/src/bin/rsvg-convert.rs
+++ b/src/bin/rsvg-convert.rs
@@ -1,4 +1,4 @@
-use clap::{ValueEnum, crate_version};
+use clap::{crate_version, ValueEnum};
 
 use gio::prelude::*;
 use gio::{Cancellable, FileCreateFlags, InputStream, OutputStream};
@@ -18,7 +18,7 @@ mod windows_imports {
 #[cfg(windows)]
 use self::windows_imports::*;
 
-use cssparser::{match_ignore_ascii_case, _cssparser_internal_to_lowercase};
+use cssparser::{_cssparser_internal_to_lowercase, match_ignore_ascii_case};
 
 use librsvg::rsvg_convert_only::{
     AspectRatio, CssLength, Dpi, Horizontal, LegacySize, Length, Normalize, NormalizeParams, Parse,
@@ -931,6 +931,7 @@ fn parse_args() -> Result<Converter, Error> {
                 .takes_value(true)
                 .value_name("color")
                 .value_parser(parse_background_color)
+                .default_value("none")
                 .help("Set the background color using a CSS color spec"),
         )
         .arg(
@@ -971,7 +972,9 @@ fn parse_args() -> Result<Converter, Error> {
 
     let matches = app.get_matches();
 
-    let format_str: &String = matches.get_one("format").expect("already provided default_value");
+    let format_str: &String = matches
+        .get_one("format")
+        .expect("already provided default_value");
 
     let format = match_ignore_ascii_case! {
         format_str,
@@ -1030,8 +1033,12 @@ fn parse_args() -> Result<Converter, Error> {
         (Some(w), Some(h)) => Some((w, h)),
     };
 
-    let dpi_x = *matches.get_one::<Resolution>("res_x").expect("already provided default_value");
-    let dpi_y = *matches.get_one::<Resolution>("res_y").expect("already provided default_value");
+    let dpi_x = *matches
+        .get_one::<Resolution>("res_x")
+        .expect("already provided default_value");
+    let dpi_y = *matches
+        .get_one::<Resolution>("res_y")
+        .expect("already provided default_value");
 
     let zoom: Option<ZoomFactor> = matches.get_one("zoom").copied();
     let zoom_x: Option<ZoomFactor> = matches.get_one("zoom_x").copied();
@@ -1052,7 +1059,9 @@ fn parse_args() -> Result<Converter, Error> {
         ));
     }
 
-    let keep_aspect_ratio = *matches.get_one("keep_aspect").expect("already provided default_value");
+    let keep_aspect_ratio = *matches
+        .get_one("keep_aspect")
+        .expect("already provided default_value");
 
     let export_id: Option<String> = matches.get_one::<String>("export_id").map(lookup_id);
 
@@ -1135,10 +1144,10 @@ impl<T> NotFound for Result<T, clap::Error> {
     }
 }
 
-fn parse_background_color(s: &str) -> Result<Color, String> {
+fn parse_background_color(s: &str) -> Result<Option<Color>, String> {
     match s {
-        "none" | "None" => Err(format!("argument not found: {}", s)),
-        _ => <Color as Parse>::parse_str(s).map_err(|_| {
+        "none" | "None" => Ok(None),
+        _ => <Color as Parse>::parse_str(s).map(Some).map_err(|_| {
             format!(
                 "Invalid value: The argument '{}' can not be parsed as a CSS color value",
                 s
@@ -1193,8 +1202,8 @@ mod color_tests {
     }
 
     #[test]
-    fn none_is_handled_as_not_found() {
-        assert!(parse_background_color("None").is_err());
+    fn none_is_handled_as_transparent() {
+        assert_eq!(parse_background_color("None").unwrap(), None,);
     }
 
     #[test]
diff --git a/src/lib.rs b/src/lib.rs
index 00f2bba5c..d93f4ffba 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -249,7 +249,8 @@ pub mod rsvg_convert_only {
     pub use crate::dpi::Dpi;
     pub use crate::error::ParseError;
     pub use crate::length::{
-        CssLength, Horizontal, Length, Normalize, NormalizeParams, Signed, ULength, Unsigned, Validate, 
Vertical,
+        CssLength, Horizontal, Length, Normalize, NormalizeParams, Signed, ULength, Unsigned,
+        Validate, Vertical,
     };
     pub use crate::parsers::{Parse, ParseValue};
     pub use crate::rect::Rect;


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