refactor adapters into modular file structure

config-sqlite: split into repository/ (per entity) + serialization/ (per type) + error.rs
http-api: split into dto/ (per resource) + routes/ (per resource)
tcp-server: split into broadcaster, event_bus, server, error
rss: split parser from adapter, external tests
media: split error, external tests
This commit is contained in:
2026-06-18 22:57:58 +02:00
parent 366d98a1ae
commit 6e77236936
37 changed files with 1428 additions and 1253 deletions

View File

@@ -0,0 +1,16 @@
#[derive(Debug)]
pub enum MediaError {
Request(reqwest::Error),
NoUrl,
Parse(String),
}
impl std::fmt::Display for MediaError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MediaError::Request(e) => write!(f, "request: {e}"),
MediaError::NoUrl => write!(f, "no url configured"),
MediaError::Parse(e) => write!(f, "parse: {e}"),
}
}
}

View File

@@ -1,3 +1,7 @@
mod error;
pub use error::MediaError;
use std::collections::BTreeMap;
use domain::{DataSource, DataSourcePort, Value};
@@ -5,23 +9,6 @@ pub struct MediaAdapter {
client: reqwest::Client,
}
#[derive(Debug)]
pub enum MediaError {
Request(reqwest::Error),
NoUrl,
Parse(String),
}
impl std::fmt::Display for MediaError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MediaError::Request(e) => write!(f, "request: {e}"),
MediaError::NoUrl => write!(f, "no url configured"),
MediaError::Parse(e) => write!(f, "parse: {e}"),
}
}
}
impl MediaAdapter {
pub fn new() -> Self {
Self {
@@ -75,84 +62,3 @@ impl DataSourcePort for MediaAdapter {
Ok(Value::Object(result))
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
use domain::{DataSourceConfig, DataSourceType};
fn subsonic_response(playing: bool) -> serde_json::Value {
if playing {
serde_json::json!({
"subsonic-response": {
"status": "ok",
"nowPlaying": {
"entry": [{
"title": "Believer",
"artist": "Imagine Dragons",
"album": "Evolve",
"duration": 204
}]
}
}
})
} else {
serde_json::json!({
"subsonic-response": {
"status": "ok",
"nowPlaying": {}
}
})
}
}
async fn start_fake_subsonic(playing: bool) -> String {
let app = axum::Router::new()
.route("/rest/getNowPlaying.view", axum::routing::get(move || async move {
axum::response::Json(subsonic_response(playing))
}));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
format!("http://{addr}")
}
fn make_source(url: String) -> DataSource {
DataSource {
id: 1,
name: "navidrome".into(),
source_type: DataSourceType::Media,
poll_interval: Duration::from_secs(5),
config: DataSourceConfig {
url: Some(url),
headers: vec![],
api_key: Some("testtoken".into()),
},
}
}
#[tokio::test]
async fn returns_now_playing_info() {
let base = start_fake_subsonic(true).await;
let adapter = MediaAdapter::new();
let source = make_source(base);
let result = adapter.poll(&source).await.unwrap();
assert_eq!(result.get_path("$.playing"), Some(&Value::Bool(true)));
assert_eq!(result.get_path("$.title"), Some(&Value::String("Believer".into())));
assert_eq!(result.get_path("$.artist"), Some(&Value::String("Imagine Dragons".into())));
}
#[tokio::test]
async fn returns_not_playing_when_empty() {
let base = start_fake_subsonic(false).await;
let adapter = MediaAdapter::new();
let source = make_source(base);
let result = adapter.poll(&source).await.unwrap();
assert_eq!(result.get_path("$.playing"), Some(&Value::Bool(false)));
}
}

View File

@@ -0,0 +1,77 @@
use std::time::Duration;
use domain::{DataSource, DataSourceConfig, DataSourcePort, DataSourceType, Value};
use media_adapter::MediaAdapter;
fn subsonic_response(playing: bool) -> serde_json::Value {
if playing {
serde_json::json!({
"subsonic-response": {
"status": "ok",
"nowPlaying": {
"entry": [{
"title": "Believer",
"artist": "Imagine Dragons",
"album": "Evolve",
"duration": 204
}]
}
}
})
} else {
serde_json::json!({
"subsonic-response": {
"status": "ok",
"nowPlaying": {}
}
})
}
}
async fn start_fake_subsonic(playing: bool) -> String {
let app = axum::Router::new()
.route("/rest/getNowPlaying.view", axum::routing::get(move || async move {
axum::response::Json(subsonic_response(playing))
}));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
format!("http://{addr}")
}
fn make_source(url: String) -> DataSource {
DataSource {
id: 1,
name: "navidrome".into(),
source_type: DataSourceType::Media,
poll_interval: Duration::from_secs(5),
config: DataSourceConfig {
url: Some(url),
headers: vec![],
api_key: Some("testtoken".into()),
},
}
}
#[tokio::test]
async fn returns_now_playing_info() {
let base = start_fake_subsonic(true).await;
let adapter = MediaAdapter::new();
let source = make_source(base);
let result = adapter.poll(&source).await.unwrap();
assert_eq!(result.get_path("$.playing"), Some(&Value::Bool(true)));
assert_eq!(result.get_path("$.title"), Some(&Value::String("Believer".into())));
assert_eq!(result.get_path("$.artist"), Some(&Value::String("Imagine Dragons".into())));
}
#[tokio::test]
async fn returns_not_playing_when_empty() {
let base = start_fake_subsonic(false).await;
let adapter = MediaAdapter::new();
let source = make_source(base);
let result = adapter.poll(&source).await.unwrap();
assert_eq!(result.get_path("$.playing"), Some(&Value::Bool(false)));
}