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 default;
31mod fonts;
32mod forms;
33mod gap;
34mod index;
35mod layout;
36mod theme;
37
38pub const DEFAULT_OUT_CSS_DIR: &str = "./static/css";
39pub const DEFAULT_OUT_CSS: &str = "bundle.min.css";
40
41pub fn bundle_css(sources: &Vec<String>, out_dir: &str, out_file: &str) {
42    let mut css: RUMString = RUMString::default();
43
44    css += theme::THEME;
45    css += index::BODY;
46    css += basic::BASIC_CSS;
47    css += default::DEFAULT_CSS;
48    css += fonts::FONTS_CSS;
49    css += gap::GAP_CSS;
50    css += animations::ANIMATIONS_CSS;
51    css += forms::FORM_CSS;
52    css += layout::LAYOUT_CSS;
53
54    for source in sources {
55        let css_data = fs::read_to_string(source).unwrap_or_default();
56        css += &css_data;
57    }
58
59    fs::create_dir_all(out_dir).unwrap_or_default();
60
61    let out_path = path::Path::new(out_dir)
62        .join(out_file)
63        .with_extension("css")
64        .to_str()
65        .expect("Could not create path to CSS file!")
66        .to_string();
67
68    let minified = minify_asset(Asset::CSS(&css))
69        .expect("Failed to minify the CSS contents!")
70        .to_rumstring();
71
72    let file_exists = fs::exists(&out_path).unwrap_or_default();
73    let skip_write_css = file_exists
74        && has_same_hash(
75            &minified,
76            &fs::read_to_string(&out_path)
77                .unwrap_or_default()
78                .to_rumstring(),
79        );
80
81    if !skip_write_css {
82        println!("Generated minified CSS file!");
83        fs::write(&out_path, minified).expect("Failed to write to CSS file!");
84    }
85}
86
87pub fn collect_css_sources(root: &str, depth: u8) -> Vec<String> {
88    let mut files = Vec::<String>::new();
89
90    let dirs = match fs::read_dir(root) {
91        Ok(dirs) => dirs,
92        Err(_) => return files,
93    };
94
95    for dir_entry in dirs {
96        let dir = dir_entry.unwrap();
97        let dir_name = dir.file_name().into_string().unwrap();
98        let dir_path = dir.path().to_str().unwrap().to_string();
99        if dir_name.ends_with(".css") && dir_name != DEFAULT_OUT_CSS {
100            files.push(dir_path.clone());
101        }
102
103        if depth == 255 {
104            return files;
105        }
106
107        if dir.file_type().unwrap().is_dir() {
108            files.extend(collect_css_sources(&dir_path, depth + 1));
109        }
110    }
111
112    files
113}
114
115#[macro_export]
116macro_rules! rumtk_web_compile_css_bundle {
117    (  ) => {{
118        use $crate::css::{bundle_css, collect_css_sources};
119        use $crate::css::{DEFAULT_OUT_CSS, DEFAULT_OUT_CSS_DIR};
120        let sources = collect_css_sources(DEFAULT_OUT_CSS_DIR, 0);
121        bundle_css(&sources, DEFAULT_OUT_CSS_DIR, DEFAULT_OUT_CSS);
122    }};
123    ( $static_dir_path:expr ) => {{
124        use $crate::css::{bundle_css, collect_css_sources};
125        use $crate::css::{DEFAULT_OUT_CSS, DEFAULT_OUT_CSS_DIR};
126        let sources = collect_css_sources($static_dir_path, 0);
127        bundle_css(&sources, DEFAULT_OUT_CSS_DIR, DEFAULT_OUT_CSS);
128    }};
129}