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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

use crate::{
    path,
    constants,
    error,
    ok,
    vprintln,
    httpfetch
};

use std::str::FromStr;
use std::collections::HashMap;
use std::path::Path;
use std::fs::File;
use std::io::Write;

use serde::{
    Serialize
};

pub fn string_is_valid_num<T:FromStr>(s:&str) -> bool {
    let num = s.parse::<T>();
    num.is_ok()
}

pub fn string_is_valid_f64(s:&str) -> bool {
    string_is_valid_num::<f64>(s)
}


pub fn string_is_valid_f32(s:&str) -> bool {
    string_is_valid_num::<f32>(s)
}

pub fn string_is_valid_i32(s:&str) -> bool {
    string_is_valid_num::<i32>(s)
}

pub fn filename_char_at_pos(filename:&str, pos:usize) -> char {
    let bn = path::basename(&filename);
    bn.chars().nth(pos).unwrap()
}

#[macro_export]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        let y = max!($($z),*);
        if $x > y {
            $x
        } else {
            y
        }
    }}
}

#[macro_export]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        let y = min!($($z),*);
        if $x < y {
            $x
        } else {
            y
        }
    }}
}





////////////////////////////////////
/// Functions for supporting instrument lists
////////////////////////////////////


pub struct InstrumentMap {
    pub map: HashMap<&'static str, Vec<&'static str>>
}

impl InstrumentMap {
    pub fn is_name_a_remote_instrument(&self, instrument:&str) -> bool {
        for rem_inst_list in self.map.values() {
            for s in rem_inst_list.iter() {
                if &instrument == s {
                    return true;
                }
            }
        }
        false
    }

    pub fn find_remote_instrument_names(&self, instrument:&str) -> error::Result<Vec<String>> {
        let mut inst_list : Vec<String> = Vec::new();
        if self.is_name_a_remote_instrument(instrument) {
            inst_list.push(String::from(instrument));
            return Ok(inst_list);
        }
    
        if self.map.contains_key(instrument) {
            let sublist = self.map.get(instrument).unwrap();
            inst_list.extend(sublist.iter().map(|&i| String::from(i)));
            
        }
    
        if !inst_list.is_empty() {
            Ok(inst_list)
        } else {
            Err(constants::status::UNSUPPORTED_INSTRUMENT)
        }
    }

    pub fn find_remote_instrument_names_fromlist(&self, instrument_inputs:&[&str]) -> error::Result<Vec<String>> {
        let mut inst_list : Vec<String> = Vec::new();
    
        for c in instrument_inputs.iter() {
            let found_list_res = self.find_remote_instrument_names(c);
            let res = match found_list_res {
                Err(_e) => return Err(constants::status::UNSUPPORTED_INSTRUMENT),
                Ok(v) => v,
            };
            inst_list.extend(res);
        }
    
        if !inst_list.is_empty() {
            Ok(inst_list)
        } else {
            Err(constants::status::UNSUPPORTED_INSTRUMENT)
        }
    }

    pub fn print_instruments(&self) {
        for (key, rem_inst_list) in &self.map {
            println!("{}", key);
            for s in rem_inst_list.iter() {
                println!("  {}", s);
            }
        }
    }
}











pub fn stringvec(a:&str, b:&str) -> Vec<String> {
    vec![a.to_owned(), b.to_owned()]
}

pub fn stringvec_b(a:&str, b:String) -> Vec<String> {
    vec![a.to_owned(), b]
}


pub fn image_exists_on_filesystem(image_url:&str) -> bool {
    //let image_url = &image["image_files"]["full_res"].as_str().unwrap();
    let bn = path::basename(image_url);
    path::file_exists(bn.as_str())
}

pub fn fetch_image(image_url:&str, only_new:bool) -> error::Result<&'static str> {
    //let image_url = &image["url"].as_str().unwrap();
    let bn = path::basename(image_url);

    if image_exists_on_filesystem(&image_url) && only_new {
        vprintln!("Output file {} exists, skipping", bn);
        return ok!();
    }

    let image_data = match httpfetch::simple_fetch_bin(image_url) {
        Ok(i) => i,
        Err(e) => return Err(e)
    };
    
    let path = Path::new(bn.as_str());

    let mut file = match File::create(&path) {
        Err(why) => panic!("couldn't create {}", why),
        Ok(file) => file,
    };

    match file.write_all(&image_data[..]) {
        Ok(_) => ok!(),
        Err(_e) => Err("Error writing image to filesystem")
    }
}

pub fn save_image_json<T:Serialize>(image_url:&str, item:&T, only_new:bool) -> error::Result<&'static str> {
    let item_str = serde_json::to_string_pretty(item).unwrap();
    save_image_json_from_string(&image_url, &item_str, only_new)
}

pub fn save_image_json_from_string(image_path:&str, item:&String, only_new:bool) -> error::Result<&'static str> {
    let out_file = image_path.replace(".jpg", "-metadata.json").replace(".JPG", "-metadata.json")
                             .replace(".png", "-metadata.json").replace(".PNG", "-metadata.json");

    if path::file_exists(out_file.as_str()) && only_new {
        vprintln!("Output file {} exists, skipping", image_path);
        return ok!();
    }

    let path = Path::new(out_file.as_str());

    vprintln!("Writing metadata file to {}", path.to_str().unwrap());

    let mut file = match File::create(&path) {
        Err(why) => panic!("couldn't create {}", why),
        Ok(file) => file,
    };

    match file.write_all(item.as_bytes()) {
        Ok(_) => ok!(),
        Err(_e) => Err("Error writing metadata to filesystem")
    }
}


pub fn append_file_name(input_file:&str, append:&str) -> String {
    let append_with_ext = format!("-{}.png", append);
    replace_image_extension(input_file, append_with_ext.as_str())
    // let append_with_ext = format!("-{}.png", append);
    // let out_file = input_file.replace(".png", append_with_ext.as_str())
    //                          .replace(".PNG", append_with_ext.as_str())
    //                          .replace(".jpg", append_with_ext.as_str())
    //                          .replace(".JPG", append_with_ext.as_str())
    //                          .replace(".tif", append_with_ext.as_str())
    //                          .replace(".TIF", append_with_ext.as_str());
    // String::from(out_file)
}

pub fn replace_image_extension(input_file:&str, append:&str) -> String {
    let out_file = input_file.replace(".png", append)
                             .replace(".PNG", append)
                             .replace(".jpg", append)
                             .replace(".JPG", append)
                             .replace(".tif", append)
                             .replace(".TIF", append);
    String::from(out_file)
}