1use std::{collections::BTreeMap, fmt::Debug, ops::AddAssign};
2
3use dependent_sort::{DependentSort, TopologicalError};
4
5use crate::{helpers::GroupedTask, CanonicalImport, Identifier, WasiInstance, WasiModule, WasiType, WasiTypeReference};
6
7mod arithmetic;
8
9#[derive(Default, Debug)]
10pub struct DependentGraph {
11 pub(crate) types: BTreeMap<Identifier, WasiType>,
12}
13
14impl DependentGraph {
15 pub fn get(&self, type_id: &WasiTypeReference) -> &WasiType {
16 match self.types.get(&type_id.symbol) {
17 Some(s) => s,
18 None => panic!("Missing Type `{}` in DependentGraph", type_id.symbol),
19 }
20 }
21
22 fn build_dag(&self) -> DependentSort<WasiType, WasiModule> {
23 let mut sorter = DependentSort::default();
24 for ty in self.types.values() {
25 match ty.dependent_task(self) {
26 Some(s) => {
27 sorter += s;
28 }
29 None => {}
30 }
31 }
32 sorter
33 }
34 pub fn resolve_imports(&self) -> Result<Vec<CanonicalImport>, TopologicalError<WasiType, WasiModule>> {
35 let mut imports = vec![];
36 for group in self.build_dag().sort_grouped_hash_specialization()? {
37 match group.id {
38 Some(s) => {
39 let mut instance = WasiInstance::new(s.clone());
40 for task in group.tasks {
41 instance.insert(task);
42 }
43 imports.push(CanonicalImport::Instance(instance));
44 }
45 None => {
46 for task in group.tasks {
47 imports.push(CanonicalImport::Type(task.clone()))
49 }
50 }
51 }
52 }
53 Ok(imports)
54 }
55}