use std::fmt::Debug;
pub struct Clipboard {
backend: Box<dyn ClipboardBackend>,
}
impl Clipboard {
pub fn new(backend: Box<dyn ClipboardBackend>) -> Self {
Self { backend }
}
pub fn get(&mut self) -> String {
self.backend.get_text()
}
pub fn set(&mut self, text: impl AsRef<str>) {
self.backend.set_text(text.as_ref());
}
}
impl Default for Clipboard {
fn default() -> Self {
Self::new(Box::new(NoopClipboard))
}
}
impl Debug for Clipboard {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Clipboard").finish()
}
}
pub trait ClipboardBackend {
fn get_text(&mut self) -> String;
fn set_text(&mut self, text: &str);
}
struct NoopClipboard;
impl ClipboardBackend for NoopClipboard {
fn get_text(&mut self) -> String {
String::new()
}
fn set_text(&mut self, _text: &str) {}
}