1use rumtk_core::base::RUMResult;
21use rumtk_core::strings::{rumtk_format, RUMString};
22use std::{fs, path};
23
24mod default_library;
25
26pub const DEFAULT_OUT_JS_DIR: &str = "./static/js";
27
28fn select_from_library(item_name: &str) -> &'static str {
29 match item_name {
30 "file_cache" => default_library::JS_FILE_CACHE,
31 "goto" => default_library::JS_GOTO,
32 _ => "",
33 }
34}
35
36pub fn rumtk_web_js_get_item(item_name: &str) -> RUMResult<RUMString> {
37 match fs::create_dir_all(DEFAULT_OUT_JS_DIR) {
38 Ok(_) => (),
39 Err(e) => return Err(rumtk_format!("Failed to create JS directory: {} => because {}", DEFAULT_OUT_JS_DIR, e)),
40 };
41 let path = path::Path::new(DEFAULT_OUT_JS_DIR)
42 .join(item_name)
43 .with_extension("js");
44 let out_path = match path
45 .to_str()
46 {
47 Some(path) => path.to_string(),
48 None => return Err(rumtk_format!("Could not create path to JS file {}!", item_name)),
49 };
50 match fs::exists(&out_path) {
51 Ok(result) => match result {
52 true => Ok(out_path),
53 false => match fs::write(&out_path, select_from_library(item_name)) {
54 Ok(_) => Ok(out_path),
55 Err(e) => Err(rumtk_format!("Failed to write JS file: {} => because {}", out_path, e)),
56 },
57 },
58 Err(e) => Err(rumtk_format!("Failed to generate JS file: {} => because {}", out_path, e)),
59 }
60}