stash: refactor error handling and entry deduplication

This includes breaking changes to the database entries, where we have
started deduplicating based on hashes instead of full entries. Entry
collisions are possible, but highly unlikely.

Additionally we use `Box<str>` for error variants to reduce allocations.
This is *yet* to give me a non-marginal performance benefit but doesn't
hurt to be more correct.

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I6a6a6964d0a33392da61372214ca3088551564ac
This commit is contained in:
raf 2025-09-19 13:46:09 +03:00
commit a41d72fb6b
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
7 changed files with 348 additions and 258 deletions

View file

@ -27,19 +27,19 @@ impl ImportCommand for SqliteClipboardDb {
let mut imported = 0;
for (lineno, line) in reader.lines().enumerate() {
let line = line.map_err(|e| {
StashError::Store(format!("Failed to read line {lineno}: {e}"))
StashError::Store(format!("Failed to read line {lineno}: {e}").into())
})?;
let mut parts = line.splitn(2, '\t');
let (Some(id_str), Some(val)) = (parts.next(), parts.next()) else {
return Err(StashError::Store(format!(
"Malformed TSV line {lineno}: {line:?}"
)));
return Err(StashError::Store(
format!("Malformed TSV line {lineno}: {line:?}").into(),
));
};
let Ok(_id) = id_str.parse::<u64>() else {
return Err(StashError::Store(format!(
"Failed to parse id from line {lineno}: {id_str}"
)));
return Err(StashError::Store(
format!("Failed to parse id from line {lineno}: {id_str}").into(),
));
};
let entry = Entry {
@ -54,9 +54,9 @@ impl ImportCommand for SqliteClipboardDb {
rusqlite::params![entry.contents, entry.mime],
)
.map_err(|e| {
StashError::Store(format!(
"Failed to insert entry at line {lineno}: {e}"
))
StashError::Store(
format!("Failed to insert entry at line {lineno}: {e}").into(),
)
})?;
imported += 1;
}