stash/src/commands/decode.rs
NotAShelf da9bf5ea3e
treewide: make logging format more consistent; make clipboard persistence opt-in
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I9092f93c29fcbe99c90483875f4acd0c6a6a6964
2026-04-03 14:12:00 +03:00

88 lines
2.4 KiB
Rust

use std::io::{Read, Write};
use wl_clipboard_rs::paste::{ClipboardType, MimeType, Seat, get_contents};
use crate::db::{ClipboardDb, SqliteClipboardDb, StashError};
pub trait DecodeCommand {
fn decode(
&self,
in_: impl Read,
out: impl Write,
input: Option<String>,
) -> Result<(), StashError>;
}
impl DecodeCommand for SqliteClipboardDb {
fn decode(
&self,
mut in_: impl Read,
mut out: impl Write,
input: Option<String>,
) -> Result<(), StashError> {
let input_str = if let Some(s) = input {
s
} else {
let mut buf = String::new();
in_
.read_to_string(&mut buf)
.map_err(|e| StashError::DecodeRead(e.to_string().into()))?;
buf
};
// If input is empty or whitespace, treat as error and trigger fallback
if input_str.trim().is_empty() {
log::debug!("no input provided to decode; relaying clipboard to stdout");
if let Ok((mut reader, _mime)) =
get_contents(ClipboardType::Regular, Seat::Unspecified, MimeType::Any)
{
let mut buf = Vec::new();
reader.read_to_end(&mut buf).map_err(|e| {
StashError::DecodeRead(
format!("Failed to read clipboard for relay: {e}").into(),
)
})?;
out.write_all(&buf).map_err(|e| {
StashError::DecodeWrite(
format!("Failed to write clipboard relay: {e}").into(),
)
})?;
} else {
return Err(StashError::DecodeGet(
"Failed to get clipboard contents for relay".into(),
));
}
return Ok(());
}
// Try decode as usual
match self.decode_entry(
input_str.as_bytes(),
&mut out,
Some(input_str.clone()),
) {
Ok(()) => Ok(()),
Err(e) => {
// On decode failure, relay clipboard as fallback
if let Ok((mut reader, _mime)) =
get_contents(ClipboardType::Regular, Seat::Unspecified, MimeType::Any)
{
let mut buf = Vec::new();
reader.read_to_end(&mut buf).map_err(|err| {
StashError::DecodeRead(
format!("Failed to read clipboard for relay: {err}").into(),
)
})?;
out.write_all(&buf).map_err(|err| {
StashError::DecodeWrite(
format!("Failed to write clipboard relay: {err}").into(),
)
})?;
Ok(())
} else {
Err(e)
}
},
}
}
}