melodium_share/
attribute.rs

1use melodium_common::descriptor::Attributes as CommonAttributes;
2use serde::{Deserialize, Serialize};
3use std::collections::BTreeMap;
4
5#[cfg_attr(feature = "webassembly", tsify::declare)]
6pub type Attribute = String;
7
8#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
9#[cfg_attr(feature = "webassembly", derive(tsify::Tsify))]
10#[cfg_attr(feature = "webassembly", tsify(into_wasm_abi, from_wasm_abi))]
11pub struct Attributes(pub BTreeMap<String, Attribute>);
12
13impl Attributes {
14    pub fn new() -> Self {
15        Self(BTreeMap::new())
16    }
17}
18
19impl From<&CommonAttributes> for Attributes {
20    fn from(value: &CommonAttributes) -> Self {
21        Self(value.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
22    }
23}
24
25impl From<CommonAttributes> for Attributes {
26    fn from(value: CommonAttributes) -> Self {
27        Self(value.into_iter().collect())
28    }
29}
30
31impl Into<CommonAttributes> for Attributes {
32    fn into(self) -> CommonAttributes {
33        self.0.into_iter().collect()
34    }
35}
36
37impl Into<CommonAttributes> for &Attributes {
38    fn into(self) -> CommonAttributes {
39        self.0.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
40    }
41}