Skip to content

Commit

Permalink
feat: use sqlite file (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Terkwood authored Jun 12, 2022
1 parent 053be3e commit ad27c08
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zeditor"
version = "0.1.0-walemoji"
version = "0.1.0-X"
edition = "2021"

[dependencies]
Expand Down
57 changes: 12 additions & 45 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,25 @@
use rusqlite::{params, Connection, Result};
use crate::env::ZEDITOR_HOME;
use rusqlite::{ Connection, Result};
use std::collections::HashMap;
use std::path::{ PathBuf};

const FILENAME: &str = ".zeditor.db";

pub struct Db {
pub conn: Connection,
}
impl Db {
pub fn new() -> Result<Self> {
let conn = Connection::open_in_memory()?;
let conn = Connection::open(path_to_db().as_path())?;

conn.execute(
"CREATE TABLE IF NOT EXISTS search_replace (
search TEXT PRIMARY KEY,
replace TEXT NOT NULL
)",
) STRICT",
[],
)?;

let _long_insert = "INSERT INTO search_replace (search, replace)
VALUES
(?1, ?2),
(?3, ?4),
(?5, ?6),
(?7, ?8),
(?9, ?10),
(?11, ?12)";

let _long_params = params![
"scala",
"[[scala]]",
"Scala",
"[[scala]]",
"rust",
"[[rust]]",
"svelte",
"[[svelte]]",
"Godot",
"[[godot]]"
];

let short_insert = "INSERT INTO search_replace (search, replace)
VALUES
(?1, ?2),
(?3, ?4),
(?5, ?6)";
let short_params = params![
"scala",
"[[scala]]",
"rust",
"[[rust]]",
"Godot",
"[[godot]]"
];

conn.execute(short_insert, short_params)?;
Ok(Self { conn })
}

Expand All @@ -71,9 +38,9 @@ impl Db {
}
}

fn _dummy_search_replace() -> Result<HashMap<String, String>> {
let mut h = HashMap::new();
h.insert("scala".to_string(), "[[scala]]".to_string());
h.insert("rust".to_string(), "[[rust]]".to_string());
Ok(h)
fn path_to_db() -> PathBuf {
let mut pb = PathBuf::new();
pb.push(ZEDITOR_HOME);
pb.push(FILENAME);
pb
}
1 change: 1 addition & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const ZEDITOR_HOME: &str = env!("ZEDITOR_HOME");
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod db;
mod env;
pub mod replace;
pub mod search;
3 changes: 1 addition & 2 deletions src/search.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::db::Db;
use crate::env::ZEDITOR_HOME;
use cursive::reexports::crossbeam_channel::{select, Receiver, Sender};
use regex::Regex;
use std::path::PathBuf;
Expand Down Expand Up @@ -49,8 +50,6 @@ pub async fn run(
}
}

const ZEDITOR_HOME: &str = env!("ZEDITOR_HOME");

pub async fn search_files(terms: &[(String, Regex)]) -> Vec<Hit> {
let mut out: Vec<Vec<Hit>> = vec![];

Expand Down

0 comments on commit ad27c08

Please sign in to comment.