1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::Arc;
use log::{debug, info, trace};
use url::Url;
use flow_impl::Implementation;
use flowrstructs::lib_manifest::{ImplementationLocator::Native, ImplementationLocator::Wasm, LibraryManifest};
use flowrstructs::manifest::Manifest;
use provider::content::provider::Provider;
use crate::errors::*;
use crate::wasm;
#[derive(Default)]
pub struct Loader {
loaded_lib_references: HashSet<String>,
global_lib_implementations: HashMap<String, Arc<dyn Implementation>>,
}
impl Loader {
pub fn new() -> Self {
Loader {
loaded_lib_references: HashSet::<String>::new(),
global_lib_implementations: HashMap::<String, Arc<dyn Implementation>>::new(),
}
}
pub fn get_lib_implementations(&self) -> &HashMap<String, Arc<dyn Implementation>> {
&self.global_lib_implementations
}
pub fn load_manifest(&mut self, provider: &dyn Provider, flow_manifest_url: &str) -> Result<Manifest> {
debug!("Loading flow manifest from '{}'", flow_manifest_url);
let (mut flow_manifest, resolved_url) = Manifest::load(provider, flow_manifest_url)
.chain_err(|| format!("Could not load manifest from: '{}'", flow_manifest_url))?;
self.load_libraries(provider, &flow_manifest)?;
self.resolve_implementations(&mut flow_manifest, &resolved_url, provider)?;
Ok(flow_manifest)
}
fn load_libraries(&mut self, provider: &dyn Provider, manifest: &Manifest) -> Result<()> {
for library_reference in manifest.get_lib_references() {
if !self.loaded_lib_references.contains(library_reference) {
info!("Attempting to load library reference '{}'", library_reference);
let (lib_manifest, lib_manifest_url) = LibraryManifest::load(provider, library_reference)
.chain_err(|| format!("Could not load library with reference: '{}'", library_reference))?;
debug!("Loading library '{}' from '{}'", library_reference, lib_manifest_url);
self.add_lib(provider, library_reference, lib_manifest, &lib_manifest_url)?;
info!("Library loaded");
}
}
Ok(())
}
pub fn resolve_implementations(&mut self, flow_manifest: &mut Manifest, manifest_url: &str, provider: &dyn Provider) -> Result<String> {
debug!("Resolving implementations");
for function in flow_manifest.get_functions() {
let implementation_source_url = function.implementation_location().to_string();
let parts: Vec<_> = implementation_source_url.split(':').collect();
match parts[0] {
"lib" => {
let implementation = self.global_lib_implementations.get(function.implementation_location())
.chain_err(|| format!("Did not find implementation for '{}'", implementation_source_url))?;
trace!("Found implementation for '{}'", function.implementation_location());
function.set_implementation(implementation.clone());
}
_ => {
let full_url = Url::parse(manifest_url)
.map_err(|e| e.to_string())?
.join(function.implementation_location())
.map_err(|_| format!("Could not join '{}' to Url", function.implementation_location()))?;
let wasm_executor = wasm::load(provider,
full_url.as_str())?;
function.set_implementation(Arc::new(wasm_executor) as Arc<dyn Implementation>);
}
}
}
Ok("All implementations found".into())
}
pub fn add_lib(&mut self, provider: &dyn Provider,
lib_reference: &str,
lib_manifest: LibraryManifest,
lib_manifest_url: &str) -> Result<()> {
debug!("Loading library '{}' from '{}'", lib_manifest.metadata.name, lib_manifest_url);
self.loaded_lib_references.insert(lib_reference.to_string());
for (reference, locator) in lib_manifest.locators {
if self.global_lib_implementations.get(&reference).is_none() {
let implementation = match locator {
Wasm(wasm_source_relative) => {
let wasm_url = Url::parse(lib_manifest_url)
.map_err(|e| e.to_string())?
.join(&wasm_source_relative)
.map_err(|e| e.to_string())?;
debug!("Looking for wasm source: '{}'", wasm_url);
let wasm_executor = wasm::load(provider, wasm_url.as_str())?;
Arc::new(wasm_executor) as Arc<dyn Implementation>
}
Native(implementation) => implementation
};
self.global_lib_implementations.insert(reference, implementation);
}
}
info!("Library '{}' loaded.", lib_manifest.metadata.name);
Ok(())
}
}
#[cfg(test)]
mod test {
use super::Loader;
#[test]
fn test_create() {
let _ = Loader::new();
}
}