Skip to main content

rumtk_web/css/
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) 2025  Luis M. Santos, M.D.
5 * Copyright (C) 2025  Nick Stephenson
6 * Copyright (C) 2025  Ethan Dixon
7 * Copyright (C) 2025  MedicalMasses L.L.C.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 */
23use crate::utils::packaging::{minify_asset, Asset};
24use rumtk_core::hash::has_same_hash;
25use rumtk_core::strings::{RUMString, RUMStringConversions};
26use std::{fs, path};
27
28mod animations;
29mod basic;
30mod components;
31mod default;
32mod fonts;
33mod forms;
34mod gap;
35mod index;
36mod layout;
37mod theme;
38
39pub const DEFAULT_OUT_CSS_DIR: &str = "./static/css";
40pub const DEFAULT_OUT_CSS: &str = "bundle.min.css";
41
42pub fn bundle_css(sources: &Vec<String>, out_dir: &str, out_file: &str) {
43    let mut css: RUMString = RUMString::default();
44
45    css += theme::THEME;
46    css += index::BODY;
47    css += basic::BASIC_CSS;
48    css += default::DEFAULT_CSS;
49    css += fonts::FONTS_CSS;
50    css += gap::GAP_CSS;
51    css += animations::ANIMATIONS_CSS;
52    css += forms::FORM_CSS;
53    css += components::LIST_CSS;
54    css += layout::LAYOUT_CSS;
55
56    for source in sources {
57        let css_data = fs::read_to_string(source).unwrap_or_default();
58        css += &css_data;
59    }
60
61    fs::create_dir_all(out_dir).unwrap_or_default();
62
63    let out_path = path::Path::new(out_dir)
64        .join(out_file)
65        .with_extension("css")
66        .to_str()
67        .expect("Could not create path to CSS file!")
68        .to_string();
69
70    let minified = minify_asset(Asset::CSS(&css))
71        .expect("Failed to minify the CSS contents!")
72        .to_rumstring();
73
74    let file_exists = fs::exists(&out_path).unwrap_or_default();
75    let skip_write_css = file_exists
76        && has_same_hash(
77            &minified,
78            &fs::read_to_string(&out_path)
79                .unwrap_or_default()
80                .to_rumstring(),
81        );
82
83    if !skip_write_css {
84        println!("Generated minified CSS file!");
85        fs::write(&out_path, minified).expect("Failed to write to CSS file!");
86    }
87}
88
89pub fn collect_css_sources(root: &str, depth: u8) -> Vec<String> {
90    let mut files = Vec::<String>::new();
91
92    let dirs = match fs::read_dir(root) {
93        Ok(dirs) => dirs,
94        Err(_) => return files,
95    };
96
97    for dir_entry in dirs {
98        let dir = dir_entry.unwrap();
99        let dir_name = dir.file_name().into_string().unwrap();
100        let dir_path = dir.path().to_str().unwrap().to_string();
101        if dir_name.ends_with(".css") && dir_name != DEFAULT_OUT_CSS {
102            files.push(dir_path.clone());
103        }
104
105        if depth == 255 {
106            return files;
107        }
108
109        if dir.file_type().unwrap().is_dir() {
110            files.extend(collect_css_sources(&dir_path, depth + 1));
111        }
112    }
113
114    files
115}
116
117#[macro_export]
118macro_rules! rumtk_web_compile_css_bundle {
119    (  ) => {{
120        use $crate::css::{bundle_css, collect_css_sources};
121        use $crate::css::{DEFAULT_OUT_CSS, DEFAULT_OUT_CSS_DIR};
122        let sources = collect_css_sources(DEFAULT_OUT_CSS_DIR, 0);
123        bundle_css(&sources, DEFAULT_OUT_CSS_DIR, DEFAULT_OUT_CSS);
124    }};
125    ( $static_dir_path:expr ) => {{
126        use $crate::css::{bundle_css, collect_css_sources};
127        use $crate::css::{DEFAULT_OUT_CSS, DEFAULT_OUT_CSS_DIR};
128        let sources = collect_css_sources($static_dir_path, 0);
129        bundle_css(&sources, DEFAULT_OUT_CSS_DIR, DEFAULT_OUT_CSS);
130    }};
131}