clipboard.rs

 1use seahash::SeaHasher;
 2use serde::{Deserialize, Serialize};
 3use std::hash::{Hash, Hasher};
 4
 5#[derive(Clone, Debug, Eq, PartialEq)]
 6pub struct ClipboardItem {
 7    pub(crate) text: String,
 8    pub(crate) metadata: Option<String>,
 9}
10
11impl ClipboardItem {
12    pub fn new(text: String) -> Self {
13        Self {
14            text,
15            metadata: None,
16        }
17    }
18
19    pub fn with_metadata<T: Serialize>(mut self, metadata: T) -> Self {
20        self.metadata = Some(serde_json::to_string(&metadata).unwrap());
21        self
22    }
23
24    pub fn text(&self) -> &String {
25        &self.text
26    }
27
28    pub fn metadata<T>(&self) -> Option<T>
29    where
30        T: for<'a> Deserialize<'a>,
31    {
32        self.metadata
33            .as_ref()
34            .and_then(|m| serde_json::from_str(m).ok())
35    }
36
37    pub(crate) fn text_hash(text: &str) -> u64 {
38        let mut hasher = SeaHasher::new();
39        text.hash(&mut hasher);
40        hasher.finish()
41    }
42}