mirror of
https://github.com/NotAShelf/stash.git
synced 2026-04-13 14:33:47 +00:00
treewide: improve logging; custom error types with thiserror
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I6a6a696464e4123d15cfaedf4727776e55948369
This commit is contained in:
parent
7dd3db4c88
commit
6e21021306
10 changed files with 221 additions and 166 deletions
276
src/db/mod.rs
276
src/db/mod.rs
|
|
@ -3,21 +3,76 @@ use std::io::{BufRead, BufReader, Read, Write};
|
|||
use std::str;
|
||||
|
||||
use image::{GenericImageView, ImageFormat};
|
||||
use log::{error, info, warn};
|
||||
use log::{error, info};
|
||||
use rmp_serde::{decode::from_read, encode::to_vec};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sled::{Db, IVec};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum StashError {
|
||||
#[error("Input is empty or too large, skipping store.")]
|
||||
EmptyOrTooLarge,
|
||||
#[error("Input is all whitespace, skipping store.")]
|
||||
AllWhitespace,
|
||||
#[error("Failed to serialize entry: {0}")]
|
||||
Serialize(String),
|
||||
#[error("Failed to store entry: {0}")]
|
||||
Store(String),
|
||||
#[error("Error reading entry during deduplication: {0}")]
|
||||
DeduplicationRead(String),
|
||||
#[error("Error decoding entry during deduplication: {0}")]
|
||||
DeduplicationDecode(String),
|
||||
#[error("Failed to remove entry during deduplication: {0}")]
|
||||
DeduplicationRemove(String),
|
||||
#[error("Failed to trim entry: {0}")]
|
||||
Trim(String),
|
||||
#[error("No entries to delete")]
|
||||
NoEntriesToDelete,
|
||||
#[error("Failed to delete last entry: {0}")]
|
||||
DeleteLast(String),
|
||||
#[error("Failed to wipe database: {0}")]
|
||||
Wipe(String),
|
||||
#[error("Failed to decode entry during list: {0}")]
|
||||
ListDecode(String),
|
||||
#[error("Failed to read input for decode: {0}")]
|
||||
DecodeRead(String),
|
||||
#[error("Failed to extract id for decode: {0}")]
|
||||
DecodeExtractId(String),
|
||||
#[error("Failed to get entry for decode: {0}")]
|
||||
DecodeGet(String),
|
||||
#[error("No entry found for id {0}")]
|
||||
DecodeNoEntry(u64),
|
||||
#[error("Failed to decode entry: {0}")]
|
||||
DecodeDecode(String),
|
||||
#[error("Failed to write decoded entry: {0}")]
|
||||
DecodeWrite(String),
|
||||
#[error("Failed to delete entry during query delete: {0}")]
|
||||
QueryDelete(String),
|
||||
#[error("Failed to delete entry with id {0}: {1}")]
|
||||
DeleteEntry(u64, String),
|
||||
}
|
||||
|
||||
pub trait ClipboardDb {
|
||||
fn store_entry(&self, input: impl Read, max_dedupe_search: u64, max_items: u64);
|
||||
fn deduplicate(&self, buf: &[u8], max: u64);
|
||||
fn trim_db(&self, max: u64);
|
||||
fn delete_last(&self);
|
||||
fn wipe_db(&self);
|
||||
fn list_entries(&self, out: impl Write, preview_width: u32);
|
||||
fn decode_entry(&self, in_: impl Read, out: impl Write, input: Option<String>);
|
||||
fn delete_query(&self, query: &str);
|
||||
fn delete_entries(&self, in_: impl Read);
|
||||
fn store_entry(
|
||||
&self,
|
||||
input: impl Read,
|
||||
max_dedupe_search: u64,
|
||||
max_items: u64,
|
||||
) -> Result<u64, StashError>;
|
||||
fn deduplicate(&self, buf: &[u8], max: u64) -> Result<usize, StashError>;
|
||||
fn trim_db(&self, max: u64) -> Result<(), StashError>;
|
||||
fn delete_last(&self) -> Result<(), StashError>;
|
||||
fn wipe_db(&self) -> Result<(), StashError>;
|
||||
fn list_entries(&self, out: impl Write, preview_width: u32) -> Result<usize, StashError>;
|
||||
fn decode_entry(
|
||||
&self,
|
||||
in_: impl Read,
|
||||
out: impl Write,
|
||||
input: Option<String>,
|
||||
) -> Result<(), StashError>;
|
||||
fn delete_query(&self, query: &str) -> Result<usize, StashError>;
|
||||
fn delete_entries(&self, in_: impl Read) -> Result<usize, StashError>;
|
||||
fn next_sequence(&self) -> u64;
|
||||
}
|
||||
|
||||
|
|
@ -39,20 +94,23 @@ pub struct SledClipboardDb {
|
|||
}
|
||||
|
||||
impl ClipboardDb for SledClipboardDb {
|
||||
fn store_entry(&self, mut input: impl Read, max_dedupe_search: u64, max_items: u64) {
|
||||
fn store_entry(
|
||||
&self,
|
||||
mut input: impl Read,
|
||||
max_dedupe_search: u64,
|
||||
max_items: u64,
|
||||
) -> Result<u64, StashError> {
|
||||
let mut buf = Vec::new();
|
||||
if input.read_to_end(&mut buf).is_err() || buf.is_empty() || buf.len() > 5 * 1_000_000 {
|
||||
warn!("Input is empty or too large, skipping store.");
|
||||
return;
|
||||
return Err(StashError::EmptyOrTooLarge);
|
||||
}
|
||||
if buf.iter().all(u8::is_ascii_whitespace) {
|
||||
warn!("Input is all whitespace, skipping store.");
|
||||
return;
|
||||
return Err(StashError::AllWhitespace);
|
||||
}
|
||||
|
||||
let mime = detect_mime(&buf);
|
||||
|
||||
self.deduplicate(&buf, max_dedupe_search);
|
||||
self.deduplicate(&buf, max_dedupe_search)?;
|
||||
|
||||
let entry = Entry {
|
||||
contents: buf.clone(),
|
||||
|
|
@ -60,210 +118,160 @@ impl ClipboardDb for SledClipboardDb {
|
|||
};
|
||||
|
||||
let id = self.next_sequence();
|
||||
let enc = match to_vec(&entry) {
|
||||
Ok(enc) => enc,
|
||||
Err(e) => {
|
||||
error!("Failed to serialize entry: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let enc = to_vec(&entry).map_err(|e| StashError::Serialize(e.to_string()))?;
|
||||
|
||||
match self.db.insert(u64_to_ivec(id), enc) {
|
||||
Ok(_) => info!("Stored entry with id {id}"),
|
||||
Err(e) => error!("Failed to store entry: {e}"),
|
||||
}
|
||||
self.trim_db(max_items);
|
||||
self.db
|
||||
.insert(u64_to_ivec(id), enc)
|
||||
.map_err(|e| StashError::Store(e.to_string()))?;
|
||||
self.trim_db(max_items)?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
fn deduplicate(&self, buf: &[u8], max: u64) {
|
||||
fn deduplicate(&self, buf: &[u8], max: u64) -> Result<usize, StashError> {
|
||||
let mut count = 0;
|
||||
let mut deduped = 0;
|
||||
for item in self.db.iter().rev().take(max as usize) {
|
||||
let (k, v) = match item {
|
||||
Ok((k, v)) => (k, v),
|
||||
Err(e) => {
|
||||
error!("Error reading entry during deduplication: {e}");
|
||||
continue;
|
||||
}
|
||||
Err(e) => return Err(StashError::DeduplicationRead(e.to_string())),
|
||||
};
|
||||
let entry: Entry = match from_read(v.as_ref()) {
|
||||
Ok(e) => e,
|
||||
Err(e) => {
|
||||
error!("Error decoding entry during deduplication: {e}");
|
||||
continue;
|
||||
}
|
||||
Err(e) => return Err(StashError::DeduplicationDecode(e.to_string())),
|
||||
};
|
||||
if entry.contents == buf {
|
||||
match self.db.remove(k) {
|
||||
Ok(_) => {
|
||||
self.db
|
||||
.remove(k)
|
||||
.map(|_| {
|
||||
deduped += 1;
|
||||
info!("Deduplicated an entry");
|
||||
}
|
||||
Err(e) => error!("Failed to remove entry during deduplication: {e}"),
|
||||
}
|
||||
})
|
||||
.map_err(|e| StashError::DeduplicationRemove(e.to_string()))?;
|
||||
}
|
||||
count += 1;
|
||||
if count >= max {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if deduped > 0 {
|
||||
info!("Deduplicated {deduped} entries");
|
||||
}
|
||||
Ok(deduped)
|
||||
}
|
||||
|
||||
fn trim_db(&self, max: u64) {
|
||||
fn trim_db(&self, max: u64) -> Result<(), StashError> {
|
||||
let mut keys: Vec<_> = self
|
||||
.db
|
||||
.iter()
|
||||
.rev()
|
||||
.filter_map(|kv| match kv {
|
||||
Ok((k, _)) => Some(k),
|
||||
Err(e) => {
|
||||
error!("Failed to read key during trim: {e}");
|
||||
None
|
||||
}
|
||||
Err(_e) => None,
|
||||
})
|
||||
.collect();
|
||||
let initial_len = keys.len();
|
||||
if keys.len() as u64 > max {
|
||||
for k in keys.drain((max as usize)..) {
|
||||
match self.db.remove(k) {
|
||||
Ok(_) => info!("Trimmed entry from database"),
|
||||
Err(e) => error!("Failed to trim entry: {e}"),
|
||||
}
|
||||
self.db
|
||||
.remove(k)
|
||||
.map_err(|e| StashError::Trim(e.to_string()))?;
|
||||
}
|
||||
info!(
|
||||
"Trimmed {} entries from database",
|
||||
initial_len - max as usize
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete_last(&self) {
|
||||
fn delete_last(&self) -> Result<(), StashError> {
|
||||
if let Some((k, _)) = self.db.iter().next_back().and_then(Result::ok) {
|
||||
match self.db.remove(k) {
|
||||
Ok(_) => info!("Deleted last entry"),
|
||||
Err(e) => error!("Failed to delete last entry: {e}"),
|
||||
}
|
||||
self.db
|
||||
.remove(k)
|
||||
.map(|_| ())
|
||||
.map_err(|e| StashError::DeleteLast(e.to_string()))
|
||||
} else {
|
||||
warn!("No entries to delete");
|
||||
Err(StashError::NoEntriesToDelete)
|
||||
}
|
||||
}
|
||||
|
||||
fn wipe_db(&self) {
|
||||
match self.db.clear() {
|
||||
Ok(()) => info!("Wiped database"),
|
||||
Err(e) => error!("Failed to wipe database: {e}"),
|
||||
}
|
||||
fn wipe_db(&self) -> Result<(), StashError> {
|
||||
self.db.clear().map_err(|e| StashError::Wipe(e.to_string()))
|
||||
}
|
||||
|
||||
fn list_entries(&self, mut out: impl Write, preview_width: u32) {
|
||||
fn list_entries(&self, mut out: impl Write, preview_width: u32) -> Result<usize, StashError> {
|
||||
let mut listed = 0;
|
||||
for (k, v) in self.db.iter().rev().filter_map(Result::ok) {
|
||||
let id = ivec_to_u64(&k);
|
||||
let entry: Entry = match from_read(v.as_ref()) {
|
||||
Ok(e) => e,
|
||||
Err(e) => {
|
||||
error!("Failed to decode entry during list: {e}");
|
||||
continue;
|
||||
}
|
||||
Err(e) => return Err(StashError::ListDecode(e.to_string())),
|
||||
};
|
||||
let preview = preview_entry(&entry.contents, entry.mime.as_deref(), preview_width);
|
||||
if writeln!(out, "{id}\t{preview}").is_ok() {
|
||||
listed += 1;
|
||||
}
|
||||
}
|
||||
info!("Listed {listed} entries");
|
||||
Ok(listed)
|
||||
}
|
||||
|
||||
fn decode_entry(&self, mut in_: impl Read, mut out: impl Write, input: Option<String>) {
|
||||
fn decode_entry(
|
||||
&self,
|
||||
mut in_: impl Read,
|
||||
mut out: impl Write,
|
||||
input: Option<String>,
|
||||
) -> Result<(), StashError> {
|
||||
let s = if let Some(input) = input {
|
||||
input
|
||||
} else {
|
||||
let mut buf = String::new();
|
||||
if let Err(e) = in_.read_to_string(&mut buf) {
|
||||
error!("Failed to read input for decode: {e}");
|
||||
return;
|
||||
}
|
||||
in_.read_to_string(&mut buf)
|
||||
.map_err(|e| StashError::DecodeRead(e.to_string()))?;
|
||||
buf
|
||||
};
|
||||
let id = match extract_id(&s) {
|
||||
Ok(id) => id,
|
||||
Err(e) => {
|
||||
error!("Failed to extract id for decode: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let v = match self.db.get(u64_to_ivec(id)) {
|
||||
Ok(Some(v)) => v,
|
||||
Ok(None) => {
|
||||
warn!("No entry found for id {id}");
|
||||
return;
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to get entry for decode: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let entry: Entry = match from_read(v.as_ref()) {
|
||||
Ok(e) => e,
|
||||
Err(e) => {
|
||||
error!("Failed to decode entry: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
if let Err(e) = out.write_all(&entry.contents) {
|
||||
error!("Failed to write decoded entry: {e}");
|
||||
} else {
|
||||
info!("Decoded entry with id {id}");
|
||||
}
|
||||
let id = extract_id(&s).map_err(|e| StashError::DecodeExtractId(e.to_string()))?;
|
||||
let v = self
|
||||
.db
|
||||
.get(u64_to_ivec(id))
|
||||
.map_err(|e| StashError::DecodeGet(e.to_string()))?
|
||||
.ok_or_else(|| StashError::DecodeNoEntry(id))?;
|
||||
let entry: Entry =
|
||||
from_read(v.as_ref()).map_err(|e| StashError::DecodeDecode(e.to_string()))?;
|
||||
|
||||
out.write_all(&entry.contents)
|
||||
.map_err(|e| StashError::DecodeWrite(e.to_string()))?;
|
||||
info!("Decoded entry with id {id}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete_query(&self, query: &str) {
|
||||
fn delete_query(&self, query: &str) -> Result<usize, StashError> {
|
||||
let mut deleted = 0;
|
||||
for (k, v) in self.db.iter().filter_map(Result::ok) {
|
||||
let entry: Entry = match from_read(v.as_ref()) {
|
||||
Ok(e) => e,
|
||||
Err(e) => {
|
||||
error!("Failed to decode entry during query delete: {e}");
|
||||
continue;
|
||||
}
|
||||
Err(_) => continue,
|
||||
};
|
||||
if entry
|
||||
.contents
|
||||
.windows(query.len())
|
||||
.any(|w| w == query.as_bytes())
|
||||
{
|
||||
match self.db.remove(k) {
|
||||
Ok(_) => {
|
||||
self.db
|
||||
.remove(k)
|
||||
.map(|_| {
|
||||
deleted += 1;
|
||||
info!("Deleted entry matching query");
|
||||
}
|
||||
Err(e) => error!("Failed to delete entry during query delete: {e}"),
|
||||
}
|
||||
})
|
||||
.map_err(|e| StashError::QueryDelete(e.to_string()))?;
|
||||
}
|
||||
}
|
||||
info!("Deleted {deleted} entries matching query '{query}'");
|
||||
Ok(deleted)
|
||||
}
|
||||
|
||||
fn delete_entries(&self, in_: impl Read) {
|
||||
fn delete_entries(&self, in_: impl Read) -> Result<usize, StashError> {
|
||||
let reader = BufReader::new(in_);
|
||||
let mut deleted = 0;
|
||||
for line in reader.lines().map_while(Result::ok) {
|
||||
if let Ok(id) = extract_id(&line) {
|
||||
match self.db.remove(u64_to_ivec(id)) {
|
||||
Ok(_) => {
|
||||
self.db
|
||||
.remove(u64_to_ivec(id))
|
||||
.map(|_| {
|
||||
deleted += 1;
|
||||
info!("Deleted entry with id {id}");
|
||||
}
|
||||
Err(e) => error!("Failed to delete entry with id {id}: {e}"),
|
||||
}
|
||||
} else {
|
||||
warn!("Failed to extract id from line: {line}");
|
||||
})
|
||||
.map_err(|e| StashError::DeleteEntry(id, e.to_string()))?;
|
||||
}
|
||||
}
|
||||
info!("Deleted {deleted} entries by id from stdin");
|
||||
Ok(deleted)
|
||||
}
|
||||
|
||||
fn next_sequence(&self) -> u64 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue