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 RssError {
Request(reqwest::Error),
NoUrl,
Parse(String),
}
impl std::fmt::Display for RssError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RssError::Request(e) => write!(f, "request: {e}"),
RssError::NoUrl => write!(f, "no url configured"),
RssError::Parse(e) => write!(f, "parse: {e}"),
}
}
}

View File

@@ -1,29 +1,15 @@
use std::collections::BTreeMap;
mod error;
mod parser;
pub use error::RssError;
pub use parser::parse_rss;
use domain::{DataSource, DataSourcePort, Value};
use quick_xml::events::Event;
use quick_xml::Reader;
pub struct RssAdapter {
client: reqwest::Client,
}
#[derive(Debug)]
pub enum RssError {
Request(reqwest::Error),
NoUrl,
Parse(String),
}
impl std::fmt::Display for RssError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RssError::Request(e) => write!(f, "request: {e}"),
RssError::NoUrl => write!(f, "no url configured"),
RssError::Parse(e) => write!(f, "parse: {e}"),
}
}
}
impl RssAdapter {
pub fn new() -> Self {
Self {
@@ -32,71 +18,6 @@ impl RssAdapter {
}
}
fn parse_rss(xml: &str) -> Result<Value, RssError> {
let mut reader = Reader::from_str(xml);
let mut items: Vec<Value> = Vec::new();
let mut current_item: Option<BTreeMap<String, Value>> = None;
let mut current_tag = String::new();
let mut in_channel = false;
let mut channel_title = String::new();
let mut channel_link = String::new();
loop {
match reader.read_event() {
Ok(Event::Start(e)) => {
let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
match tag.as_str() {
"channel" => in_channel = true,
"item" => { current_item = Some(BTreeMap::new()); }
_ => current_tag = tag,
}
}
Ok(Event::End(e)) => {
let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
if tag == "item" {
if let Some(item) = current_item.take() {
items.push(Value::Object(item));
}
}
current_tag.clear();
}
Ok(Event::Text(e)) => {
let text = e.unescape().unwrap_or_default().to_string();
if !current_tag.is_empty() && !text.trim().is_empty() {
if let Some(item) = current_item.as_mut() {
item.insert(current_tag.clone(), Value::String(text));
} else if in_channel {
match current_tag.as_str() {
"title" => channel_title = text,
"link" => channel_link = text,
_ => {}
}
}
}
}
Ok(Event::CData(e)) => {
let text = String::from_utf8_lossy(&e).to_string();
if !current_tag.is_empty() {
if let Some(item) = current_item.as_mut() {
item.insert(current_tag.clone(), Value::String(text));
}
}
}
Ok(Event::Eof) => break,
Err(e) => return Err(RssError::Parse(format!("{e}"))),
_ => {}
}
}
let mut result = BTreeMap::new();
result.insert("title".into(), Value::String(channel_title));
result.insert("link".into(), Value::String(channel_link));
result.insert("count".into(), Value::Number(items.len() as f64));
result.insert("items".into(), Value::Array(items));
Ok(Value::Object(result))
}
impl DataSourcePort for RssAdapter {
type Error = RssError;
@@ -106,39 +27,6 @@ impl DataSourcePort for RssAdapter {
let resp = self.client.get(url).send().await.map_err(RssError::Request)?;
let xml = resp.text().await.map_err(RssError::Request)?;
parse_rss(&xml)
}
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE_RSS: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Test Feed</title>
<link>https://example.com</link>
<item>
<title>First Article</title>
<description>Description of first article</description>
<link>https://example.com/1</link>
</item>
<item>
<title>Second Article</title>
<description>Description of second</description>
<link>https://example.com/2</link>
</item>
</channel>
</rss>"#;
#[test]
fn parses_rss_into_value() {
let result = parse_rss(SAMPLE_RSS).unwrap();
assert_eq!(result.get_path("$.title"), Some(&Value::String("Test Feed".into())));
assert_eq!(result.get_path("$.items[0].title"), Some(&Value::String("First Article".into())));
assert_eq!(result.get_path("$.items[1].title"), Some(&Value::String("Second Article".into())));
assert_eq!(result.get_path("$.items[0].description"), Some(&Value::String("Description of first article".into())));
parser::parse_rss(&xml)
}
}

View File

@@ -0,0 +1,74 @@
use std::collections::BTreeMap;
use domain::Value;
use quick_xml::Reader;
use quick_xml::events::Event;
use crate::error::RssError;
pub fn parse_rss(xml: &str) -> Result<Value, RssError> {
let mut reader = Reader::from_str(xml);
let mut items: Vec<Value> = Vec::new();
let mut current_item: Option<BTreeMap<String, Value>> = None;
let mut current_tag = String::new();
let mut in_channel = false;
let mut channel_title = String::new();
let mut channel_link = String::new();
loop {
match reader.read_event() {
Ok(Event::Start(e)) => {
let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
match tag.as_str() {
"channel" => in_channel = true,
"item" => {
current_item = Some(BTreeMap::new());
}
_ => current_tag = tag,
}
}
Ok(Event::End(e)) => {
let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
if tag == "item" {
if let Some(item) = current_item.take() {
items.push(Value::Object(item));
}
}
current_tag.clear();
}
Ok(Event::Text(e)) => {
let text = e.unescape().unwrap_or_default().to_string();
if !current_tag.is_empty() && !text.trim().is_empty() {
if let Some(item) = current_item.as_mut() {
item.insert(current_tag.clone(), Value::String(text));
} else if in_channel {
match current_tag.as_str() {
"title" => channel_title = text,
"link" => channel_link = text,
_ => {}
}
}
}
}
Ok(Event::CData(e)) => {
let text = String::from_utf8_lossy(&e).to_string();
if !current_tag.is_empty() {
if let Some(item) = current_item.as_mut() {
item.insert(current_tag.clone(), Value::String(text));
}
}
}
Ok(Event::Eof) => break,
Err(e) => return Err(RssError::Parse(format!("{e}"))),
_ => {}
}
}
let mut result = BTreeMap::new();
result.insert("title".into(), Value::String(channel_title));
result.insert("link".into(), Value::String(channel_link));
result.insert("count".into(), Value::Number(items.len() as f64));
result.insert("items".into(), Value::Array(items));
Ok(Value::Object(result))
}