summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/db.rs10
-rw-r--r--src/files.rs29
2 files changed, 22 insertions, 17 deletions
diff --git a/src/db.rs b/src/db.rs
index 6622010..7ca313b 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -26,14 +26,10 @@ pub struct Database(pub Connection);
impl Database {
pub async fn new(path: impl AsRef<Path>) -> Result<Self> {
- if path.as_ref().is_file() {
- let conn = Builder::new_local(path).build().await?.connect()?;
- conn.execute(TABLE_CREATE, ()).await?;
+ let conn = Builder::new_local(path).build().await?.connect()?;
+ conn.execute(TABLE_CREATE, ()).await?;
- Ok(Database(conn))
- } else {
- Err(anyhow!("Not a file"))
- }
+ Ok(Database(conn))
}
pub async fn insert_file(&self, filename: impl AsRef<Path>) -> Result<()> {
diff --git a/src/files.rs b/src/files.rs
index 52b4058..7f346cc 100644
--- a/src/files.rs
+++ b/src/files.rs
@@ -1,5 +1,6 @@
use anyhow::{Result, anyhow};
use futures_util::StreamExt;
+#[cfg(not(test))]
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
use pin_utils::pin_mut;
use std::{
@@ -14,7 +15,9 @@ use walkdir::WalkDir;
use crate::{db::Database, flac::encode_file};
+#[cfg(not(test))]
const BAR_TEMPLATE: &str = "{msg:<} [{wide_bar:.green/cyan}] Elapsed: {elapsed} {pos:>7}/{len:7}";
+#[cfg(not(test))]
const SPINNER_TEMPLATE: &str = "Removed from db: {pos:.green}";
#[derive(Debug)]
@@ -84,7 +87,7 @@ pub async fn index_files_recursively(
let abspath = path.as_ref().canonicalize()?;
let mut tasks: JoinSet<Result<(), anyhow::Error>> = JoinSet::new();
-
+ #[cfg(not(test))]
let bar = ProgressBar::with_draw_target(Some(0), ProgressDrawTarget::stdout_with_hz(60))
.with_style(ProgressStyle::with_template(BAR_TEMPLATE)?.progress_chars("#>-"))
.with_message("Indexing");
@@ -97,13 +100,15 @@ pub async fn index_files_recursively(
if let Some(ext) = path.extension() {
if ext == "flac" {
let newconn = conn.clone();
-
+ #[cfg(not(test))]
let newbar = bar.clone();
tasks.spawn(async move {
handle_file(path, newconn).await?;
+ #[cfg(not(test))]
newbar.inc(1);
Ok(())
});
+ #[cfg(not(test))]
bar.inc_length(1);
}
}
@@ -122,7 +127,7 @@ pub async fn index_files_recursively(
_ => {}
}
}
-
+ #[cfg(not(test))]
bar.abandon_with_message("Indexing aborted");
return Ok(())
},
@@ -134,7 +139,7 @@ pub async fn index_files_recursively(
_ => {}
}
}
-
+ #[cfg(not(test))]
bar.finish_with_message("Finished indexing");
Ok(())
}
@@ -145,18 +150,20 @@ pub async fn reencode_files(conn: &Database, canceltoken: CancellationToken) ->
let mut tasks = JoinSet::new();
+ #[cfg(not(test))]
let bar = ProgressBar::with_draw_target(
Some(conn.get_toencode_number().await?),
ProgressDrawTarget::stdout_with_hz(60),
)
.with_style(ProgressStyle::with_template(BAR_TEMPLATE)?.progress_chars("#>-"))
- .with_message("Indexing");
+ .with_message("Reencoding");
while let Some(Ok(row)) = stream.next().await {
if let Some(file) = row.get_value(0)?.as_text() {
let filename = Path::new(file).canonicalize()?;
if filename.exists() {
let newconn = conn.clone();
+ #[cfg(not(test))]
let newbar = bar.clone();
tasks.spawn(async move {
let file = filename.clone();
@@ -169,6 +176,7 @@ pub async fn reencode_files(conn: &Database, canceltoken: CancellationToken) ->
if let Err(error) = newconn.update_file(&filename).await {
return Err(anyhow!(FileError::new(&filename, error)));
};
+ #[cfg(not(test))]
newbar.inc(1);
Ok(())
});
@@ -187,7 +195,7 @@ pub async fn reencode_files(conn: &Database, canceltoken: CancellationToken) ->
_ => {}
}
}
-
+ #[cfg(not(test))]
bar.abandon_with_message("Reencoding aborted");
return Ok(())
},
@@ -199,7 +207,7 @@ pub async fn reencode_files(conn: &Database, canceltoken: CancellationToken) ->
_ => {}
}
}
-
+ #[cfg(not(test))]
bar.finish_with_message("Finished encoding");
Ok(())
@@ -210,18 +218,19 @@ pub async fn clean_files(conn: &Database) -> Result<()> {
let query_res = conn.init_clean_files().await?;
pin_mut!(query_res);
-
+ #[cfg(not(test))]
let spinner = ProgressBar::with_draw_target(None, ProgressDrawTarget::stdout_with_hz(60))
.with_style(ProgressStyle::with_template(SPINNER_TEMPLATE)?);
while let Some(Ok(row)) = query_res.next().await {
let path = PathBuf::from(row.get_str(0)?);
let newconn = conn.clone();
-
+ #[cfg(not(test))]
let newspinner = spinner.clone();
tasks.spawn(async move {
if !path.exists() {
newconn.remove_file(path).await?;
+ #[cfg(not(test))]
newspinner.inc(1);
}
Ok(())
@@ -229,7 +238,7 @@ pub async fn clean_files(conn: &Database) -> Result<()> {
}
tasks.join_all().await;
-
+ #[cfg(not(test))]
spinner.finish();
conn.0.execute("VACUUM", ()).await?;