melodium_share/
treatment_design.rs1use crate::{
2 ConnectionDesign, ModelInstanciationDesign, SharingResult, TreatmentInstanciationDesign,
3};
4use melodium_common::descriptor::Collection;
5use melodium_engine::{descriptor::Treatment as DesignedTreatment, design::Treatment};
6use serde::{Deserialize, Serialize};
7use std::{
8 collections::{BTreeMap, HashMap},
9 sync::Arc,
10};
11
12#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
13#[cfg_attr(feature = "webassembly", derive(tsify::Tsify))]
14#[cfg_attr(feature = "webassembly", tsify(into_wasm_abi, from_wasm_abi))]
15pub struct TreatmentDesign {
16 pub model_instanciations: BTreeMap<String, ModelInstanciationDesign>,
17 pub treatments: BTreeMap<String, TreatmentInstanciationDesign>,
18 pub connections: Vec<ConnectionDesign>,
19}
20
21impl TreatmentDesign {
22 pub(crate) fn make_design(
23 &self,
24 collection: &Collection,
25 descriptor: &Arc<DesignedTreatment>,
26 ) -> SharingResult<Treatment> {
27 let mut result = SharingResult::new_success(());
28
29 let mut model_instanciations = HashMap::with_capacity(self.model_instanciations.len());
30
31 for (name, model_instanciation) in &self.model_instanciations {
32 if let Some(model_instanciation) = result.merge_degrade_failure(
33 model_instanciation.make_design(collection, descriptor, name),
34 ) {
35 model_instanciations.insert(name.clone(), model_instanciation);
36 }
37 }
38
39 let mut treatments = HashMap::with_capacity(self.treatments.len());
40
41 for (name, treatment) in &self.treatments {
42 if let Some(treatment) =
43 result.merge_degrade_failure(treatment.make_design(collection, descriptor, name))
44 {
45 treatments.insert(name.clone(), treatment);
46 }
47 }
48
49 result.and_then(|_| {
50 SharingResult::new_success(Treatment {
51 descriptor: Arc::downgrade(descriptor),
52 model_instanciations,
53 treatments,
54 connections: self.connections.iter().map(|conn| conn.into()).collect(),
55 })
56 })
57 }
58}
59
60impl From<&Treatment> for TreatmentDesign {
61 fn from(value: &Treatment) -> Self {
62 Self {
63 model_instanciations: value
64 .model_instanciations
65 .iter()
66 .map(|(name, model)| (name.clone(), model.into()))
67 .collect(),
68 treatments: value
69 .treatments
70 .iter()
71 .map(|(name, treatment)| (name.clone(), treatment.into()))
72 .collect(),
73 connections: value.connections.iter().map(|conn| conn.into()).collect(),
74 }
75 }
76}