ayun_core/support/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
pub fn type_name<T>() -> String
where
    T: std::any::Any + ?Sized,
{
    std::any::type_name::<T>()
        .split("::")
        .last()
        .expect("Cannot get type name")
        .to_string()
}

pub fn copy(from: &std::path::Path, to: &std::path::Path) -> crate::Result<()> {
    match std::fs::metadata(from)?.is_dir() {
        false => {
            std::fs::copy(from, to)?;
        }
        true => {
            std::fs::create_dir_all(to)?;

            for entry in std::fs::read_dir(from)? {
                let entry = entry?;
                let from = entry.path();
                let to = to.join(entry.file_name());

                if from.is_dir() {
                    copy(&from, &to)?;
                } else {
                    std::fs::copy(&from, to)?;
                }
            }
        }
    };

    Ok(())
}

pub fn base_path(str: &str) -> std::path::PathBuf {
    let base_path = std::env::var("CARGO_MANIFEST_DIR")
        .map(std::path::PathBuf::from)
        .unwrap_or_else(|_| {
            std::env::current_dir()
                .expect("project directory does not exist or permissions are insufficient")
        });

    base_path.join(str)
}