artifacts: rewrite the entire artifact querying layer to create modular 'tools' and 'datasource's

This commit is contained in:
2026-06-17 11:09:50 +02:00
parent 33e0b1768f
commit 3a8130d785
11 changed files with 672 additions and 257 deletions
+48 -25
View File
@@ -1,8 +1,8 @@
use std::collections::HashSet;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use sqlite::OpenFlags;
use crate::artifacts::{Album, Artifact, Artist, SourceID, Track};
use crate::artifacts::{Album, Artifact, ArtifactBuilder, Artist, SourceID, Track, tools::{DataSource, ToolDescription}};
#[derive(Debug)]
#[allow(unused)]
@@ -16,11 +16,24 @@ impl From<sqlite::Error> for MixxxError {
}
}
pub struct MixxxDB(());
pub struct MixxxDB;
impl MixxxDB {
pub fn load(playlist_name: &str) -> Result<Vec<Artifact>, MixxxError> {
#[derive(Serialize, Deserialize, Debug, Default, JsonSchema)]
pub struct MixxxQuery {
pub playlist_name: String
}
impl DataSource for MixxxDB {
type Args = MixxxQuery;
type Error = MixxxError;
async fn synchronize(&mut self, _artifact: &mut Artifact) -> Result<Vec<Artifact>, Self::Error> {
Ok(vec![])
}
async fn query(&mut self, args: &Self::Args) -> Result<Vec<Artifact>, Self::Error> {
let mut ret = vec![];
let playlist_name = args.playlist_name.as_str();
log::info!("Loading Mixxx playlist {}", playlist_name);
let connection = sqlite::Connection::open_thread_safe_with_flags("mixxxdb.sqlite", OpenFlags::new().with_read_only())?;
let query = "SELECT id FROM Playlists WHERE name = ? ORDER BY id DESC LIMIT 1";
@@ -36,29 +49,39 @@ impl MixxxDB {
let artist = track.try_read::<&str, _>("artist").unwrap_or("Unknown Artist");
let album = track.try_read::<&str, _>("album").unwrap_or("Unknown Album");
let bpm = track.try_read::<f64, _>("bpm").unwrap_or(0.);
ret.push(Artifact::Track(Track {
artist: Some(artist.into()),
album: Some(album.into()),
title: title.into(),
bpm: Some(bpm),
sources: HashSet::from([SourceID::Mixxx]),
..Default::default()
}));
ret.push(ArtifactBuilder::new(SourceID::Mixxx)
.contents(Track {
artist: Some(artist.into()),
album: Some(album.into()),
title: title.into(),
bpm: Some(bpm),
..Default::default()
}).build());
ret.push(Artifact::Album(Album {
artist: artist.into(),
title: album.into(),
sources: HashSet::from([SourceID::Mixxx]),
..Default::default()
}));
ret.push(ArtifactBuilder::new(SourceID::Mixxx)
.contents(Album {
artist: artist.into(),
title: album.into(),
..Default::default()
}).build());
ret.push(Artifact::Artist(Artist {
name: artist.into(),
sources: HashSet::from([SourceID::Mixxx]),
..Default::default()
}));
ret.push(ArtifactBuilder::new(SourceID::Mixxx)
.contents(Artist {
name: artist.into(),
..Default::default()
}).build());
}
Ok(ret)
}
}
impl ToolDescription for MixxxDB {
fn description(&self) -> &str {
"Loads artifacts from a given Mixxx playlist name"
}
fn name(&self) -> &str {
"query_mixxx"
}
}