diff options
Diffstat (limited to 'src/local')
| -rw-r--r-- | src/local/database.rs | 39 | ||||
| -rw-r--r-- | src/local/history.rs | 6 | ||||
| -rw-r--r-- | src/local/import.rs | 24 |
3 files changed, 34 insertions, 35 deletions
diff --git a/src/local/database.rs b/src/local/database.rs index 2b014bc0..4f99d8ac 100644 --- a/src/local/database.rs +++ b/src/local/database.rs @@ -9,7 +9,7 @@ use crate::History; pub trait Database { fn save(&mut self, h: History) -> Result<()>; - fn save_bulk(&mut self, h: &Vec<History>) -> Result<()>; + fn save_bulk(&mut self, h: &[History]) -> Result<()>; fn load(&self, id: &str) -> Result<History>; fn list(&self, distinct: bool) -> Result<()>; fn update(&self, h: History) -> Result<()>; @@ -17,12 +17,12 @@ pub trait Database { // Intended for use on a developer machine and not a sync server. // TODO: implement IntoIterator -pub struct SqliteDatabase { +pub struct Sqlite { conn: Connection, } -impl SqliteDatabase { - pub fn new(path: impl AsRef<Path>) -> Result<SqliteDatabase> { +impl Sqlite { + pub fn new(path: impl AsRef<Path>) -> Result<Self> { let path = path.as_ref(); debug!("opening sqlite database at {:?}", path); @@ -39,7 +39,7 @@ impl SqliteDatabase { Self::setup_db(&conn)?; } - Ok(SqliteDatabase { conn }) + Ok(Self { conn }) } fn setup_db(conn: &Connection) -> Result<()> { @@ -65,7 +65,7 @@ impl SqliteDatabase { } } -impl Database for SqliteDatabase { +impl Database for Sqlite { fn save(&mut self, h: History) -> Result<()> { debug!("saving history to sqlite"); let v = vec![h]; @@ -73,7 +73,7 @@ impl Database for SqliteDatabase { self.save_bulk(&v) } - fn save_bulk(&mut self, h: &Vec<History>) -> Result<()> { + fn save_bulk(&mut self, h: &[History]) -> Result<()> { debug!("saving history to sqlite"); let tx = self.conn.transaction()?; @@ -116,7 +116,7 @@ impl Database for SqliteDatabase { where id = ?1", )?; - let iter = stmt.query_map(params![id], |row| { + let mut iter = stmt.query_map(params![id], |row| { Ok(History { id: String::from(id), timestamp: row.get(1)?, @@ -129,11 +129,12 @@ impl Database for SqliteDatabase { }) })?; - for i in iter { - return Ok(i.unwrap()); - } + let history = iter.next().unwrap(); - return Err(eyre!("Failed to fetch history: {}", id)); + match history { + Ok(i) => Ok(i), + Err(e) => Err(eyre!("could not find item: {}", e)), + } } fn update(&self, h: History) -> Result<()> { @@ -152,14 +153,12 @@ impl Database for SqliteDatabase { fn list(&self, distinct: bool) -> Result<()> { debug!("listing history"); - let mut stmt = match distinct { - false => self - .conn - .prepare("SELECT command FROM history order by timestamp asc")?, - - true => self - .conn - .prepare("SELECT distinct command FROM history order by timestamp asc")?, + let mut stmt = if distinct { + self.conn + .prepare("SELECT command FROM history order by timestamp asc")? + } else { + self.conn + .prepare("SELECT distinct command FROM history order by timestamp asc")? }; let history_iter = stmt.query_map(params![], |row| { diff --git a/src/local/history.rs b/src/local/history.rs index 893edbb7..e46180b8 100644 --- a/src/local/history.rs +++ b/src/local/history.rs @@ -23,10 +23,10 @@ impl History { duration: i64, session: Option<String>, hostname: Option<String>, - ) -> History { + ) -> Self { // get the current session or just generate a random string let env_session = - env::var("ATUIN_SESSION").unwrap_or(Uuid::new_v4().to_simple().to_string()); + env::var("ATUIN_SESSION").unwrap_or_else(|_| Uuid::new_v4().to_simple().to_string()); // best attempt at getting the current hostname, or just unknown let os_hostname = hostname::get().unwrap(); @@ -36,7 +36,7 @@ impl History { let session = session.unwrap_or(env_session); let hostname = hostname.unwrap_or(os_hostname); - History { + Self { id: Uuid::new_v4().to_simple().to_string(), timestamp, command, diff --git a/src/local/import.rs b/src/local/import.rs index e8552397..046c74bf 100644 --- a/src/local/import.rs +++ b/src/local/import.rs @@ -9,7 +9,7 @@ use eyre::{eyre, Result}; use crate::local::history::History; #[derive(Debug)] -pub struct ImportZsh { +pub struct Zsh { file: BufReader<File>, pub loc: u64, @@ -23,14 +23,14 @@ fn count_lines(path: &str) -> Result<usize> { Ok(buf.lines().count()) } -impl ImportZsh { - pub fn new(path: &str) -> Result<ImportZsh> { +impl Zsh { + pub fn new(path: &str) -> Result<Self> { let loc = count_lines(path)?; let file = File::open(path)?; let buf = BufReader::new(file); - Ok(ImportZsh { + Ok(Self { file: buf, loc: loc as u64, }) @@ -50,17 +50,17 @@ fn trim_newline(s: &str) -> String { s } -fn parse_extended(line: String) -> History { +fn parse_extended(line: &str) -> History { let line = line.replacen(": ", "", 2); - let mut split = line.splitn(2, ":"); + let mut split = line.splitn(2, ':'); let time = split.next().unwrap_or("-1"); let time = time .parse::<i64>() - .unwrap_or(chrono::Utc::now().timestamp_nanos()); + .unwrap_or_else(|_| chrono::Utc::now().timestamp_nanos()); - let duration = split.next().unwrap(); // might be 0;the command - let mut split = duration.split(";"); + let duration_command = split.next().unwrap(); // might be 0;the command + let mut split = duration_command.split(';'); let duration = split.next().unwrap_or("-1"); // should just be the 0 let duration = duration.parse::<i64>().unwrap_or(-1); @@ -79,7 +79,7 @@ fn parse_extended(line: String) -> History { ) } -impl Iterator for ImportZsh { +impl Iterator for Zsh { type Item = Result<History>; fn next(&mut self) -> Option<Self::Item> { @@ -94,10 +94,10 @@ impl Iterator for ImportZsh { Err(e) => Some(Err(eyre!("failed to parse line: {}", e))), Ok(_) => { - let extended = line.starts_with(":"); + let extended = line.starts_with(':'); if extended { - Some(Ok(parse_extended(line))) + Some(Ok(parse_extended(line.as_str()))) } else { Some(Ok(History::new( chrono::Utc::now().timestamp_nanos(), // what else? :/ |
