df_file/
lib.rs

1use std::{env, fs};
2use std::fs::{DirBuilder, remove_file};
3use std::path::{Path, PathBuf};
4use json::{JsonValue, object};
5use log::error;
6
7/// 加载配置文件
8/// * path  env!("CARGO_MANIFEST_DIR")
9pub fn root_path(path: &str) -> &'static str {
10    if PathBuf::from(path) != env::current_dir().unwrap() {
11        let mut dir = env::current_exe().unwrap();
12        dir.pop();
13        env::set_current_dir(dir).unwrap();
14    }
15    let root_path = env::current_dir().unwrap();
16    Box::leak(root_path.to_str().unwrap().to_string().into_boxed_str())
17}
18
19/// 创建目录
20///
21/// * path 路径带文件名 /home/roger/foo/bar/baz.txt
22pub fn create_dir(mut path: &str) -> bool {
23    if path.contains(".") {
24        let data = Path::new(path);
25        let data = data.parent().unwrap();
26        path = data.to_str().unwrap();
27    }
28    match DirBuilder::new().recursive(true).create(path) {
29        Ok(_) => {
30            return true;
31        }
32        Err(_e) => {
33            return false;
34        }
35    }
36}
37
38/// 获取文件夹下的文件列表
39pub fn dir_files(path: &str) -> Vec<String> {
40    let mut files = vec![];
41    for entry in walkdir::WalkDir::new(path) {
42        match entry {
43            Ok(e) => {
44                if e.path().is_dir() {
45                    continue;
46                }
47                files.push(e.path().display().to_string())
48            }
49            _ => {}
50        }
51    }
52    return files;
53}
54
55/// 获取目录下文件列表
56pub fn get_file_list(path: &str) -> Vec<String> {
57    let mut files = vec![];
58    let paths = fs::read_dir(path).unwrap();
59    for path in paths {
60        match path {
61            Ok(e) => {
62                if e.path().is_file() {
63                    files.push(e.file_name().to_str().unwrap().to_string().clone());
64                }
65            }
66            Err(_) => {}
67        }
68    }
69    return files;
70}
71
72/// 获取目录下文件夹列表
73pub fn get_dir_list(path: &str) -> Vec<String> {
74    let mut dirs = vec![];
75    let paths = fs::read_dir(path).unwrap();
76    for path in paths {
77        match path {
78            Ok(e) => {
79                if e.path().is_dir() {
80                    dirs.push(e.file_name().to_str().unwrap().to_string().clone());
81                }
82            }
83            Err(_) => {}
84        }
85    }
86    return dirs;
87}
88
89
90/// 获取文件内容
91pub fn file_content_get(path: &str) -> String {
92    let data = fs::read_to_string(path);
93    match data {
94        Ok(content) => {
95            return content;
96        }
97        Err(e) => {
98            error!("{}", e);
99            return "".to_string();
100        }
101    }
102}
103
104/// 获取文件内容
105pub fn file_content_get_stream(path: &str) -> String {
106    let file = fs::read(path).unwrap();
107    let contents = unsafe { String::from_utf8_unchecked(file) };
108    contents
109}
110
111
112/// 获取JSON文件内容
113pub fn file_content_get_json(path: &str) -> JsonValue {
114    if !is_file(path) {
115        return object! {};
116    }
117    let data = file_content_get(path);
118    if data == "" {
119        return object! {};
120    }
121    json::parse(data.as_str()).unwrap()
122}
123
124/// 写入文件内容
125pub fn file_content_put(path: &str, data: &str) -> bool {
126    create_dir(path);
127    let data = fs::write(path, data);
128    match data {
129        Ok(_) => {
130            return true;
131        }
132        Err(e) => {
133            error!("{}", e);
134            return false;
135        }
136    }
137}
138
139/// 写入文件内容
140pub fn file_content_put_u8(path: &str, data: Vec<u8>) -> bool {
141    create_dir(path);
142    let data = fs::write(path, data);
143    match data {
144        Ok(_) => {
145            return true;
146        }
147        Err(e) => {
148            error!("{}", e);
149            return false;
150        }
151    }
152}
153
154/// 判断文件是否存在
155pub fn is_file(file: &str) -> bool {
156    let o = Path::new(file);
157    return o.is_file();
158}
159
160/// 判断文件夹是否存在
161pub fn is_dir(path: &str) -> bool {
162    let o = Path::new(path);
163    return o.is_dir();
164}
165
166/// 删除文件
167pub fn remove(path: &str) -> bool {
168    let res = remove_file(path);
169    match res {
170        Ok(_) => {
171            true
172        }
173        Err(e) => {
174            error!("remove file error:{}", e);
175            false
176        }
177    }
178}