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