mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-28 03:25:59 +00:00
fdfed3d7db
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
42 lines
955 B
Rust
42 lines
955 B
Rust
use seahash::SeaHasher;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct ClipboardItem {
|
|
pub(crate) text: String,
|
|
pub(crate) metadata: Option<String>,
|
|
}
|
|
|
|
impl ClipboardItem {
|
|
pub fn new(text: String) -> Self {
|
|
Self {
|
|
text,
|
|
metadata: None,
|
|
}
|
|
}
|
|
|
|
pub fn with_metadata<T: Serialize>(mut self, metadata: T) -> Self {
|
|
self.metadata = Some(serde_json::to_string(&metadata).unwrap());
|
|
self
|
|
}
|
|
|
|
pub fn text(&self) -> &String {
|
|
&self.text
|
|
}
|
|
|
|
pub fn metadata<T>(&self) -> Option<T>
|
|
where
|
|
T: for<'a> Deserialize<'a>,
|
|
{
|
|
self.metadata
|
|
.as_ref()
|
|
.and_then(|m| serde_json::from_str(m).ok())
|
|
}
|
|
|
|
pub(crate) fn text_hash(text: &str) -> u64 {
|
|
let mut hasher = SeaHasher::new();
|
|
text.hash(&mut hasher);
|
|
hasher.finish()
|
|
}
|
|
}
|