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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use {
    crate::mei::Mei,
    std::{
        fs,
        io::ErrorKind,
        path::{Path, PathBuf},
    },
};

pub fn create_dir<P>(dir: P)
where
    P: AsRef<Path>,
{
    fn create_dir_impl(dir: &Path) {
        let dir = canonicalize(dir);
        let mei = Mei::get();
        if mei.verbose() {
            _ = mei.log().info(&format_args!(
                "create directory at {dir}",
                dir = dir.display(),
            ));
        }

        if let Err(err) = fs::create_dir(&dir) {
            assert!(
                err.kind() == ErrorKind::AlreadyExists,
                "failed to create directory at {dir}: {err}",
                dir = dir.display(),
            );
        }
    }

    create_dir_impl(dir.as_ref());
}

pub fn copy<P, Q>(from: P, to: Q)
where
    P: AsRef<Path>,
    Q: AsRef<Path>,
{
    fn copy_impl(from: &Path, to: &Path) {
        let from = canonicalize(from);
        let to = canonicalize(to);
        let mei = Mei::get();
        if mei.verbose() {
            _ = mei.log().info(&format_args!(
                "copy from {from} to {to}",
                from = from.display(),
                to = to.display(),
            ));
        }

        if let Err(err) = fs::copy(&from, &to) {
            panic!(
                "failed to copy from {from} to {to}: {err}",
                from = from.display(),
                to = to.display(),
            );
        }
    }

    copy_impl(from.as_ref(), to.as_ref());
}

pub fn write<P, C>(path: P, contents: C)
where
    P: AsRef<Path>,
    C: AsRef<[u8]>,
{
    fn write_impl(path: &Path, contents: &[u8]) {
        let path = canonicalize(path);
        let mei = Mei::get();
        if mei.verbose() {
            _ = mei
                .log()
                .info(&format_args!("write to file {path}", path = path.display()));
        }

        if let Some(parent) = path.parent() {
            if !parent.exists() {
                create_dir(parent);
            }
        }

        if let Err(err) = fs::write(&path, contents) {
            panic!("failed to write to {path}: {err}", path = path.display());
        }
    }

    write_impl(path.as_ref(), contents.as_ref());
}

pub fn read_to_string<P>(path: P) -> String
where
    P: AsRef<Path>,
{
    fn read_to_string_impl(path: &Path) -> String {
        let path = canonicalize(path);
        let mei = Mei::get();
        if mei.verbose() {
            _ = mei
                .log()
                .info(&format_args!("read file {path}", path = path.display()));
        }

        match fs::read_to_string(&path) {
            Ok(content) => content,
            Err(err) => panic!("failed to read from {path}: {err}", path = path.display()),
        }
    }

    read_to_string_impl(path.as_ref())
}

fn canonicalize(path: &Path) -> PathBuf {
    path.canonicalize().unwrap_or_else(|_| path.to_owned())
}