devalang_core/utils/
file.rs

1use std::{ fs::{ self }, path::Path };
2use include_dir::{ Dir, DirEntry };
3
4pub fn copy_dir_recursive(dir: &Dir, target_root: &Path, base_path: &Path) {
5    for entry in dir.entries() {
6        match entry {
7            DirEntry::Dir(subdir) => {
8                copy_dir_recursive(subdir, target_root, base_path);
9            }
10            DirEntry::File(file) => {
11                let rel_path = file.path().strip_prefix(base_path).unwrap();
12                let dest_path = target_root.join(rel_path);
13
14                if let Some(parent) = dest_path.parent() {
15                    fs::create_dir_all(parent).unwrap();
16                }
17
18                fs::write(&dest_path, file.contents()).expect("Error writing file");
19            }
20        }
21    }
22}
23
24pub fn format_file_size(bytes: u64) -> String {
25    const KB: u64 = 1024;
26    const MB: u64 = 1024 * 1024;
27
28    if bytes >= MB {
29        format!("{:.2} Mb", (bytes as f64) / (MB as f64))
30    } else if bytes >= KB {
31        format!("{:.2} Kb", (bytes as f64) / (KB as f64))
32    } else {
33        format!("{} bytes", bytes)
34    }
35}