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