diff --git a/Cargo.toml b/Cargo.toml index 7b3ffed..93cd08f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,6 @@ keywords = ["clipboard"] edition = "2018" readme = "README.md" -[dependencies] -anyhow = "1.0.31" - [target.'cfg(windows)'.dependencies] clipboard-win = {version = "4.0.2", features=["std"]} @@ -21,6 +18,5 @@ objc_id = "0.1" objc-foundation = "0.1" [target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))'.dependencies] -failure = "0.1" -wl-clipboard-rs = "0.4" -x11-clipboard = "0.5.1" +wl-clipboard-rs = "0.6" +x11-clipboard = "0.6" diff --git a/README.md b/README.md index 2480155..6d071e1 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ cli-clipboard is a fork of [rust-clipboard](https://github.com/aweinstock314/rus On Linux it will first attempt to setup a Wayland clipboard provider. If that fails it will then fallback to the X11 clipboard provider. +Note: On Linux, you'll need to have xorg-dev and libxcb-composite0-dev to compile. On Debian and Ubuntu you can install them with + + sudo apt install xorg-dev libxcb-composite0-dev + ## Examples Using ClipboardContext to create a clipboard provider: @@ -53,7 +57,7 @@ fn clear(&mut self) -> anhow::Result<()>; ### ClipboardContext -- `ClipboardContext` is a type alias for one of {`WindowsClipboardContext`, `OSXClipboardContext`, `LinuxClipboardContext`}, all of which implement `ClipboardProvider`. Which concrete type is chosen for `ClipboardContext` depends on the OS (via conditional compilation). +- `ClipboardContext` is a type alias for one of {`WindowsClipboardContext`, `OSXClipboardContext`, `LinuxClipboardContext`}, all of which implement `ClipboardProvider`. Which concrete type is chosen for `ClipboardContext` depends on the OS (via conditional compilation). - `WaylandClipboardContext` and `X11ClipboardContext` are also available but generally the correct one will be chosen by `LinuxClipboardContext`. ### Convenience Functions diff --git a/src/common.rs b/src/common.rs index 28152b4..7915a06 100644 --- a/src/common.rs +++ b/src/common.rs @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -use anyhow::Result; +use crate::Result; /// Trait for clipboard access pub trait ClipboardProvider: Sized { diff --git a/src/lib.rs b/src/lib.rs index a5d1070..403d880 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,7 @@ extern crate x11_clipboard as x11_clipboard_crate; #[macro_use] extern crate objc; -use anyhow::Result; +type Result = std::result::Result>; mod common; pub use common::ClipboardProvider; diff --git a/src/linux_clipboard.rs b/src/linux_clipboard.rs index db660bb..2925372 100644 --- a/src/linux_clipboard.rs +++ b/src/linux_clipboard.rs @@ -1,7 +1,7 @@ use crate::common::*; use crate::wayland_clipboard::WaylandClipboardContext; use crate::x11_clipboard::{Clipboard, X11ClipboardContext}; -use anyhow::Result; +use crate::Result; enum LinuxContext { Wayland(WaylandClipboardContext), diff --git a/src/macos_clipboard.rs b/src/macos_clipboard.rs index e139c1b..89cc2cc 100644 --- a/src/macos_clipboard.rs +++ b/src/macos_clipboard.rs @@ -15,7 +15,7 @@ limitations under the License. */ use crate::common::*; -use anyhow::{anyhow, Result}; +use crate::Result; use objc::runtime::{Class, Object}; use objc_foundation::{INSArray, INSObject, INSString}; use objc_foundation::{NSArray, NSDictionary, NSObject, NSString}; @@ -32,11 +32,10 @@ extern "C" {} impl ClipboardProvider for MacOSClipboardContext { fn new() -> Result { - let cls = - Class::get("NSPasteboard").ok_or_else(|| anyhow!("Class::get(\"NSPasteboard\")"))?; + let cls = Class::get("NSPasteboard").ok_or_else(|| "Class::get(\"NSPasteboard\")")?; let pasteboard: *mut Object = unsafe { msg_send![cls, generalPasteboard] }; if pasteboard.is_null() { - return Err(anyhow!("NSPasteboard#generalPasteboard returned null")); + return Err("NSPasteboard#generalPasteboard returned null".into()); } let pasteboard: Id = unsafe { Id::from_ptr(pasteboard) }; Ok(MacOSClipboardContext { pasteboard }) @@ -53,16 +52,12 @@ impl ClipboardProvider for MacOSClipboardContext { let obj: *mut NSArray = msg_send![self.pasteboard, readObjectsForClasses:&*classes options:&*options]; if obj.is_null() { - return Err(anyhow!( - "pasteboard#readObjectsForClasses:options: returned null", - )); + return Err("pasteboard#readObjectsForClasses:options: returned null".into()); } Id::from_ptr(obj) }; if string_array.count() == 0 { - Err(anyhow!( - "pasteboard#readObjectsForClasses:options: returned empty", - )) + Err("pasteboard#readObjectsForClasses:options: returned empty".into()) } else { Ok(string_array[0].as_str().to_owned()) } @@ -75,7 +70,7 @@ impl ClipboardProvider for MacOSClipboardContext { if success { Ok(()) } else { - Err(anyhow!("NSPasteboard#writeObjects: returned false")) + Err("NSPasteboard#writeObjects: returned false".into()) } } diff --git a/src/wayland_clipboard.rs b/src/wayland_clipboard.rs index c482370..7bb5ba2 100644 --- a/src/wayland_clipboard.rs +++ b/src/wayland_clipboard.rs @@ -15,8 +15,7 @@ limitations under the License. */ use crate::common::*; -use anyhow::Result; -use failure::Fail; +use crate::Result; use std::io::{self, Read}; use wl_clipboard_rs::{ copy::{self, clear, Options, ServeRequests}, @@ -62,10 +61,8 @@ impl ClipboardProvider for WaylandClipboardContext { fn new() -> Result { let supports_primary_selection = match utils::is_primary_selection_supported() { Ok(v) => v, - Err(e) => match e { - utils::PrimarySelectionCheckError::NoSeats => false, - _ => return Err(into_boxed_error(e)), - }, + Err(utils::PrimarySelectionCheckError::NoSeats) => false, + Err(e) => return Err(e.into()), }; Ok(WaylandClipboardContext { @@ -111,12 +108,10 @@ impl ClipboardProvider for WaylandClipboardContext { paste::MimeType::Text, ) { Ok((reader, _)) => reader, - Err(e) => match e { - paste::Error::NoSeats | paste::Error::ClipboardEmpty | paste::Error::NoMimeType => { - return Ok("".to_string()); - } - _ => return Err(into_boxed_error(e)), - }, + Err( + paste::Error::NoSeats | paste::Error::ClipboardEmpty | paste::Error::NoMimeType, + ) => return Ok("".to_string()), + Err(e) => return Err(e.into()), }; Ok(read_into_string(&mut reader).map_err(Box::new)?) @@ -148,22 +143,18 @@ impl ClipboardProvider for WaylandClipboardContext { copy::Source::Bytes(data.into_bytes().into()), copy::MimeType::Text, ) - .map_err(into_boxed_error) + .map_err(Into::into) } fn clear(&mut self) -> Result<()> { if self.supports_primary_selection { - clear(copy::ClipboardType::Both, copy::Seat::All).map_err(into_boxed_error) + clear(copy::ClipboardType::Both, copy::Seat::All).map_err(Into::into) } else { - clear(copy::ClipboardType::Regular, copy::Seat::All).map_err(into_boxed_error) + clear(copy::ClipboardType::Regular, copy::Seat::All).map_err(Into::into) } } } -fn into_boxed_error(fail: F) -> anyhow::Error { - fail.compat().into() -} - fn read_into_string(reader: &mut R) -> io::Result { let mut contents = String::new(); reader.read_to_string(&mut contents)?; diff --git a/src/windows_clipboard.rs b/src/windows_clipboard.rs index c140c6a..c71f7a5 100644 --- a/src/windows_clipboard.rs +++ b/src/windows_clipboard.rs @@ -17,7 +17,7 @@ limitations under the License. use clipboard_win::{empty, get_clipboard_string, set_clipboard_string, Clipboard}; use crate::common::ClipboardProvider; -use anyhow::Result; +use crate::Result; pub struct WindowsClipboardContext; diff --git a/src/x11_clipboard.rs b/src/x11_clipboard.rs index d299ee1..d19976e 100644 --- a/src/x11_clipboard.rs +++ b/src/x11_clipboard.rs @@ -15,10 +15,10 @@ limitations under the License. */ use crate::common::*; -use anyhow::Result; +use crate::Result; use std::marker::PhantomData; use std::time::Duration; -use x11_clipboard_crate::xcb::xproto::Atom; +use x11_clipboard_crate::xcb::x::Atom; use x11_clipboard_crate::Atoms; use x11_clipboard_crate::Clipboard as X11Clipboard;