Files
pmomusic/pmoupnp/src/actions/action_instance.rs

135 lines
3.7 KiB
Rust
Raw Normal View History

2025-10-02 18:39:32 +02:00
use std::sync::Arc;
use xmltree::{Element, XMLNode};
use crate::actions::Action;
2025-10-05 14:35:44 +02:00
use crate::actions::ArgInstanceSet;
use crate::actions::ActionInstance;
2025-10-02 18:39:32 +02:00
use crate::UpnpInstance;
use crate::UpnpObject;
use crate::UpnpTyped;
use crate::UpnpTypedInstance;
2025-10-03 21:19:14 +02:00
use crate::UpnpObjectType;
2025-10-02 18:39:32 +02:00
impl UpnpObject for ActionInstance {
2025-10-05 14:35:44 +02:00
fn to_xml_element(&self) -> Element {
let mut elem = Element::new("action");
// <name>
let mut name_elem = Element::new("name");
name_elem.children.push(XMLNode::Text(self.get_name().clone()));
elem.children.push(XMLNode::Element(name_elem));
2025-10-05 14:35:44 +02:00
// Utiliser le set d'instances d'arguments
let args_container = self.arguments.to_xml_element();
elem.children.push(XMLNode::Element(args_container));
elem
}
}
2025-10-02 18:39:32 +02:00
impl UpnpTyped for ActionInstance {
fn as_upnp_object_type(&self) -> &UpnpObjectType {
return &self.object;
}
}
2025-10-02 18:39:32 +02:00
impl UpnpInstance for ActionInstance {
type Model = Action;
2025-10-02 18:39:32 +02:00
fn new(action: &Action) -> Self {
2025-10-05 14:35:44 +02:00
// Créer les instances d'arguments
let mut arguments = ArgInstanceSet::new();
for arg_model in action.arguments().all() {
let arg_instance = Arc::new(crate::actions::ArgumentInstance::new(&*arg_model));
if let Err(e) = arguments.insert(arg_instance) {
tracing::error!("Failed to insert argument instance: {:?}", e);
}
}
Self {
object: UpnpObjectType {
name: action.get_name().clone(),
object_type: "ActionInstance".to_string(),
},
model: action.clone(),
2025-10-05 14:35:44 +02:00
arguments, // ⬅️ Set d'instances, pas le modèle !
}
}
2025-10-02 18:39:32 +02:00
}
impl UpnpTypedInstance for ActionInstance {
fn get_model(&self) -> &Self::Model {
&self.model
}
}
impl ActionInstance {
2025-10-05 14:35:44 +02:00
/// Retourne une instance d'argument par son nom.
///
/// # Arguments
///
/// * `name` - Nom de l'argument à rechercher
///
/// # Returns
///
/// `Some(Arc<ArgumentInstance>)` si trouvé, `None` sinon.
pub fn argument(&self, name: &str) -> Option<Arc<crate::actions::ArgumentInstance>> {
self.arguments.get_by_name(name)
}
2025-10-02 18:39:32 +02:00
2025-10-05 14:35:44 +02:00
/// Retourne le set d'instances d'arguments.
///
/// # Returns
///
/// Référence vers le `ArgInstanceSet` contenant toutes les instances.
///
/// # Examples
///
/// ```ignore
/// for arg_instance in action_instance.arguments_set().all() {
/// println!("Argument: {}", arg_instance.get_name());
/// if let Some(var) = arg_instance.get_variable_instance() {
/// println!(" Variable: {} = {}", var.get_name(), var.value());
/// }
/// }
/// ```
pub fn arguments_set(&self) -> &ArgInstanceSet {
&self.arguments // ⬅️ Retourne les INSTANCES, pas les modèles !
}
2025-10-05 14:35:44 +02:00
}
2025-10-05 14:35:44 +02:00
#[cfg(test)]
mod tests {
use super::*;
use crate::actions::Action;
use crate::UpnpInstance;
#[test]
fn test_action_instance_creation() {
let action = Action::new("Play".to_string());
let instance = ActionInstance::new(&action);
assert_eq!(instance.get_name(), "Play");
}
#[test]
fn test_action_instance_has_argument_instances() {
let action = Action::new("Play".to_string());
let instance = ActionInstance::new(&action);
// Vérifier que arguments_set() retourne bien des instances
assert!(instance.arguments_set().all().iter().all(|arg| {
// Chaque argument doit être une ArgumentInstance
arg.get_model(); // Cette méthode existe seulement sur les instances
true
}));
}
}
2025-10-05 14:35:44 +02:00