wasmflow_interface/
maps.rs

1use std::collections::HashMap;
2use std::str::FromStr;
3
4use serde::{Deserialize, Serialize};
5
6use crate::signatures::{CollectionSignature, ComponentSignature, ParseError, TypeSignature};
7use crate::TypeDefinition;
8
9#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
10#[serde(transparent)]
11#[must_use]
12/// A HashMap of type names to their signature.
13pub struct FieldMap(HashMap<String, TypeSignature>);
14
15impl FieldMap {
16  /// Constructor for [TypeMap]
17  pub fn new() -> Self {
18    Self(HashMap::new())
19  }
20}
21
22impl FieldMap {
23  wasmflow_macros::kv_impl! {TypeSignature, pub}
24}
25
26impl From<HashMap<String, TypeSignature>> for FieldMap {
27  fn from(map: HashMap<String, TypeSignature>) -> Self {
28    Self(map)
29  }
30}
31
32impl TryFrom<Vec<(&str, &str)>> for FieldMap {
33  type Error = ParseError;
34
35  fn try_from(list: Vec<(&str, &str)>) -> Result<Self, ParseError> {
36    let mut map = FieldMap::new();
37    for (k, v) in list {
38      map.insert(k, TypeSignature::from_str(v)?);
39    }
40    Ok(map)
41  }
42}
43
44impl FromIterator<(String, TypeSignature)> for FieldMap {
45  fn from_iter<T: IntoIterator<Item = (String, TypeSignature)>>(iter: T) -> Self {
46    let mut map: HashMap<String, TypeSignature> = HashMap::new();
47    for (k, v) in iter {
48      map.insert(k, v);
49    }
50    Self(map)
51  }
52}
53
54#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
55#[serde(transparent)]
56#[must_use]
57/// A HashMap of struct names to their signature.
58pub struct TypeMap(pub HashMap<String, TypeDefinition>);
59
60impl From<HashMap<String, TypeDefinition>> for TypeMap {
61  fn from(map: HashMap<String, TypeDefinition>) -> Self {
62    Self(map)
63  }
64}
65
66impl TypeMap {
67  /// Constructor for [TypeMap]
68  pub fn new() -> Self {
69    Self(HashMap::new())
70  }
71  wasmflow_macros::kv_impl! {TypeDefinition, pub}
72}
73
74#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
75#[serde(transparent)]
76#[must_use]
77/// A HashMap from collection names to their signatures.
78pub struct CollectionMap(pub HashMap<String, CollectionSignature>);
79
80impl From<HashMap<String, CollectionSignature>> for CollectionMap {
81  fn from(map: HashMap<String, CollectionSignature>) -> Self {
82    Self(map)
83  }
84}
85
86impl CollectionMap {
87  wasmflow_macros::kv_impl! {CollectionSignature, pub}
88}
89
90#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
91#[serde(transparent)]
92#[must_use]
93/// A HashMap of component names to their signature.
94pub struct ComponentMap(pub HashMap<String, ComponentSignature>);
95
96impl ComponentMap {
97  wasmflow_macros::kv_impl! {ComponentSignature, pub}
98}
99
100impl From<HashMap<String, ComponentSignature>> for ComponentMap {
101  fn from(map: HashMap<String, ComponentSignature>) -> Self {
102    Self(map)
103  }
104}
105
106impl From<HashMap<&str, ComponentSignature>> for ComponentMap {
107  fn from(map: HashMap<&str, ComponentSignature>) -> Self {
108    Self(map.into_iter().map(|(k, v)| (k.to_owned(), v)).collect())
109  }
110}