Skip to main content

melodium_share/
input.rs

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