refactor: deduplicate content_type/role string converters in MCP tools

This commit is contained in:
2026-07-12 15:27:52 +02:00
parent 5561b70e1b
commit f52e024e98
3 changed files with 26 additions and 39 deletions

View File

@@ -94,20 +94,7 @@ struct RecentSyncDto {
items_found: u32, items_found: u32,
} }
fn content_type_to_str(ct: &domain::ContentType) -> &'static str { use super::{content_type_str, role_str};
match ct {
domain::ContentType::Movie => "movie",
domain::ContentType::Episode => "episode",
domain::ContentType::Short => "short",
}
}
fn role_to_str(r: &domain::MediaRole) -> &'static str {
match r {
domain::MediaRole::Program => "program",
domain::MediaRole::Interstitial => "interstitial",
}
}
fn item_to_dto(i: &domain::MediaItem) -> LibraryItemDto { fn item_to_dto(i: &domain::MediaItem) -> LibraryItemDto {
LibraryItemDto { LibraryItemDto {
@@ -115,7 +102,7 @@ fn item_to_dto(i: &domain::MediaItem) -> LibraryItemDto {
provider_id: i.provider_id().to_string(), provider_id: i.provider_id().to_string(),
external_id: i.external_id().to_string(), external_id: i.external_id().to_string(),
title: i.title().to_string(), title: i.title().to_string(),
content_type: content_type_to_str(i.content_type()).to_string(), content_type: content_type_str(i.content_type()).to_string(),
duration_secs: i.duration_secs(), duration_secs: i.duration_secs(),
series_name: i.series_name().map(|s| s.to_string()), series_name: i.series_name().map(|s| s.to_string()),
season_number: i.season_number(), season_number: i.season_number(),
@@ -125,7 +112,7 @@ fn item_to_dto(i: &domain::MediaItem) -> LibraryItemDto {
tags: i.tags().to_vec(), tags: i.tags().to_vec(),
collection_id: i.collection_id().map(|s| s.to_string()), collection_id: i.collection_id().map(|s| s.to_string()),
thumbnail_url: i.thumbnail_url().map(|s| s.to_string()), thumbnail_url: i.thumbnail_url().map(|s| s.to_string()),
role: role_to_str(i.role()).to_string(), role: role_str(i.role()).to_string(),
} }
} }
@@ -241,7 +228,7 @@ pub async fn browse_library(
.iter() .iter()
.filter(|i| { .filter(|i| {
if let Some(ref r) = role { if let Some(ref r) = role {
let item_role = role_to_str(i.role()); let item_role = role_str(i.role());
if item_role != r.as_str() { if item_role != r.as_str() {
return false; return false;
} }
@@ -270,13 +257,13 @@ pub async fn browse_library(
BrowseItemDto { BrowseItemDto {
id: i.id().value().to_string(), id: i.id().value().to_string(),
title: i.title().to_string(), title: i.title().to_string(),
content_type: content_type_to_str(i.content_type()).to_string(), content_type: content_type_str(i.content_type()).to_string(),
duration_mins: i.duration_secs() / 60, duration_mins: i.duration_secs() / 60,
series_name: i.series_name().map(|s| s.to_string()), series_name: i.series_name().map(|s| s.to_string()),
season_episode: se, season_episode: se,
year: i.year(), year: i.year(),
genres: i.genres().to_vec(), genres: i.genres().to_vec(),
role: role_to_str(i.role()).to_string(), role: role_str(i.role()).to_string(),
} }
}) })
.collect(); .collect();
@@ -337,10 +324,10 @@ pub async fn library_stats(
for item in &items { for item in &items {
*by_content_type *by_content_type
.entry(content_type_to_str(item.content_type()).to_string()) .entry(content_type_str(item.content_type()).to_string())
.or_default() += 1; .or_default() += 1;
*by_role *by_role
.entry(role_to_str(item.role()).to_string()) .entry(role_str(item.role()).to_string())
.or_default() += 1; .or_default() += 1;
for genre in item.genres() { for genre in item.genres() {
*genre_counts.entry(genre.clone()).or_default() += 1; *genre_counts.entry(genre.clone()).or_default() += 1;

View File

@@ -2,3 +2,18 @@ pub mod channels;
pub mod ical; pub mod ical;
pub mod library; pub mod library;
pub mod schedule; pub mod schedule;
pub(crate) fn content_type_str(ct: &domain::ContentType) -> &'static str {
match ct {
domain::ContentType::Movie => "movie",
domain::ContentType::Episode => "episode",
domain::ContentType::Short => "short",
}
}
pub(crate) fn role_str(r: &domain::MediaRole) -> &'static str {
match r {
domain::MediaRole::Program => "program",
domain::MediaRole::Interstitial => "interstitial",
}
}

View File

@@ -132,12 +132,7 @@ pub async fn analyze_schedule(
let entry = title_counts let entry = title_counts
.entry(title_key) .entry(title_key)
.or_insert_with(|| { .or_insert_with(|| {
let ct = match slot.item().content_type() { (super::content_type_str(slot.item().content_type()).to_string(), 0)
domain::ContentType::Movie => "movie",
domain::ContentType::Episode => "episode",
domain::ContentType::Short => "short",
};
(ct.to_string(), 0)
}); });
entry.1 += 1; entry.1 += 1;
@@ -261,12 +256,7 @@ pub async fn preview_schedule(
start_at: s.start_at().to_rfc3339(), start_at: s.start_at().to_rfc3339(),
end_at: s.end_at().to_rfc3339(), end_at: s.end_at().to_rfc3339(),
title: s.item().title().to_string(), title: s.item().title().to_string(),
content_type: match s.item().content_type() { content_type: super::content_type_str(s.item().content_type()).to_string(),
domain::ContentType::Movie => "movie",
domain::ContentType::Episode => "episode",
domain::ContentType::Short => "short",
}
.to_string(),
duration_mins: (s.end_at() - s.start_at()).num_seconds() as f64 / 60.0, duration_mins: (s.end_at() - s.start_at()).num_seconds() as f64 / 60.0,
block_id: s.source_block_id().to_string(), block_id: s.source_block_id().to_string(),
}) })
@@ -318,12 +308,7 @@ pub async fn preview_config(
start_at: s.start_at().to_rfc3339(), start_at: s.start_at().to_rfc3339(),
end_at: s.end_at().to_rfc3339(), end_at: s.end_at().to_rfc3339(),
title: s.item().title().to_string(), title: s.item().title().to_string(),
content_type: match s.item().content_type() { content_type: super::content_type_str(s.item().content_type()).to_string(),
domain::ContentType::Movie => "movie",
domain::ContentType::Episode => "episode",
domain::ContentType::Short => "short",
}
.to_string(),
duration_mins: (s.end_at() - s.start_at()).num_seconds() as f64 / 60.0, duration_mins: (s.end_at() - s.start_at()).num_seconds() as f64 / 60.0,
block_id: s.source_block_id().to_string(), block_id: s.source_block_id().to_string(),
}) })