aboutsummaryrefslogtreecommitdiff
path: root/copypasta/src/windows.rs
blob: 95e3db55ff09f1ec95b3e473323aee7c90c17bae (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use clipboard::ClipboardContext;
use clipboard::ClipboardProvider;

use super::{Load, Store};

pub struct Clipboard(ClipboardContext);

#[derive(Debug)]
pub enum Error {
    Clipboard(Box<::std::error::Error>),
}

impl ::std::error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Clipboard(..) => "Error opening clipboard",
        }
    }
}

impl ::std::fmt::Display for Error {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        match *self {
            Error::Clipboard(ref err) => err.fmt(f),
        }
    }
}

unsafe impl Send for Error {}
unsafe impl Sync for Error {}

impl Load for Clipboard {
    type Err = Error;

    fn new() -> Result<Self, Error> {
        ClipboardContext::new()
            .map(Clipboard)
            .map_err(Error::Clipboard)
    }

    fn load_primary(&self) -> Result<String, Self::Err> {
        let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
        ctx.get_contents().map_err(Error::Clipboard)
    }

    fn load_selection(&self) -> Result<String, Self::Err> {
        let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
        ctx.get_contents().map_err(Error::Clipboard)
    }
}

impl Store for Clipboard {
    /// Sets the primary clipboard contents
    #[inline]
    fn store_primary<S>(&mut self, contents: S) -> Result<(), Self::Err>
    where
        S: Into<String>,
    {
        self.0
            .set_contents(contents.into())
            .map_err(Error::Clipboard)
    }

    /// Sets the secondary clipboard contents
    #[inline]
    fn store_selection<S>(&mut self, contents: S) -> Result<(), Self::Err>
    where
        S: Into<String>,
    {
        self.0
            .set_contents(contents.into())
            .map_err(Error::Clipboard)
    }
}