2025-10-01 20:49:50 +02:00
|
|
|
//! # pmodidl - DIDL-Lite Parser
|
|
|
|
|
//!
|
|
|
|
|
//! Parser et utilitaires pour le format DIDL-Lite utilisé dans UPnP/DLNA.
|
|
|
|
|
|
2025-10-19 13:42:29 +02:00
|
|
|
use bevy_reflect::Reflect;
|
2025-10-01 20:49:50 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
|
|
|
|
|
// ============= Couche d'abstraction générique =============
|
|
|
|
|
|
|
|
|
|
/// Trait pour tout parser de métadonnées média
|
|
|
|
|
pub trait MediaMetadataParser: Sized {
|
|
|
|
|
type Error: std::error::Error + Send + Sync + 'static;
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Parse une chaîne de métadonnées
|
|
|
|
|
fn parse(input: &str) -> Result<Self, Self::Error>;
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Retourne le format du parser
|
|
|
|
|
fn format_name() -> &'static str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Enveloppe générique pour tout type de métadonnées parsées
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Reflect)]
|
|
|
|
|
pub struct ParsedMetadata<T> {
|
|
|
|
|
/// Format du document (ex: "DIDL-Lite", "RSS", etc.)
|
|
|
|
|
pub format: String,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Données parsées
|
|
|
|
|
pub data: T,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Timestamp du parsing (exclu de la réflexion car SystemTime n'implémente pas Reflect)
|
|
|
|
|
#[reflect(ignore)]
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub parsed_at: Option<std::time::SystemTime>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> ParsedMetadata<T> {
|
|
|
|
|
pub fn new(format: impl Into<String>, data: T) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
format: format.into(),
|
|
|
|
|
data,
|
|
|
|
|
parsed_at: Some(std::time::SystemTime::now()),
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Transforme les données avec une fonction
|
|
|
|
|
pub fn map<U, F>(self, f: F) -> ParsedMetadata<U>
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(T) -> U,
|
|
|
|
|
{
|
|
|
|
|
ParsedMetadata {
|
|
|
|
|
format: self.format,
|
|
|
|
|
data: f(self.data),
|
|
|
|
|
parsed_at: self.parsed_at,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Fonction helper pour parser et envelopper automatiquement
|
|
|
|
|
pub fn parse_metadata<P: MediaMetadataParser>(input: &str) -> Result<ParsedMetadata<P>, P::Error> {
|
|
|
|
|
let data = P::parse(input)?;
|
|
|
|
|
Ok(ParsedMetadata::new(P::format_name(), data))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ============= Implémentation pour DIDLLite =============
|
|
|
|
|
|
|
|
|
|
impl MediaMetadataParser for DIDLLite {
|
|
|
|
|
type Error = quick_xml::de::DeError;
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
fn parse(input: &str) -> Result<Self, Self::Error> {
|
|
|
|
|
quick_xml::de::from_str(input)
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
fn format_name() -> &'static str {
|
|
|
|
|
"DIDL-Lite"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Type alias pour faciliter l'utilisation
|
|
|
|
|
pub type DidlMetadata = ParsedMetadata<DIDLLite>;
|
|
|
|
|
|
|
|
|
|
// ============= Structures DIDL-Lite =============
|
|
|
|
|
|
|
|
|
|
/// Racine d'un document DIDL-Lite
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema, Reflect)]
|
|
|
|
|
#[serde(rename = "DIDL-Lite")]
|
|
|
|
|
pub struct DIDLLite {
|
|
|
|
|
#[serde(rename = "@xmlns")]
|
|
|
|
|
pub xmlns: String,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@xmlns:upnp", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub xmlns_upnp: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@xmlns:dc", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub xmlns_dc: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@xmlns:dlna", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub xmlns_dlna: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@xmlns:sec", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub xmlns_sec: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@xmlns:pv", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub xmlns_pv: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "container", default)]
|
|
|
|
|
pub containers: Vec<Container>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "item", default)]
|
|
|
|
|
pub items: Vec<Item>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Container pouvant contenir d'autres containers ou items
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema, Reflect)]
|
|
|
|
|
pub struct Container {
|
|
|
|
|
#[serde(rename = "@id")]
|
|
|
|
|
pub id: String,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@parentID")]
|
|
|
|
|
pub parent_id: String,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@restricted", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub restricted: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@childCount", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub child_count: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-26 23:49:20 +01:00
|
|
|
#[serde(rename = "@searchable", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub searchable: Option<String>,
|
|
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "dc:title", alias = "title")]
|
|
|
|
|
pub title: String,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "upnp:class", alias = "class")]
|
|
|
|
|
pub class: String,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "container", default)]
|
|
|
|
|
pub containers: Vec<Container>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "item", default)]
|
|
|
|
|
pub items: Vec<Item>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Item représentant un objet audio
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema, Reflect)]
|
|
|
|
|
pub struct Item {
|
|
|
|
|
#[serde(rename = "@id")]
|
|
|
|
|
pub id: String,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@parentID")]
|
|
|
|
|
pub parent_id: String,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@restricted", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub restricted: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "dc:title", alias = "title")]
|
|
|
|
|
pub title: String,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
|
|
|
|
#[serde(
|
|
|
|
|
rename = "dc:creator",
|
|
|
|
|
alias = "creator",
|
|
|
|
|
skip_serializing_if = "Option::is_none"
|
|
|
|
|
)]
|
2025-10-01 20:49:50 +02:00
|
|
|
pub creator: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "upnp:class", alias = "class")]
|
|
|
|
|
pub class: String,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
|
|
|
|
#[serde(
|
|
|
|
|
rename = "upnp:artist",
|
|
|
|
|
alias = "artist",
|
|
|
|
|
skip_serializing_if = "Option::is_none"
|
|
|
|
|
)]
|
2025-10-01 20:49:50 +02:00
|
|
|
pub artist: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
|
|
|
|
#[serde(
|
|
|
|
|
rename = "upnp:album",
|
|
|
|
|
alias = "album",
|
|
|
|
|
skip_serializing_if = "Option::is_none"
|
|
|
|
|
)]
|
2025-10-01 20:49:50 +02:00
|
|
|
pub album: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
|
|
|
|
#[serde(
|
|
|
|
|
rename = "upnp:genre",
|
|
|
|
|
alias = "genre",
|
|
|
|
|
skip_serializing_if = "Option::is_none"
|
|
|
|
|
)]
|
2025-10-01 20:49:50 +02:00
|
|
|
pub genre: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
|
|
|
|
#[serde(
|
|
|
|
|
rename = "upnp:albumArtURI",
|
|
|
|
|
alias = "albumArtURI",
|
|
|
|
|
skip_serializing_if = "Option::is_none"
|
|
|
|
|
)]
|
2025-10-01 20:49:50 +02:00
|
|
|
pub album_art: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(skip)]
|
|
|
|
|
pub album_art_pk: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
|
|
|
|
#[serde(
|
|
|
|
|
rename = "dc:date",
|
|
|
|
|
alias = "date",
|
|
|
|
|
skip_serializing_if = "Option::is_none"
|
|
|
|
|
)]
|
2025-10-01 20:49:50 +02:00
|
|
|
pub date: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
|
|
|
|
#[serde(
|
|
|
|
|
rename = "upnp:originalTrackNumber",
|
|
|
|
|
alias = "originalTrackNumber",
|
|
|
|
|
skip_serializing_if = "Option::is_none"
|
|
|
|
|
)]
|
2025-10-01 20:49:50 +02:00
|
|
|
pub original_track_number: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "res", default)]
|
|
|
|
|
pub resources: Vec<Resource>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "desc", default)]
|
|
|
|
|
pub descriptions: Vec<Description>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Ressource média (fichier audio)
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema, Reflect)]
|
|
|
|
|
pub struct Resource {
|
|
|
|
|
#[serde(rename = "@protocolInfo")]
|
|
|
|
|
pub protocol_info: String,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@bitsPerSample", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub bits_per_sample: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@sampleFrequency", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub sample_frequency: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@nrAudioChannels", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub nr_audio_channels: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@duration", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub duration: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "$text")]
|
|
|
|
|
pub url: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Description avec métadonnées additionnelles (replaygain, etc.)
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema, Reflect)]
|
|
|
|
|
pub struct Description {
|
|
|
|
|
#[serde(rename = "@id", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub id: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "@nameSpace", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub namespace: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "track_gain", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub track_gain: Option<String>,
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[serde(rename = "track_peak", skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub track_peak: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ============= Implémentation des méthodes =============
|
|
|
|
|
|
2025-10-25 22:24:18 +02:00
|
|
|
impl Default for DIDLLite {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
xmlns: "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/".to_string(),
|
|
|
|
|
xmlns_upnp: Some("urn:schemas-upnp-org:metadata-1-0/upnp/".to_string()),
|
|
|
|
|
xmlns_dc: Some("http://purl.org/dc/elements/1.1/".to_string()),
|
|
|
|
|
xmlns_dlna: None,
|
|
|
|
|
xmlns_sec: None,
|
|
|
|
|
xmlns_pv: None,
|
|
|
|
|
containers: Vec::new(),
|
|
|
|
|
items: Vec::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
impl DIDLLite {
|
|
|
|
|
/// Itère sur tous les containers de manière récursive
|
|
|
|
|
pub fn all_containers(&self) -> impl Iterator<Item = &Container> {
|
|
|
|
|
AllContainersIter::new(&self.containers)
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Itère sur tous les items de manière récursive
|
|
|
|
|
pub fn all_items(&self) -> impl Iterator<Item = &Item> {
|
|
|
|
|
AllItemsIter::new(&self.containers, &self.items)
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Trouve un container par ID
|
|
|
|
|
pub fn get_container_by_id(&self, id: &str) -> Option<&Container> {
|
|
|
|
|
self.all_containers().find(|c| c.id == id)
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Trouve un item par ID
|
|
|
|
|
pub fn get_item_by_id(&self, id: &str) -> Option<&Item> {
|
|
|
|
|
self.all_items().find(|i| i.id == id)
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Filtre les containers
|
|
|
|
|
pub fn filter_containers<F>(&self, predicate: F) -> impl Iterator<Item = &Container>
|
|
|
|
|
where
|
|
|
|
|
F: Fn(&Container) -> bool,
|
|
|
|
|
{
|
|
|
|
|
self.all_containers().filter(move |c| predicate(c))
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Filtre les items
|
|
|
|
|
pub fn filter_items<F>(&self, predicate: F) -> impl Iterator<Item = &Item>
|
|
|
|
|
where
|
|
|
|
|
F: Fn(&Item) -> bool,
|
|
|
|
|
{
|
|
|
|
|
self.all_items().filter(move |i| predicate(i))
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Génère une représentation Markdown
|
|
|
|
|
pub fn to_markdown(&self) -> String {
|
|
|
|
|
let mut buf = String::new();
|
|
|
|
|
buf.push_str("### DIDL-Lite Document\n\n");
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
if !self.containers.is_empty() {
|
|
|
|
|
buf.push_str("#### Containers\n\n");
|
|
|
|
|
for container in &self.containers {
|
|
|
|
|
container.write_markdown(&mut buf, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
if !self.items.is_empty() {
|
|
|
|
|
buf.push_str("#### Items\n\n");
|
|
|
|
|
for item in &self.items {
|
|
|
|
|
item.write_markdown(&mut buf, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
buf
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Container {
|
|
|
|
|
/// Itère sur tous les containers enfants récursivement
|
|
|
|
|
pub fn all_containers(&self) -> impl Iterator<Item = &Container> {
|
|
|
|
|
AllContainersIter::new(&self.containers)
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Itère sur tous les items de ce container et ses enfants
|
|
|
|
|
pub fn all_items(&self) -> impl Iterator<Item = &Item> {
|
|
|
|
|
AllItemsIter::new(&self.containers, &self.items)
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
fn write_markdown(&self, buf: &mut String, depth: usize) {
|
|
|
|
|
let indent = " ".repeat(depth);
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
writeln!(buf, "{}- **Container**: {}", indent, self.title).unwrap();
|
|
|
|
|
writeln!(buf, "{} - ID: `{}`", indent, self.id).unwrap();
|
|
|
|
|
writeln!(buf, "{} - ParentID: `{}`", indent, self.parent_id).unwrap();
|
|
|
|
|
writeln!(buf, "{} - Class: `{}`", indent, self.class).unwrap();
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
if let Some(ref restricted) = self.restricted {
|
|
|
|
|
writeln!(buf, "{} - Restricted: `{}`", indent, restricted).unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref count) = self.child_count {
|
|
|
|
|
writeln!(buf, "{} - ChildCount: `{}`", indent, count).unwrap();
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
if !self.containers.is_empty() {
|
|
|
|
|
writeln!(buf, "{} - Subcontainers:", indent).unwrap();
|
|
|
|
|
for sub in &self.containers {
|
|
|
|
|
sub.write_markdown(buf, depth + 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
if !self.items.is_empty() {
|
|
|
|
|
writeln!(buf, "{} - Items:", indent).unwrap();
|
|
|
|
|
for item in &self.items {
|
|
|
|
|
item.write_markdown(buf, depth + 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
buf.push('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Item {
|
|
|
|
|
/// Itère sur les ressources audio uniquement
|
|
|
|
|
pub fn audio_resources(&self) -> impl Iterator<Item = &Resource> {
|
2025-10-19 13:42:29 +02:00
|
|
|
self.resources
|
|
|
|
|
.iter()
|
2025-10-01 20:49:50 +02:00
|
|
|
.filter(|r| r.protocol_info.contains("audio/"))
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Retourne la ressource principale (première disponible)
|
|
|
|
|
pub fn primary_resource(&self) -> Option<&Resource> {
|
|
|
|
|
self.resources.first()
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
/// Itère sur les métadonnées sous forme de paires clé-valeur
|
|
|
|
|
pub fn metadata(&self) -> impl Iterator<Item = (&str, &str)> {
|
|
|
|
|
let mut pairs = Vec::new();
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
pairs.push(("title", self.title.as_str()));
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
if let Some(ref artist) = self.artist {
|
|
|
|
|
pairs.push(("artist", artist.as_str()));
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref album) = self.album {
|
|
|
|
|
pairs.push(("album", album.as_str()));
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref genre) = self.genre {
|
|
|
|
|
pairs.push(("genre", genre.as_str()));
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref date) = self.date {
|
|
|
|
|
pairs.push(("date", date.as_str()));
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref track) = self.original_track_number {
|
|
|
|
|
pairs.push(("trackNumber", track.as_str()));
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
for desc in &self.descriptions {
|
|
|
|
|
if let Some(ref gain) = desc.track_gain {
|
|
|
|
|
pairs.push(("replayGain", gain.as_str()));
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref peak) = desc.track_peak {
|
|
|
|
|
pairs.push(("replayPeak", peak.as_str()));
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
pairs.into_iter()
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
fn write_markdown(&self, buf: &mut String, depth: usize) {
|
|
|
|
|
let indent = " ".repeat(depth);
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
writeln!(buf, "{}- **Item**: {}", indent, self.title).unwrap();
|
|
|
|
|
writeln!(buf, "{} - ID: `{}`", indent, self.id).unwrap();
|
|
|
|
|
writeln!(buf, "{} - ParentID: `{}`", indent, self.parent_id).unwrap();
|
|
|
|
|
writeln!(buf, "{} - Class: `{}`", indent, self.class).unwrap();
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
if let Some(ref creator) = self.creator {
|
|
|
|
|
writeln!(buf, "{} - Creator: {}", indent, creator).unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref artist) = self.artist {
|
|
|
|
|
writeln!(buf, "{} - Artist: {}", indent, artist).unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref album) = self.album {
|
|
|
|
|
writeln!(buf, "{} - Album: {}", indent, album).unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref genre) = self.genre {
|
|
|
|
|
writeln!(buf, "{} - Genre: {}", indent, genre).unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref art) = self.album_art {
|
|
|
|
|
writeln!(buf, "{} - Album Art: ", indent, art).unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref date) = self.date {
|
|
|
|
|
writeln!(buf, "{} - Date: {}", indent, date).unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref track) = self.original_track_number {
|
|
|
|
|
writeln!(buf, "{} - Track: {}", indent, track).unwrap();
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
if !self.resources.is_empty() {
|
|
|
|
|
writeln!(buf, "{} - Resources:", indent).unwrap();
|
|
|
|
|
for res in &self.resources {
|
|
|
|
|
writeln!(buf, "{} - URL: {}", indent, res.url).unwrap();
|
|
|
|
|
writeln!(buf, "{} - Protocol: `{}`", indent, res.protocol_info).unwrap();
|
|
|
|
|
if let Some(ref dur) = res.duration {
|
|
|
|
|
writeln!(buf, "{} - Duration: `{}`", indent, dur).unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref bits) = res.bits_per_sample {
|
|
|
|
|
writeln!(buf, "{} - BitsPerSample: `{}`", indent, bits).unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref freq) = res.sample_frequency {
|
|
|
|
|
writeln!(buf, "{} - SampleFrequency: `{}`", indent, freq).unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref channels) = res.nr_audio_channels {
|
|
|
|
|
writeln!(buf, "{} - Channels: `{}`", indent, channels).unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
if !self.descriptions.is_empty() {
|
|
|
|
|
writeln!(buf, "{} - Descriptions:", indent).unwrap();
|
|
|
|
|
for desc in &self.descriptions {
|
|
|
|
|
if let Some(ref ns) = desc.namespace {
|
|
|
|
|
writeln!(buf, "{} - Namespace: `{}`", indent, ns).unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref gain) = desc.track_gain {
|
|
|
|
|
writeln!(buf, "{} - Track Gain: `{}`", indent, gain).unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref peak) = desc.track_peak {
|
|
|
|
|
writeln!(buf, "{} - Track Peak: `{}`", indent, peak).unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
buf.push('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ============= Itérateurs personnalisés =============
|
|
|
|
|
|
|
|
|
|
struct AllContainersIter<'a> {
|
|
|
|
|
stack: Vec<&'a Container>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> AllContainersIter<'a> {
|
|
|
|
|
fn new(containers: &'a [Container]) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
stack: containers.iter().collect(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Iterator for AllContainersIter<'a> {
|
|
|
|
|
type Item = &'a Container;
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
|
self.stack.pop().map(|container| {
|
|
|
|
|
// Ajouter les enfants à la pile
|
|
|
|
|
self.stack.extend(container.containers.iter());
|
|
|
|
|
container
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct AllItemsIter<'a> {
|
|
|
|
|
containers: Vec<&'a Container>,
|
|
|
|
|
current_items: std::slice::Iter<'a, Item>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> AllItemsIter<'a> {
|
|
|
|
|
fn new(containers: &'a [Container], items: &'a [Item]) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
containers: containers.iter().collect(),
|
|
|
|
|
current_items: items.iter(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Iterator for AllItemsIter<'a> {
|
|
|
|
|
type Item = &'a Item;
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
|
loop {
|
|
|
|
|
if let Some(item) = self.current_items.next() {
|
|
|
|
|
return Some(item);
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
let container = self.containers.pop()?;
|
|
|
|
|
self.containers.extend(container.containers.iter());
|
|
|
|
|
self.current_items = container.items.iter();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_parse_simple_didl() {
|
|
|
|
|
let xml = r#"
|
|
|
|
|
<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"
|
|
|
|
|
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
|
|
|
|
xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/">
|
|
|
|
|
<item id="1" parentID="0">
|
|
|
|
|
<dc:title>Test Song</dc:title>
|
|
|
|
|
<upnp:class>object.item.audioItem.musicTrack</upnp:class>
|
|
|
|
|
<res protocolInfo="http-get:*:audio/mpeg:*">http://example.com/song.mp3</res>
|
|
|
|
|
</item>
|
|
|
|
|
</DIDL-Lite>
|
|
|
|
|
"#;
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
let didl = DIDLLite::parse(xml).unwrap();
|
|
|
|
|
assert_eq!(didl.items.len(), 1);
|
|
|
|
|
assert_eq!(didl.items[0].title, "Test Song");
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_parse_without_namespaces() {
|
|
|
|
|
// Teste un XML sans namespaces explicites (devices UPnP laxistes)
|
|
|
|
|
let xml = r#"
|
|
|
|
|
<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/">
|
|
|
|
|
<item id="1" parentID="0">
|
|
|
|
|
<title>Test Song</title>
|
|
|
|
|
<class>object.item.audioItem.musicTrack</class>
|
|
|
|
|
<res protocolInfo="http-get:*:audio/mpeg:*">http://example.com/song.mp3</res>
|
|
|
|
|
</item>
|
|
|
|
|
</DIDL-Lite>
|
|
|
|
|
"#;
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
let didl = DIDLLite::parse(xml).unwrap();
|
|
|
|
|
assert_eq!(didl.items.len(), 1);
|
|
|
|
|
assert_eq!(didl.items[0].title, "Test Song");
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_generic_parser() {
|
|
|
|
|
let xml = r#"
|
|
|
|
|
<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"
|
|
|
|
|
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
|
|
|
|
xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/">
|
|
|
|
|
</DIDL-Lite>
|
|
|
|
|
"#;
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
// Utiliser le parser générique
|
|
|
|
|
let metadata: DidlMetadata = parse_metadata(xml).unwrap();
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
assert_eq!(metadata.format, "DIDL-Lite");
|
|
|
|
|
assert!(metadata.parsed_at.is_some());
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_metadata_map() {
|
|
|
|
|
let xml = r#"
|
|
|
|
|
<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"
|
|
|
|
|
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
|
|
|
|
xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/">
|
|
|
|
|
</DIDL-Lite>
|
|
|
|
|
"#;
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
let metadata: DidlMetadata = parse_metadata(xml).unwrap();
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
// Transformer les données
|
|
|
|
|
let item_count = metadata.map(|didl| didl.items.len());
|
2025-10-19 13:42:29 +02:00
|
|
|
|
2025-10-01 20:49:50 +02:00
|
|
|
assert_eq!(item_count.format, "DIDL-Lite");
|
|
|
|
|
assert_eq!(item_count.data, 0);
|
|
|
|
|
}
|
2025-10-19 13:42:29 +02:00
|
|
|
}
|