sdf_metadata/metadata/package_interface/
package_import.rs1use crate::wit::{
2 dataflow::Header,
3 package_interface::{FunctionImport, PackageImport},
4};
5
6impl PackageImport {
7 pub fn builder(header: Header) -> PackageImportBuilder {
8 PackageImportBuilder::new(header)
9 }
10
11 pub fn merge(&mut self, other: &PackageImport) {
12 self.types.extend(other.types.iter().cloned());
13 self.types.sort();
14 self.types.dedup();
15
16 self.states.extend(other.states.iter().cloned());
17 self.states.sort();
18 self.states.dedup();
19
20 self.functions.extend(other.functions.iter().cloned());
21 self.functions.sort_by(|a, b| a.name.cmp(&b.name));
22 self.functions.dedup();
23 }
24}
25
26#[derive(Default)]
27pub struct PackageImportBuilder {
28 header: Header,
29 path: Option<String>,
30 types: Vec<String>,
31 states: Vec<String>,
32 functions: Vec<FunctionImport>,
33}
34
35impl PackageImportBuilder {
36 pub fn new(header: Header) -> Self {
37 Self {
38 header,
39 ..Default::default()
40 }
41 }
42
43 pub fn path(mut self, path: String) -> Self {
44 self.path = Some(path);
45 self
46 }
47
48 pub fn types(mut self, types: Vec<String>) -> Self {
49 self.types = types;
50 self
51 }
52
53 pub fn states(mut self, states: Vec<String>) -> Self {
54 self.states = states;
55 self
56 }
57
58 pub fn functions(mut self, functions: Vec<FunctionImport>) -> Self {
59 self.functions = functions;
60 self
61 }
62
63 pub fn build(self) -> PackageImport {
64 PackageImport {
65 metadata: self.header,
66 path: self.path,
67 types: self.types,
68 states: self.states,
69 functions: self.functions,
70 }
71 }
72}
73
74#[cfg(test)]
75mod test {
76 use crate::wit::{
77 metadata::Header,
78 package_interface::{FunctionImport, PackageImport},
79 };
80
81 #[test]
82 fn test_merge_adds_assets() {
83 let mut existing_import = PackageImport {
84 metadata: Header {
85 name: "cats_package".to_string(),
86 version: "0.1.0".to_string(),
87 namespace: "inf-namespace".to_string(),
88 },
89 path: None,
90 types: vec!["cat".to_string()],
91 states: vec!["my-state".to_string()],
92 functions: vec![FunctionImport {
93 name: "cat_map_cat".to_string(),
94 alias: None,
95 }],
96 };
97
98 let new_import = PackageImport {
99 metadata: Header {
100 name: "cats_package".to_string(),
101 version: "0.1.0".to_string(),
102 namespace: "inf-namespace".to_string(),
103 },
104 path: None,
105 types: vec!["dog".to_string()],
106 states: vec!["my_other_state".to_string()],
107 functions: vec![FunctionImport {
108 name: "cat_map_dog".to_string(),
109 alias: None,
110 }],
111 };
112
113 existing_import.merge(&new_import);
114
115 assert_eq!(
116 existing_import.types,
117 vec!["cat".to_string(), "dog".to_string()]
118 );
119 assert_eq!(
120 existing_import.states,
121 vec!["my-state".to_string(), "my_other_state".to_string()]
122 );
123 assert_eq!(
124 existing_import.functions,
125 vec![
126 FunctionImport {
127 name: "cat_map_cat".to_string(),
128 alias: None,
129 },
130 FunctionImport {
131 name: "cat_map_dog".to_string(),
132 alias: None,
133 },
134 ]
135 );
136 }
137
138 #[test]
139 fn test_merge_adds_assets_without_duplication() {
140 let mut existing_import = PackageImport {
141 metadata: Header {
142 name: "cats_package".to_string(),
143 version: "0.1.0".to_string(),
144 namespace: "inf-namespace".to_string(),
145 },
146 path: None,
147 types: vec!["cat".to_string()],
148 states: vec!["my-state".to_string()],
149 functions: vec![FunctionImport {
150 name: "cat_map_cat".to_string(),
151 alias: None,
152 }],
153 };
154
155 let new_import = PackageImport {
156 metadata: Header {
157 name: "cats_package".to_string(),
158 version: "0.1.0".to_string(),
159 namespace: "inf-namespace".to_string(),
160 },
161 path: None,
162 types: vec!["cat".to_string(), "dog".to_string()],
163 states: vec!["my-state".to_string(), "my_other_state".to_string()],
164 functions: vec![
165 FunctionImport {
166 name: "cat_map_cat".to_string(),
167 alias: None,
168 },
169 FunctionImport {
170 name: "cat_map_dog".to_string(),
171 alias: None,
172 },
173 ],
174 };
175
176 existing_import.merge(&new_import);
177
178 assert_eq!(
179 existing_import.types,
180 vec!["cat".to_string(), "dog".to_string()]
181 );
182 assert_eq!(
183 existing_import.states,
184 vec!["my-state".to_string(), "my_other_state".to_string()]
185 );
186 assert_eq!(
187 existing_import.functions,
188 vec![
189 FunctionImport {
190 name: "cat_map_cat".to_string(),
191 alias: None,
192 },
193 FunctionImport {
194 name: "cat_map_dog".to_string(),
195 alias: None,
196 },
197 ]
198 );
199 }
200}