feat: add more scanned properties, switch over to diesel from rusqlite (with first migration)
This commit is contained in:
84
src/main.rs
84
src/main.rs
@@ -1,57 +1,58 @@
|
||||
use std::{fs::metadata, path::Path};
|
||||
|
||||
use lofty::ogg::tag;
|
||||
use rusqlite::{Connection, Result};
|
||||
use diesel::prelude::*;
|
||||
use dotenvy::dotenv;
|
||||
use std::env;
|
||||
|
||||
mod library_scan;
|
||||
mod types;
|
||||
mod models;
|
||||
mod schema;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
println!("Hello, world!");
|
||||
fn open_db_file() -> SqliteConnection {
|
||||
dotenv().ok();
|
||||
|
||||
let conn = Connection::open_in_memory()?;
|
||||
let database_url = env::var("DATABASE_URL")
|
||||
.expect("DATABASE_URL missing");
|
||||
|
||||
println!("Initializing database...");
|
||||
SqliteConnection::establish(&database_url)
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
||||
}
|
||||
|
||||
conn.execute(
|
||||
"CREATE TABLE artist (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL
|
||||
)",
|
||||
(),
|
||||
)?;
|
||||
|
||||
conn.execute(
|
||||
"CREATE TABLE collection (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL
|
||||
)",
|
||||
(),
|
||||
)?;
|
||||
|
||||
conn.execute(
|
||||
"CREATE TABLE track (
|
||||
id INTEGER PRIMARY KEY,
|
||||
artist INTEGER,
|
||||
collection INTEGER,
|
||||
title TEXT
|
||||
)",
|
||||
(),
|
||||
)?;
|
||||
|
||||
println!("Database initialized.");
|
||||
fn open_db_in_memory() -> SqliteConnection {
|
||||
SqliteConnection::establish(":memory:")
|
||||
.unwrap_or_else(|_| panic!("Error starting in-memory database"))
|
||||
}
|
||||
|
||||
fn import_scan(conn: &mut SqliteConnection) {
|
||||
for track in library_scan::scan(Path::new("./test/fixtures")).expect("oh no") {
|
||||
println!("-- {}", track.path.into_string().unwrap());
|
||||
|
||||
match track.metadata {
|
||||
Ok(metadata) => {
|
||||
println!("artist: {}", metadata.artist.unwrap_or("unknown".into()));
|
||||
println!("artist: {}", metadata.artist.as_deref().unwrap_or("unknown"));
|
||||
println!(
|
||||
"collection: {}",
|
||||
metadata.collection.unwrap_or("unknown".into())
|
||||
metadata.collection.as_deref().unwrap_or("unknown")
|
||||
);
|
||||
println!("title: {}", metadata.title.unwrap_or("unknown".into()));
|
||||
println!("title: {}", metadata.title.as_deref().unwrap_or("unknown"));
|
||||
println!(
|
||||
"number: {}",
|
||||
metadata
|
||||
.number
|
||||
.map(|value| value.to_string())
|
||||
.unwrap_or_else(|| "unknown".to_owned())
|
||||
);
|
||||
println!("genre: {}", metadata.genre.as_deref().unwrap_or("unknown"));
|
||||
println!(
|
||||
"datetime: {}",
|
||||
metadata
|
||||
.datetime
|
||||
.as_ref()
|
||||
.map(|value| format!("{value:?}"))
|
||||
.unwrap_or_else(|| "unknown".to_owned())
|
||||
);
|
||||
println!("length_sec: {}", metadata.length_sec);
|
||||
}
|
||||
Err(e) => {
|
||||
eprint!("{e}");
|
||||
@@ -59,6 +60,13 @@ fn main() -> Result<()> {
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
let conn = &mut open_db_in_memory();
|
||||
|
||||
import_scan(conn);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user