use super::{ClipboardBackend, ClipboardError};
use std::sync::{Arc, Mutex};
#[derive(Clone, Debug, Default)]
pub struct MemoryClipboard {
text: Arc<Mutex<Option<String>>>,
failure: Arc<Mutex<Option<String>>>,
}
impl MemoryClipboard {
pub fn new() -> Self {
Self::default()
}
pub fn with_text(text: impl Into<String>) -> Self {
let clipboard = Self::new();
clipboard.set(text);
clipboard
}
pub fn get(&self) -> Option<String> {
self.text.lock().expect("clipboard mutex").clone()
}
pub fn set(&self, text: impl Into<String>) {
*self.text.lock().expect("clipboard mutex") = Some(text.into());
}
pub fn fail_with(&self, reason: Option<&str>) {
*self.failure.lock().expect("clipboard mutex") = reason.map(str::to_owned);
}
fn failure(&self) -> Result<(), ClipboardError> {
match &*self.failure.lock().expect("clipboard mutex") {
Some(reason) => Err(ClipboardError::Unavailable(reason.clone())),
None => Ok(()),
}
}
}
impl ClipboardBackend for MemoryClipboard {
fn text(&mut self) -> Result<String, ClipboardError> {
self.failure()?;
self.get().ok_or(ClipboardError::ContentNotAvailable)
}
fn set_text(&mut self, text: String) -> Result<(), ClipboardError> {
self.failure()?;
self.set(text);
Ok(())
}
}