summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjakka <jakkadoujin@gmail.com>2025-06-11 23:01:05 +0300
committerjakka <jakkadoujin@gmail.com>2025-06-11 23:01:05 +0300
commit382e526f76ada0ca58f560a1217cb9a597ba3945 (patch)
tree8234370be62b2939edcd5c79029c8bbe99d89a15 /src
parent4d9ff07589cf9bb6ca986a643c523196945f9937 (diff)
minor db logic changes
Diffstat (limited to 'src')
-rw-r--r--src/db.rs42
-rw-r--r--src/main.rs3
2 files changed, 31 insertions, 14 deletions
diff --git a/src/db.rs b/src/db.rs
index e42d235..9346575 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -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!()
}