[librsvg/rustify-rsvg-convert] PathOrBuf: new from_os_str() method



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

    PathOrBuf: new from_os_str() method
    
    For rsvg-convert we need to be able to take a command-line argument
    from clap, which is an OsStr, and do PathOrUrl's magic to decide
    whether the argument is a URL or a file system path.

 src/c_api/handle.rs | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
---
diff --git a/src/c_api/handle.rs b/src/c_api/handle.rs
index 3e5df6c7..2d706665 100644
--- a/src/c_api/handle.rs
+++ b/src/c_api/handle.rs
@@ -23,7 +23,7 @@
 //! [`CHandle`]: struct.CHandle.html
 
 use std::cell::{Cell, Ref, RefCell, RefMut};
-use std::ffi::{CStr, CString};
+use std::ffi::{CStr, CString, OsStr};
 use std::fmt;
 use std::ops;
 use std::path::PathBuf;
@@ -2158,6 +2158,14 @@ impl PathOrUrl {
             }
         })
     }
+
+    pub fn from_os_str(osstr: &OsStr) -> PathOrUrl {
+        osstr
+            .to_str()
+            .ok_or(())
+            .and_then(Self::try_from_str)
+            .unwrap_or_else(|_| PathOrUrl::Path(PathBuf::from(osstr.to_os_string())))
+    }
 }
 
 fn check_cairo_context(cr: &cairo::Context) -> Result<(), RenderingError> {


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