Skip to content

Commit

Permalink
feat: content skip (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Terkwood authored Jun 13, 2022
1 parent 1489039 commit 2a87f13
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 51 deletions.
87 changes: 86 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[package]
name = "zeditor"
version = "0.1.1-b"
version = "0.1.1-c"
edition = "2021"

[dependencies]
blake3 = "1.3.1"
cursive = { git = "https://github.com/gyscos/cursive" }
futures = "0.3.21"
glob = "0.3.0"
regex = "1.5.6"
rusqlite = {version = "0.27.0", features = ["bundled"]}
rusqlite = {version = "0.27.0", features = ["bundled","blob"]}
tokio = {version = "1.19.2", features = ["full"]}
49 changes: 47 additions & 2 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::env::ZEDITOR_HOME;
use rusqlite::{Connection, Result};
use std::collections::HashMap;
use crate::replace::Replacement;
use crate::skip::SkipContent;
use rusqlite::{params, Connection, Result};
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;

const FILENAME: &str = ".zeditor.db";
Expand All @@ -20,6 +22,17 @@ impl Db {
[],
)?;

conn.execute(
"CREATE TABLE IF NOT EXISTS skip_content (
hash BLOB NOT NULL,
start INTEGER NOT NULL,
end INTEGER NOT NULL,
search TEXT NOT NULL,
PRIMARY KEY (hash, start, end, search)
) STRICT",
[],
)?;

Ok(Self { conn })
}

Expand All @@ -36,6 +49,38 @@ impl Db {

Ok(out)
}

pub fn write_perm_skip(&self, skip: SkipContent) -> Result<()> {
self.conn.execute(
"INSERT INTO skip_content (hash, start, end, search)
VALUES (?1, ?2, ?3, ?4)",
params![skip.0.as_bytes(), skip.1.start, skip.1.end, skip.1.search],
)?;
Ok(())
}

pub fn get_skip_contents(&self) -> Result<HashSet<SkipContent>> {
let mut stmt = self
.conn
.prepare("SELECT hash, start, end, search FROM skip_content")?;
let mut rows = stmt.query([])?;

let mut out = HashSet::new();
while let Some(row) = rows.next()? {
let hash_bytes: [u8; 32] = row.get(0)?;
let hash: blake3::Hash = hash_bytes.into();
out.insert(SkipContent(
hash,
Replacement {
start: row.get(1)?,
end: row.get(2)?,
search: row.get(3)?,
},
));
}

Ok(out)
}
}

fn path_to_db() -> PathBuf {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod db;
mod env;
pub mod replace;
pub mod search;
pub mod skip;
Loading

0 comments on commit 2a87f13

Please sign in to comment.