mirror of
https://github.com/NotAShelf/stash.git
synced 2026-05-20 05:56:37 +00:00
modularize codebase
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I6a6a69648ea836bfeb539450db14a666c623412e
This commit is contained in:
parent
478c020579
commit
b0820a1940
10 changed files with 445 additions and 252 deletions
|
|
@ -1,24 +1,28 @@
|
|||
use crate::{Entry, detect_mime, u64_to_ivec};
|
||||
use rmp_serde::encode::to_vec;
|
||||
use sled::Db;
|
||||
use crate::db::{Entry, SledClipboardDb, detect_mime, u64_to_ivec};
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
pub fn import_tsv(db: &Db, input: impl io::Read) {
|
||||
let reader = io::BufReader::new(input);
|
||||
let mut imported = 0;
|
||||
for line in reader.lines().map_while(Result::ok) {
|
||||
let mut parts = line.splitn(2, '\t');
|
||||
if let (Some(id_str), Some(val)) = (parts.next(), parts.next()) {
|
||||
if let Ok(id) = id_str.parse::<u64>() {
|
||||
let entry = Entry {
|
||||
contents: val.as_bytes().to_vec(),
|
||||
mime: detect_mime(val.as_bytes()),
|
||||
};
|
||||
let enc = to_vec(&entry).unwrap();
|
||||
db.insert(u64_to_ivec(id), enc).unwrap();
|
||||
imported += 1;
|
||||
pub trait ImportCommand {
|
||||
fn import_tsv(&self, input: impl io::Read);
|
||||
}
|
||||
|
||||
impl ImportCommand for SledClipboardDb {
|
||||
fn import_tsv(&self, input: impl io::Read) {
|
||||
let reader = io::BufReader::new(input);
|
||||
let mut imported = 0;
|
||||
for line in reader.lines().map_while(Result::ok) {
|
||||
let mut parts = line.splitn(2, '\t');
|
||||
if let (Some(id_str), Some(val)) = (parts.next(), parts.next()) {
|
||||
if let Ok(id) = id_str.parse::<u64>() {
|
||||
let entry = Entry {
|
||||
contents: val.as_bytes().to_vec(),
|
||||
mime: detect_mime(val.as_bytes()),
|
||||
};
|
||||
let enc = rmp_serde::encode::to_vec(&entry).unwrap();
|
||||
self.db.insert(u64_to_ivec(id), enc).unwrap();
|
||||
imported += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
eprintln!("Imported {imported} records from TSV into sled database.");
|
||||
}
|
||||
eprintln!("Imported {imported} records from TSV into sled database.");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue