diff --git a/Cargo.toml b/Cargo.toml index c4778da..7d920c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "clipboard" version = "0.5.0" +edition = "2021" authors = ["Avi Weinstock "] description = "rust-clipboard is a cross-platform library for getting and setting the contents of the OS-level clipboard." repository = "https://github.com/aweinstock314/rust-clipboard" @@ -8,7 +9,7 @@ license = "MIT / Apache-2.0" keywords = ["clipboard"] [target.'cfg(windows)'.dependencies] -clipboard-win = "2.1" +clipboard-win = "4.4.1" [target.'cfg(target_os = "macos")'.dependencies] objc = "0.2" @@ -16,4 +17,4 @@ objc_id = "0.1" objc-foundation = "0.1" [target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))'.dependencies] -x11-clipboard = "0.3" +x11-clipboard = "0.5.3" diff --git a/examples/primary_selection.rs b/examples/primary_selection.rs index b9812c0..efb5b28 100644 --- a/examples/primary_selection.rs +++ b/examples/primary_selection.rs @@ -1,8 +1,7 @@ extern crate clipboard; -use clipboard::ClipboardProvider; #[cfg(target_os = "linux")] -use clipboard::x11_clipboard::{X11ClipboardContext, Primary}; +use clipboard::x11_clipboard::{Primary, X11ClipboardContext}; #[cfg(target_os = "linux")] fn main() { diff --git a/src/common.rs b/src/common.rs index 92f80d5..683328b 100644 --- a/src/common.rs +++ b/src/common.rs @@ -16,19 +16,19 @@ limitations under the License. use std::error::Error; -pub fn err(s: &str) -> Box { - Box::::from(s) +pub fn err(s: &str) -> Box { + Box::::from(s) } /// Trait for clipboard access pub trait ClipboardProvider: Sized { /// Create a context with which to access the clipboard // TODO: consider replacing Box with an associated type? - fn new() -> Result>; + fn new() -> Result>; /// Method to get the clipboard contents as a String - fn get_contents(&mut self) -> Result>; + fn get_contents(&mut self) -> Result>; /// Method to set the clipboard contents as a String - fn set_contents(&mut self, String) -> Result<(), Box>; + fn set_contents(&mut self, _: String) -> Result<(), Box>; // TODO: come up with some platform-agnostic API for richer types // than just strings (c.f. issue #31) } diff --git a/src/lib.rs b/src/lib.rs index de6169b..bb75d8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,46 +16,59 @@ limitations under the License. #![crate_name = "clipboard"] #![crate_type = "lib"] -#![crate_type = "dylib"] -#![crate_type = "rlib"] -#[cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))] +#[cfg(all( + unix, + not(any(target_os = "macos", target_os = "android", target_os = "emscripten")) +))] extern crate x11_clipboard as x11_clipboard_crate; #[cfg(windows)] extern crate clipboard_win; -#[cfg(target_os="macos")] +#[cfg(target_os = "macos")] #[macro_use] extern crate objc; -#[cfg(target_os="macos")] -extern crate objc_id; -#[cfg(target_os="macos")] +#[cfg(target_os = "macos")] extern crate objc_foundation; +#[cfg(target_os = "macos")] +extern crate objc_id; mod common; -pub use common::ClipboardProvider; +pub use crate::common::ClipboardProvider; -#[cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))] +#[cfg(all( + unix, + not(any(target_os = "macos", target_os = "android", target_os = "emscripten")) +))] pub mod x11_clipboard; #[cfg(windows)] pub mod windows_clipboard; -#[cfg(target_os="macos")] +#[cfg(target_os = "macos")] pub mod osx_clipboard; pub mod nop_clipboard; -#[cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))] +#[cfg(all( + unix, + not(any(target_os = "macos", target_os = "android", target_os = "emscripten")) +))] pub type ClipboardContext = x11_clipboard::X11ClipboardContext; #[cfg(windows)] pub type ClipboardContext = windows_clipboard::WindowsClipboardContext; -#[cfg(target_os="macos")] +#[cfg(target_os = "macos")] pub type ClipboardContext = osx_clipboard::OSXClipboardContext; -#[cfg(target_os="android")] +#[cfg(target_os = "android")] pub type ClipboardContext = nop_clipboard::NopClipboardContext; // TODO: implement AndroidClipboardContext (see #52) -#[cfg(not(any(unix, windows, target_os="macos", target_os="android", target_os="emscripten")))] +#[cfg(not(any( + unix, + windows, + target_os = "macos", + target_os = "android", + target_os = "emscripten" +)))] pub type ClipboardContext = nop_clipboard::NopClipboardContext; #[test] diff --git a/src/nop_clipboard.rs b/src/nop_clipboard.rs index 75a2913..0ecb37d 100644 --- a/src/nop_clipboard.rs +++ b/src/nop_clipboard.rs @@ -14,23 +14,27 @@ See the License for the specific language governing permissions and limitations under the License. */ -use common::ClipboardProvider; +use crate::common::ClipboardProvider; use std::error::Error; pub struct NopClipboardContext; impl ClipboardProvider for NopClipboardContext { - fn new() -> Result> { + fn new() -> Result> { Ok(NopClipboardContext) } - fn get_contents(&mut self) -> Result> { - println!("Attempting to get the contents of the clipboard, which hasn't yet been \ - implemented on this platform."); + fn get_contents(&mut self) -> Result> { + println!( + "Attempting to get the contents of the clipboard, which hasn't yet been \ + implemented on this platform." + ); Ok("".to_string()) } - fn set_contents(&mut self, _: String) -> Result<(), Box> { - println!("Attempting to set the contents of the clipboard, which hasn't yet been \ - implemented on this platform."); + fn set_contents(&mut self, _: String) -> Result<(), Box> { + println!( + "Attempting to set the contents of the clipboard, which hasn't yet been \ + implemented on this platform." + ); Ok(()) } } diff --git a/src/osx_clipboard.rs b/src/osx_clipboard.rs index cf8c0b3..8ab3244 100644 --- a/src/osx_clipboard.rs +++ b/src/osx_clipboard.rs @@ -14,10 +14,10 @@ See the License for the specific language governing permissions and limitations under the License. */ -use common::*; -use objc::runtime::{Object, Class}; -use objc_foundation::{INSArray, INSString, INSObject}; -use objc_foundation::{NSArray, NSDictionary, NSString, NSObject}; +use crate::common::*; +use objc::runtime::{Class, Object}; +use objc_foundation::{INSArray, INSObject, INSString}; +use objc_foundation::{NSArray, NSDictionary, NSObject, NSString}; use objc_id::{Id, Owned}; use std::error::Error; use std::mem::transmute; @@ -31,16 +31,16 @@ pub struct OSXClipboardContext { extern "C" {} impl ClipboardProvider for OSXClipboardContext { - fn new() -> Result> { - let cls = try!(Class::get("NSPasteboard").ok_or(err("Class::get(\"NSPasteboard\")"))); + fn new() -> Result> { + let cls = Class::get("NSPasteboard").ok_or_else(|| err("Class::get(\"NSPasteboard\")"))?; let pasteboard: *mut Object = unsafe { msg_send![cls, generalPasteboard] }; if pasteboard.is_null() { return Err(err("NSPasteboard#generalPasteboard returned null")); } let pasteboard: Id = unsafe { Id::from_ptr(pasteboard) }; - Ok(OSXClipboardContext { pasteboard: pasteboard }) + Ok(OSXClipboardContext { pasteboard }) } - fn get_contents(&mut self) -> Result> { + fn get_contents(&mut self) -> Result> { let string_class: Id = { let cls: Id = unsafe { Id::from_ptr(class("NSString")) }; unsafe { transmute(cls) } @@ -51,20 +51,24 @@ impl ClipboardProvider for OSXClipboardContext { let obj: *mut NSArray = msg_send![self.pasteboard, readObjectsForClasses:&*classes options:&*options]; if obj.is_null() { - return Err(err("pasteboard#readObjectsForClasses:options: returned null")); + return Err(err( + "pasteboard#readObjectsForClasses:options: returned null", + )); } Id::from_ptr(obj) }; if string_array.count() == 0 { - Err(err("pasteboard#readObjectsForClasses:options: returned empty")) + Err(err( + "pasteboard#readObjectsForClasses:options: returned empty", + )) } else { Ok(string_array[0].as_str().to_owned()) } } - fn set_contents(&mut self, data: String) -> Result<(), Box> { + fn set_contents(&mut self, data: String) -> Result<(), Box> { let string_array = NSArray::from_vec(vec![NSString::from_str(&data)]); let _: usize = unsafe { msg_send![self.pasteboard, clearContents] }; - let success: bool = unsafe { msg_send![self.pasteboard, writeObjects:string_array] }; + let success: bool = unsafe { msg_send![self.pasteboard, writeObjects: string_array] }; return if success { Ok(()) } else {