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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::collections::HashMap;

use log::{debug, info, trace};
use url::Url;

use flowrstructs::input::InputInitializer;
use flowrstructs::manifest::{Cargo, MetaData};
use provider::content::provider::Provider;

use crate::deserializers::deserializer_helper::get_deserializer;
use crate::errors::*;
use crate::model::flow::Flow;
use crate::model::function::Function;
use crate::model::io::IO;
use crate::model::name::HasName;
use crate::model::name::Name;
use crate::model::process::Process;
use crate::model::process::Process::FlowProcess;
use crate::model::process::Process::FunctionProcess;
use crate::model::route::Route;
use crate::model::route::SetRoute;

/// All deserializers have to implement this trait for content deserialization, plus a method
/// to return their name to be able to inform the user of which deserializer was used
pub trait Deserializer {
    fn deserialize(&self, contents: &str, url: Option<&str>) -> Result<Process>;
    fn name(&self) -> &'static str;
}

/// Many structs in the model implement the `Validate` method which is used to check the
/// description deserialized from file obeys some additional constraints that cannot be expressed
/// in the struct definition in `serde`
pub trait Validate {
    fn validate(&self) -> Result<()>;
}

/// Load a context process definition from `url`, recursively loading all sub-processes referenced.
///
/// The return value is a `Result` containing the `Process`, or a `String` describing the error
/// found while loading.
///
/// # Example
/// ```
/// use provider::content::provider::Provider;
/// use provider::errors::Result;
/// use std::env;
/// use url::Url;
///
/// // Clients need to provide a Provider of content for the loader as flowlibc is independent of
/// // file systems and io.
/// struct DummyProvider;
///
/// // A Provider must implement the `Provider` trait, with the methods to `resolve` a URL and to
/// // `get` the contents for parsing.
/// impl Provider for DummyProvider {
///     fn resolve_url(&self, url: &str, default_filename: &str, _ext: &[&str]) -> Result<(String, Option<String>)> {
///        // Just fake the url resolution in this example
///        Ok((url.to_string(), None))
///     }
///
///    fn get_contents(&self, url: &str) -> Result<Vec<u8>> {
///        // Return the simplest flow definition possible - ignoring the url passed in
///        Ok("flow = \"test\"".as_bytes().to_owned())
///     }
/// }
///
/// // Create an instance of the `DummyProvider`
/// let dummy_provider = DummyProvider{};
///
/// // load the flow from `url = file:///example.toml` using the `dummy_provider`
/// flowclib::compiler::loader::load("file:///example.toml", &dummy_provider).unwrap();
/// ```
pub fn load(url: &str, provider: &dyn Provider) -> Result<Process> {
    trace!("load()");
    load_process(&Route::default(), &Name::default(),
                 0, &mut 0, url, provider, &&None)
}

#[allow(clippy::too_many_arguments)]
fn load_process(parent_route: &Route, alias: &Name, parent_flow_id: usize, flow_count: &mut usize, url: &str, provider: &dyn Provider,
                initializations: &Option<HashMap<String, InputInitializer>>) -> Result<Process> {
    trace!("load_process()");
    trace!("  --> resolve_url()");
    let (resolved_url, lib_ref) = provider.resolve_url(url, "context", &["toml"])
        .chain_err(|| format!("Could not resolve the url: '{}'", url))?;
    debug!("Source URL '{}' resolved to: '{}'", url, resolved_url);
    trace!("  --> get_contents()");
    let contents = provider.get_contents(&resolved_url)
        .chain_err(|| format!("Could not get contents of resolved url: '{}'", resolved_url))?;

    trace!("  --> get_deserializer()");
    let deserializer = get_deserializer(&resolved_url)?;
    if !alias.is_empty() {
        info!("Loading process with alias = '{}'", alias);
    }

    debug!("Loading process from url = '{}' with deserializer: '{}'", resolved_url, deserializer.name());
    trace!("  --> deserialize()");
    let mut process = deserializer.deserialize(&String::from_utf8(contents)
        .chain_err(|| "Could not read UTF8 contents")?,
                                               Some(url))
        .chain_err(|| format!("Could not deserialize process from content in '{}'", url))?;

    debug!("Deserialized the flow, now parsing and loading any sub-processes");
    match process {
        FlowProcess(ref mut flow) => {
            config_flow(flow, &resolved_url, parent_route, alias, *flow_count, initializations)?;
            *flow_count += 1;
            load_process_refs(flow, flow_count, provider)?;
            flow.build_connections()?;
        }
        FunctionProcess(ref mut function) => {
            config_function(function, &resolved_url, parent_route, alias, parent_flow_id,
                            lib_ref, initializations)?;
        }
    }

    Ok(process)
}

