summary history branches tags files
commit:af5c575f2220c345389373ab6461a832e24ded64
author:Jørgen P. Tjernø
committer:Jørgen P. Tjernø
date:Mon Mar 1 01:41:33 2021 -0800
parents:01090d51a3fbb78a7ec4bd416233b6d86d16be8a
Add an nsstring_to_string method

This is a helper to convert `NSString` Objects into Rust `String`, so
that it's easy to parse the parameter passed to application:openFile:
diff --git a/src/lib.rs b/src/lib.rs
line changes: +3/-0
index cbd0825..3685e06
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -120,6 +120,9 @@ pub use osx::FruitCallbackKey;
 #[cfg(all(target_os = "macos", not(feature="dummy")))]
 pub use osx::parse_url_event;
 
+#[cfg(all(target_os = "macos", not(feature="dummy")))]
+pub use osx::nsstring_to_string;
+
 #[cfg(any(not(target_os = "macos"), feature="dummy"))]
 /// Docs in OS X build.
 pub enum FruitCallbackKey {

diff --git a/src/osx.rs b/src/osx.rs
line changes: +12/-3
index 286524c..b777fe7
--- a/src/osx.rs
+++ b/src/osx.rs
@@ -857,13 +857,22 @@ pub fn parse_url_event(event: *mut Object) -> String {
         }
         let subevent: *mut Object = msg_send![event, paramDescriptorForKeyword: ::keyDirectObject];
         let nsstring: *mut Object = msg_send![subevent, stringValue];
+        nsstring_to_string(nsstring)
+    }
+}
+
+/// Convert an NSString to a Rust `String`
+pub fn nsstring_to_string(nsstring: *mut Object) -> String {
+    unsafe {
         let cstr: *const i8 = msg_send![nsstring, UTF8String];
         if cstr != std::ptr::null() {
-            let rstr = std::ffi::CStr::from_ptr(cstr).to_string_lossy().into_owned();
-            return rstr;
+            std::ffi::CStr::from_ptr(cstr)
+                .to_string_lossy()
+                .into_owned()
+        } else {
+            "".into()
         }
     }
-    "".into()
 }
 
 /// ObjcSubclass is a subclass of the objective-c NSObject base class.