diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/db.rs | 42 | ||||
| -rw-r--r-- | src/main.rs | 3 |
2 files changed, 31 insertions, 14 deletions
@@ -8,7 +8,7 @@ use std::{ path::{Path, absolute}, }; -use crate::flac; +use crate::flac::{get_vendor, CURRENT_VENDOR}; pub async fn open_db() -> Result<Connection> { let conn = if let Some(base_dir) = BaseDirs::new() { @@ -18,13 +18,13 @@ pub async fn open_db() -> Result<Connection> { return Err(anyhow!("Failed to locate data directory")); }; - conn.execute("CREATE TABLE IF NOT EXISTS flacs (path TEXT PRIMARY KEY, vendor TEXT, toencode BOOLEAN NOT NULL)", ()).await?; + conn.execute("CREATE TABLE IF NOT EXISTS flacs (path TEXT PRIMARY KEY, toencode BOOLEAN NOT NULL)", ()).await?; Ok(conn) } #[derive(Debug)] -enum Errors { +pub enum Errors { EmptyQuery, } @@ -36,23 +36,23 @@ impl Display for Errors { } } -trait Reencoder { +pub trait Reencoder { async fn insert_file(&self, filename: &impl AsRef<OsStr>) -> Result<()>; async fn update_file(&self, filename: &impl AsRef<OsStr>) -> Result<()>; async fn get_files_toencode(&self) -> Result<Vec<String>>; + async fn get_files_indexed(&self) -> Result<Vec<String>>; } impl Reencoder for Connection { async fn insert_file(&self, filename: &impl AsRef<OsStr>) -> Result<()> { let file = Path::new(filename); - let vendor = flac::get_vendor(file); - let toencode = !matches!(vendor.as_str(), flac::CURRENT_VENDOR); + let toencode = !matches!(get_vendor(file).as_str(), CURRENT_VENDOR); let abs_filename = absolute(file)?; self.execute( - "INSERT INTO flacs (path, vendor, toencode) VALUES (?1, ?2, ?3)", - params![abs_filename.to_str().unwrap(), vendor, toencode], + "INSERT INTO flacs (path, toencode) VALUES (?1, ?2)", + params![abs_filename.to_str().unwrap(), toencode], ) .await?; @@ -63,8 +63,8 @@ impl Reencoder for Connection { let abs_filename = absolute(Path::new(filename))?; self.execute( - "REPLACE INTO flacs (path, vendor, toencode) VALUES (?1, ?2, ?3)", - params![abs_filename.to_str().unwrap(), flac::CURRENT_VENDOR, false], + "REPLACE INTO flacs (path, toencode) VALUES (?1, ?2)", + params![abs_filename.to_str().unwrap(), false], ) .await?; @@ -87,6 +87,23 @@ impl Reencoder for Connection { Ok(filenames) } + + async fn get_files_indexed(&self) -> Result<Vec<String>> { + let rows = self + .query("SELECT path FROM flacs", ()) + .await?; + if rows.column_count() == 0 { + return Err(anyhow!(Errors::EmptyQuery)); + }; + + let filenames = rows + .into_stream() + .map(|row| row.unwrap().get_str(0).unwrap().to_string()) + .collect::<Vec<String>>() + .await; + + Ok(filenames) + } } #[cfg(test)] @@ -100,7 +117,7 @@ mod tests { .unwrap() .connect() .unwrap(); - conn.execute("CREATE TABLE IF NOT EXISTS flacs (path TEXT PRIMARY KEY, vendor TEXT, toencode BOOLEAN NOT NULL)", ()).await.unwrap(); + conn.execute("CREATE TABLE IF NOT EXISTS flacs (path TEXT PRIMARY KEY, toencode BOOLEAN NOT NULL)", ()).await.unwrap(); conn } @@ -128,10 +145,9 @@ mod tests { let _ = conn .execute( - "REPLACE INTO flacs (path, vendor, toencode) VALUES (?1, ?2, ?3)", + "REPLACE INTO flacs (path,, toencode) VALUES (?1, ?2)", params![ absolute(Path::new("16bit.flac")).unwrap().to_str(), - "", true ], ) diff --git a/src/main.rs b/src/main.rs index 6a67fc4..05e3bc2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ mod db; mod flac; +use anyhow::Result; #[tokio::main] -async fn main() { +async fn main() -> Result<()> { todo!() } |
