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
use ffi::EdgeKind;
use flow::definitions::{Graph, Node, NodeParams};
use flow::nodes;
use internal_prelude::works_everywhere::*;
use ::ffi;
use ::rustc_serialize::hex::FromHex;
use ::rustc_serialize::base64::FromBase64;
use ::{Context,Job};

pub struct IoTranslator<'a> {
    ctx: *mut ::ffi::ImageflowContext,
    c: &'a Context,
}
impl<'a> IoTranslator<'a> {
    pub fn new(c: &'a Context) -> IoTranslator<'a> {
        IoTranslator { ctx: c.flow_c(), c: c }
    }

    unsafe fn create_jobio_ptr_from_enum(&self,
                                         io_enum: s::IoEnum,
                                         dir: s::IoDirection)
                                         -> Result<*mut ::ffi::ImageflowJobIo> {
        match io_enum {
            s::IoEnum::ByteArray(vec) => {
                let bytes = vec;
                self.c.create_io_from_copy_of_slice(&bytes)
            }
            s::IoEnum::Base64(b64_string) => {
                let bytes = b64_string.as_str().from_base64().unwrap();
                self.c.create_io_from_copy_of_slice(&bytes)
            }
            s::IoEnum::BytesHex(hex_string) => {
                let bytes = hex_string.as_str().from_hex().unwrap();
                self.c.create_io_from_copy_of_slice(&bytes)
            }
            s::IoEnum::Filename(path) => {
                self.c.create_io_from_filename(&path, dir)
            }
            s::IoEnum::Url(url) => {
                let bytes = ::imageflow_helpers::fetching::fetch_bytes(&url).unwrap();
                self.c.create_io_from_copy_of_slice(&bytes)
            }
            s::IoEnum::OutputBuffer |
            s::IoEnum::OutputBase64 => {
                self.c.create_io_output_buffer()
            },
            s::IoEnum::Placeholder => {
                panic!("Placeholder was never substituted!")
            }
        }
    }

    pub unsafe fn add_to_job(&self, job: &mut Job, io_vec: Vec<s::IoObject>) {
        let mut io_list = Vec::new();
        for io_obj in io_vec {
            //TODO: add format!("Failed to create IO for {:?}", &io_obj)
            let io_ptr = self.create_jobio_ptr_from_enum(io_obj.io, io_obj.direction).unwrap();

            io_list.push((io_ptr, io_obj.io_id, io_obj.direction));
        }

        for io_list in io_list {
            job.add_io(io_list.0, io_list.1, io_list.2).unwrap();
        }

    }
}