aboutsummaryrefslogtreecommitdiffstats
path: root/src/local
diff options
context:
space:
mode:
authorEllie Huxtable <e@elm.sh>2020-10-05 19:25:34 +0100
committerGitHub <noreply@github.com>2020-10-05 19:25:34 +0100
commit46285309fe7f0ccbc829555d402c6c446b69c1da (patch)
tree5939c6d8b137d3a44662b5d1fbadca3407b69ed9 /src/local
parentMerge pull request #1 from conradludgate/main (diff)
parentfix: help text (diff)
downloadatuin-46285309fe7f0ccbc829555d402c6c446b69c1da.zip
Merge pull request #2 from conradludgate/main
feat: use directories project data dir
Diffstat (limited to 'src/local')
-rw-r--r--src/local/database.rs46
-rw-r--r--src/local/history.rs6
-rw-r--r--src/local/mod.rs2
3 files changed, 29 insertions, 25 deletions
diff --git a/src/local/database.rs b/src/local/database.rs
index b84bf99c..b94a6445 100644
--- a/src/local/database.rs
+++ b/src/local/database.rs
@@ -1,12 +1,11 @@
use std::path::Path;
use eyre::Result;
-use shellexpand;
-use rusqlite::{params, Connection};
use rusqlite::NO_PARAMS;
+use rusqlite::{params, Connection};
-use super::history::History;
+use crate::History;
pub trait Database {
fn save(&self, h: History) -> Result<()>;
@@ -19,23 +18,25 @@ pub struct SqliteDatabase {
conn: Connection,
}
-impl SqliteDatabase{
- pub fn new(path: &str) -> Result<SqliteDatabase> {
- let path = shellexpand::full(path)?;
+impl SqliteDatabase {
+ pub fn new(path: impl AsRef<Path>) -> Result<SqliteDatabase> {
let path = path.as_ref();
-
debug!("opening sqlite database at {:?}", path);
- let create = !Path::new(path).exists();
+ let create = !path.exists();
+ if create {
+ if let Some(dir) = path.parent() {
+ std::fs::create_dir_all(dir)?;
+ }
+ }
+
let conn = Connection::open(path)?;
if create {
Self::setup_db(&conn)?;
}
- Ok(SqliteDatabase{
- conn: conn,
- })
+ Ok(SqliteDatabase { conn })
}
fn setup_db(conn: &Connection) -> Result<()> {
@@ -43,11 +44,11 @@ impl SqliteDatabase{
conn.execute(
"create table if not exists history (
- id integer primary key,
- timestamp integer not null,
- command text not null,
- cwd text not null
- )",
+ id integer primary key,
+ timestamp integer not null,
+ command text not null,
+ cwd text not null
+ )",
NO_PARAMS,
)?;
@@ -61,19 +62,22 @@ impl Database for SqliteDatabase {
self.conn.execute(
"insert into history (
- timestamp,
+ timestamp,
command,
cwd
- ) values (?1, ?2, ?3)",
- params![h.timestamp, h.command, h.cwd])?;
+ ) values (?1, ?2, ?3)",
+ params![h.timestamp, h.command, h.cwd],
+ )?;
Ok(())
}
fn list(&self) -> Result<()> {
debug!("listing history");
-
- let mut stmt = self.conn.prepare("SELECT timestamp, command, cwd FROM history")?;
+
+ let mut stmt = self
+ .conn
+ .prepare("SELECT timestamp, command, cwd FROM history")?;
let history_iter = stmt.query_map(params![], |row| {
Ok(History {
timestamp: row.get(0)?,
diff --git a/src/local/history.rs b/src/local/history.rs
index 61438b6d..e84d718c 100644
--- a/src/local/history.rs
+++ b/src/local/history.rs
@@ -8,11 +8,11 @@ pub struct History {
}
impl History {
- pub fn new(command: &str, cwd: &str) -> History {
+ pub fn new(command: String, cwd: String) -> History {
History {
timestamp: chrono::Utc::now().timestamp_millis(),
- command: command.to_string(),
- cwd: cwd.to_string(),
+ command,
+ cwd,
}
}
}
diff --git a/src/local/mod.rs b/src/local/mod.rs
index 8854aa8e..f587d016 100644
--- a/src/local/mod.rs
+++ b/src/local/mod.rs
@@ -1,2 +1,2 @@
-pub mod history;
pub mod database;
+pub mod history;