/// load library metadata from the given url using the provider.
/// Currently it used the `package` table of Cargo.toml as a source but it could
/// easily use another file as along as it has the required fields to satisfy `MetaData` struct
pub fn load_metadata(url: &str, provider: &dyn Provider) -> Result<MetaData> {
    trace!("Loading Metadata");
    let (resolved_url, _) = provider.resolve_url(url, "Cargo", &["toml"])
        .chain_err(|| format!("Could not resolve the url: '{}'", url))?;

    debug!("Source URL '{}' resolved to: '{}'", url, resolved_url);
    let contents = provider.get_contents(&resolved_url)
        .chain_err(|| format!("Could not get contents of resolved url: '{}'", resolved_url))?;

    let cargo: Cargo = toml::from_str(&String::from_utf8(contents)
        .chain_err(|| "Could not read UTF8 contents")?)
        .chain_err(|| format!("Error deserializing Toml from: '{:?}'", resolved_url))?;

    Ok(cargo.package)
}

/*
    Configure a flow with additional information after it is deserialized from file
*/
fn config_flow(flow: &mut Flow, source_url: &str, parent_route: &Route, alias_from_reference: &Name, id: usize,
               initializations: &Option<HashMap<String, InputInitializer>>) -> Result<()> {
    flow.id = id;
    flow.set_alias(alias_from_reference);
    flow.source_url = source_url.to_string();
    IO::set_initial_values(flow.inputs_mut(), initializations);
    flow.set_routes_from_parent(parent_route);
    flow.validate()
}

/*
    Load sub-processes from the process_refs in a flow
*/
fn load_process_refs(flow: &mut Flow, flow_count: &mut usize, provider: &dyn Provider) -> Result<()> {
    if let Some(ref mut process_refs) = flow.process_refs {
        for process_ref in process_refs {
            let subprocess_url = Url::parse(&flow.source_url)
                .map_err(|e| e.to_string())?
                .join(&process_ref.source)
                .map_err(|e| e.to_string())?;
            let process = load_process(&flow.route, &process_ref.alias(),
                                       flow.id, flow_count, subprocess_url.as_str(),
                                       provider, &process_ref.initializations)?;
            process_ref.set_alias(process.name());

            // runtime needs references to library functions to be able to load the implementations at load time
            // library flow definitions are "compiled down" to just library function references at compile time.
            if let FunctionProcess(function) = &process {
                if let Some(lib_ref) = function.get_lib_reference() {
                    flow.lib_references.push(format!("{}/{}", lib_ref, function.name()));
                }
            }

            flow.subprocesses.insert(process_ref.alias().to_owned(), process);
        }
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn config_function(function: &mut Function, source_url: &str, parent_route: &Route, alias: &Name,
                   flow_id: usize,
                   lib_ref: Option<String>,
                   initializations: &Option<HashMap<String, InputInitializer>>)
                   -> Result<()> {
    function.set_flow_id(flow_id);
    function.set_alias(alias);
    function.set_source_url(source_url);
    function.set_lib_reference(lib_ref);
    function.set_routes_from_parent(parent_route);
    IO::set_initial_values(&mut function.inputs, initializations);
    function.validate()
}

#[cfg(test)]
mod test {
    use flowrstructs::manifest::{Cargo, MetaData};

    #[test]
    fn deserialize_library() {
        let contents = include_str!("../../../tests/test_libs/Cargo.toml");
        let cargo: Cargo = toml::from_str(contents)
            .expect("Could not parse Cargo.toml in deserialize_library test");
        let _: MetaData = cargo.package;
    }
}