Skip to main content

rumtk_web/js/
mod.rs

1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2026  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 * Copyright (C) 2026  MedicalMasses L.L.C. <contact@medicalmasses.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20use 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}