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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#[allow(warnings)]
///
///## useage of fs module
///```rust,ignore
///fn main() {
///    use doe::*;
///    use doe::DebugPrint;
///    use doe::Str;
///    use std::ops::Deref;
///    // append data to file
///    append_data_to_file("demo.txt", "demo".as_bytes().to_vec()).unwrap();
///
///    // get all files and folders
///    walk_dir(".".to_path().deref()).unwrap().dprintln();
///
///    // get all folders
///    walk_dir_get_files(".".to_path().deref()).unwrap().dprintln();
///
///    // get all folders
///    walk_dir_get_folders(".".to_path().deref())
///        .unwrap()
///        .dprintln();
///
///    //move file the directory to a new directory
///    move_file(
///        "/Users/ryanandrew/code/test/t1/demo.zip".to_path().deref(),
///        "/Users/ryanandrew/code/test/t2/d2.zip".to_path().deref(),
///    )
///    .unwrap();
///
///    //copy file the directory to a new directory
///    copy_file(
///        "/Users/ryanandrew/code/test/t1/demo.zip".to_path().deref(),
///        "/Users/ryanandrew/code/test/t2/d2.zip".to_path().deref(),
///    )
///    .unwrap();
///    //move all files in the directory to a new directory
///    move_folder(
///        "/Users/ryanandrew/code/test/t1".to_path().deref(),
///        "/Users/ryanandrew/code/test/t2/t1".to_path().deref(),
///    )
///    .unwrap();
///
///    //copy all files in the directory to a new directory
///    copy_folder(
///        "/Users/ryanandrew/code/test/d1".to_path().deref(),
///        "/Users/ryanandrew/code/test/d2".to_path().deref(),
///    )
///    .unwrap();
///}
///```
pub mod fs {
    use std::fs::{self, File, OpenOptions};
    use std::io::{self, Write};
    use std::path::{Path, PathBuf};

    ///append data to file
    pub fn append_data_to_file(path: &str, data: Vec<u8>) -> io::Result<()> {
        let mut file = OpenOptions::new().create(true).append(true).open(path)?;

        file.write_all(&data)?;
        Ok(())
    }

    /// get all files
    pub fn walk_dir_get_files(path: impl ToString) -> io::Result<Vec<PathBuf>> {
        let path = path.to_string();
        let mut files = walk_dir(path)?;
        Ok(files.into_iter().filter(|s| s.is_file()).collect())
    }

    /// get all folders
    pub fn walk_dir_get_folders(path: impl ToString) -> io::Result<Vec<PathBuf>> {
        let path = path.to_string();
        let mut files = walk_dir(path)?;
        Ok(files.into_iter().filter(|s| s.is_dir()).collect())
    }

    /// get all files and folders
    pub fn walk_dir(path: impl ToString) -> io::Result<Vec<PathBuf>> {
        let path = path.to_string();
        let dir = Path::new(&path);
        let mut files = Vec::new();
        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let newpath = entry.path();

            if newpath.is_dir() {
                files.extend(walk_dir(&newpath.display())?);
                files.push(newpath);
            } else {
                files.push(newpath);
            }
        }
        Ok(files)
    }
    /// move all file in the directory to a new directory
    pub fn move_file(from: &Path, to: &Path) -> io::Result<()> {
        fs::rename(from, to)?;
        Ok(())
    }
    /// copy all file in the directory to a new directory
    pub fn copy_file(from: &Path, to: &Path) -> io::Result<()> {
        fs::copy(from, to)?;
        Ok(())
    }
    /// move all files in the directory to a new directory
    pub fn move_folder(from: &Path, to: &Path) -> io::Result<()> {
        fs::create_dir_all(to)?;

        let entries = fs::read_dir(from)?;
        for entry in entries {
            let entry = entry?;
            let source_path = entry.path();
            let file_name = entry.file_name();
            let target_path = to.join(file_name);
            // move
            fs::rename(source_path, target_path)?;
        }
        std::fs::remove_dir_all(from)?;

        Ok(())
    }
    /// Copy all files and directories in the directory to a new directory
    pub fn copy_folder(from: &Path, to: &Path) -> io::Result<()> {
        // Create the destination directory if it doesn't exist
        fs::create_dir_all(to)?;

        // Iterate over the entries in the source directory
        for entry in fs::read_dir(from)? {
            let entry = entry?;
            let entry_path = entry.path();

            // Get the destination path by joining the entry's file name with the destination directory
            let dest_path = to.join(entry.file_name());

            // Check if the entry is a file or a directory
            if entry_path.is_file() {
                // Copy the file to the destination directory
                fs::copy(&entry_path, &dest_path)?;
            } else if entry_path.is_dir() {
                // Recursively copy the directory to the destination directory
                copy_folder(&entry_path, &dest_path)?;
            }
        }
        Ok(())
    }
    ///get Home directory
    /// ```rust
    /// fn main(){
    ///     use doe::home_dir;
    ///     use doe::DebugPrint;
    ///      home_dir().expect("REASON").display().dprintln();
    ///  }
    /// ```
    pub fn home_dir() -> Option<PathBuf> {
        if let Some(home_dir) = std::env::var("HOME").ok().map(PathBuf::from) {
            Some(home_dir)
        } else {
            None
        }
    }
}