summaryrefslogtreecommitdiffstats
path: root/copypasta/src/lib.rs
blob: 828c23bde3b0c587c57c2669ce5c2104a58b06e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! A cross-platform clipboard library

// This has to be here due to macro_use
#[cfg(target_os = "macos")]
#[macro_use] extern crate objc;

/// Types that can get the system clipboard contents
pub trait Load : Sized {
    /// Errors encountered when working with a clipboard. Each implementation is
    /// allowed to define its own error type, but it must conform to std error.
    type Err: ::std::error::Error + Send + Sync + 'static;

    /// Create a clipboard
    fn new() -> Result<Self, Self::Err>;

    /// Get the primary clipboard contents.
    fn load_primary(&self) -> Result<String, Self::Err>;

    /// Get the clipboard selection contents.
    ///
    /// On most platforms, this doesn't mean anything. A default implementation
    /// is provided which uses the primary clipboard.
    #[inline]
    fn load_selection(&self) -> Result<String, Self::Err> {
        self.load_primary()
    }
}

/// Types that can set the system clipboard contents
///
/// Note that some platforms require the clipboard context to stay active in
/// order to load the contents from other applications.
pub trait Store : Load {
    /// Sets the primary clipboard contents
    fn store_primary<S>(&mut self, contents: S) -> Result<(), Self::Err>
        where S: Into<String>;

    /// Sets the secondary clipboard contents
    fn store_selection<S>(&mut self, contents: S) -> Result<(), Self::Err>
        where S: Into<String>;
}

#[cfg(any(target_os = "linux", target_os = "freebsd"))]
mod x11;
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
pub use x11::{Clipboard, Error};

#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use macos::{Clipboard, Error};