Skip to main content

melodium_share/
output.rs

1use crate::SharingResult;
2
3use super::{Attributes, DescribedType, Flow};
4use melodium_common::descriptor::{
5    Attribuable, Collection, Identifier as CommonIdentifier, Output as CommonOutput,
6};
7use serde::{Deserialize, Serialize};
8
9#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
10#[cfg_attr(feature = "webassembly", derive(tsify::Tsify))]
11#[cfg_attr(feature = "webassembly", tsify(into_wasm_abi, from_wasm_abi))]
12pub struct Output {
13    pub name: String,
14    pub described_type: DescribedType,
15    pub flow: Flow,
16    pub attributes: Attributes,
17}
18
19impl Output {
20    pub fn to_output(
21        &self,
22        collection: &Collection,
23        scope: &CommonIdentifier,
24    ) -> SharingResult<CommonOutput> {
25        self.described_type
26            .to_described_type(collection, scope)
27            .and_then(|described_type| {
28                SharingResult::new_success(CommonOutput::new(
29                    &self.name,
30                    described_type,
31                    (&self.flow).into(),
32                    (&self.attributes).into(),
33                ))
34            })
35    }
36}
37
38impl From<&CommonOutput> for Output {
39    fn from(value: &CommonOutput) -> Self {
40        Self {
41            name: value.name().to_string(),
42            described_type: value.described_type().into(),
43            flow: Flow::from(value.flow()),
44            attributes: value.attributes().into(),
45        }
46    }
47}
48
49impl TryInto<CommonOutput> for Output {
50    type Error = ();
51    fn try_into(self) -> Result<CommonOutput, ()> {
52        (&self).try_into()
53    }
54}
55
56impl TryInto<CommonOutput> for &Output {
57    type Error = ();
58    fn try_into(self) -> Result<CommonOutput, ()> {
59        Ok(CommonOutput::new(
60            &self.name,
61            (&self.described_type).try_into()?,
62            (&self.flow).into(),
63            (&self.attributes).into(),
64        ))
65    }
66}