[librsvg/rustify-rsvg-convert: 60/78] rsvg-convert: use PathOrUrl instead of plain PathBuf




commit f87bba8f6623ee9cafcd9ee80b147665c21ab8cd
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Jan 21 13:48:24 2021 -0600

    rsvg-convert: use PathOrUrl instead of plain PathBuf
    
    This restores the ability to use URLs on the command line.

 src/bin/rsvg-convert.rs           | 15 +++++++++------
 tests/src/cmdline/rsvg_convert.rs | 17 +++++++++++++++++
 2 files changed, 26 insertions(+), 6 deletions(-)
---
diff --git a/src/bin/rsvg-convert.rs b/src/bin/rsvg-convert.rs
index 77f06678..e3f22727 100644
--- a/src/bin/rsvg-convert.rs
+++ b/src/bin/rsvg-convert.rs
@@ -6,7 +6,7 @@ use gio::{
     Cancellable, FileCreateFlags, FileExt, InputStream, OutputStream, UnixInputStream,
     UnixOutputStream,
 };
-use librsvg::rsvg_convert_only::LegacySize;
+use librsvg::rsvg_convert_only::{LegacySize, PathOrUrl};
 use librsvg::{CairoRenderer, Color, Loader, Parse, RenderingError};
 use once_cell::unsync::OnceCell;
 use std::ops::Deref;
@@ -285,14 +285,14 @@ impl std::os::unix::io::IntoRawFd for Stdout {
 #[derive(Clone, Debug)]
 enum Input {
     Stdin,
-    Path(PathBuf),
+    Named(PathOrUrl),
 }
 
 impl std::fmt::Display for Input {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         match self {
             Input::Stdin => "stdin".fmt(f),
-            Input::Path(p) => p.display().fmt(f),
+            Input::Named(p) => p.fmt(f),
         }
     }
 }
@@ -354,8 +354,8 @@ impl Converter {
         for input in &self.input {
             let (stream, basefile) = match input {
                 Input::Stdin => (Stdin::stream().upcast::<InputStream>(), None),
-                Input::Path(p) => {
-                    let file = gio::File::new_for_path(p);
+                Input::Named(p) => {
+                    let file = p.get_gfile();
                     let stream = file
                         .read(None::<&Cancellable>)
                         .unwrap_or_else(|e| exit!("Error reading file \"{}\": {}", input, e));
@@ -615,7 +615,10 @@ fn parse_args() -> Result<Converter, clap::Error> {
     let zoom_y = value_t!(matches, "zoom_y", f64).or_none()?;
 
     let input = match matches.values_of_os("FILE") {
-        Some(values) => values.map(PathBuf::from).map(Input::Path).collect(),
+        Some(values) => values
+            .map(PathOrUrl::from_os_str)
+            .map(Input::Named)
+            .collect(),
         None => vec![Input::Stdin],
     };
 
diff --git a/tests/src/cmdline/rsvg_convert.rs b/tests/src/cmdline/rsvg_convert.rs
index 2315ecb8..cc3a991f 100644
--- a/tests/src/cmdline/rsvg_convert.rs
+++ b/tests/src/cmdline/rsvg_convert.rs
@@ -14,6 +14,7 @@ use predicates::prelude::*;
 use predicates::str::*;
 use std::path::Path;
 use tempfile::Builder;
+use url::Url;
 
 // What should be tested here?
 // The goal is to test the code in rsvg-convert, not the entire library.
@@ -89,6 +90,22 @@ fn argument_is_input_filename() {
         .stdout(file::is_png());
 }
 
+#[test]
+fn argument_is_url() {
+    let path = Path::new("tests/fixtures/dimensions/521-with-viewbox.svg")
+        .canonicalize()
+        .unwrap();
+    let url = Url::from_file_path(path).unwrap();
+    let stringified = url.as_str();
+    assert!(stringified.starts_with("file://"));
+
+    RsvgConvert::new()
+        .arg(stringified)
+        .assert()
+        .success()
+        .stdout(file::is_png());
+}
+
 #[test]
 fn output_format_png() {
     RsvgConvert::new_with_input("tests/fixtures/dimensions/521-with-viewbox.svg")


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