df-file 0.1.3

This is an file
Documentation
use std::{env, fs};
use std::fs::{DirBuilder, remove_file};
use std::path::{Path, PathBuf};
use json::{JsonValue, object};
use log::error;

/// 加载配置文件
/// * path  env!("CARGO_MANIFEST_DIR")
pub fn root_path(path: &str) -> &'static str {
    if PathBuf::from(path) != env::current_dir().unwrap() {
        let mut dir = env::current_exe().unwrap();
        dir.pop();
        env::set_current_dir(dir).unwrap();
    }
    let root_path = env::current_dir().unwrap();
    Box::leak(root_path.to_str().unwrap().to_string().into_boxed_str())
}

/// 创建目录
///
/// * path 路径带文件名 /home/roger/foo/bar/baz.txt
pub fn create_dir(mut path: &str) -> bool {
    if path.contains(".") {
        let data = Path::new(path);
        let data = data.parent().unwrap();
        path = data.to_str().unwrap();
    }
    match DirBuilder::new().recursive(true).create(path) {
        Ok(_) => {
            return true;
        }
        Err(_e) => {
            return false;
        }
    }
}

/// 获取文件夹下的文件列表
pub fn dir_files(path: &str) -> Vec<String> {
    let mut files = vec![];
    for entry in walkdir::WalkDir::new(path) {
        match entry {
            Ok(e) => {
                if e.path().is_dir() {
                    continue;
                }
                files.push(e.path().display().to_string())
            }
            _ => {}
        }
    }
    return files;
}

/// 获取目录下文件列表
pub fn get_file_list(path: &str) -> Vec<String> {
    let mut files = vec![];
    let paths = fs::read_dir(path).unwrap();
    for path in paths {
        match path {
            Ok(e) => {
                if e.path().is_file() {
                    files.push(e.file_name().to_str().unwrap().to_string().clone());
                }
            }
            Err(_) => {}
        }
    }
    return files;
}

/// 获取目录下文件夹列表
pub fn get_dir_list(path: &str) -> Vec<String> {
    let mut dirs = vec![];
    let paths = fs::read_dir(path).unwrap();
    for path in paths {
        match path {
            Ok(e) => {
                if e.path().is_dir() {
                    dirs.push(e.file_name().to_str().unwrap().to_string().clone());
                }
            }
            Err(_) => {}
        }
    }
    return dirs;
}


/// 获取文件内容
pub fn file_content_get(path: &str) -> String {
    let data = fs::read_to_string(path);
    match data {
        Ok(content) => {
            return content;
        }
        Err(e) => {
            error!("{}", e);
            return "".to_string();
        }
    }
}

/// 获取文件内容
pub fn file_content_get_stream(path: &str) -> String {
    let file = fs::read(path).unwrap();
    let contents = unsafe { String::from_utf8_unchecked(file) };
    contents
}


/// 获取JSON文件内容
pub fn file_content_get_json(path: &str) -> JsonValue {
    if !is_file(path) {
        return object! {};
    }
    let data = file_content_get(path);
    if data == "" {
        return object! {};
    }
    json::parse(data.as_str()).unwrap()
}

/// 写入文件内容
pub fn file_content_put(path: &str, data: &str) -> bool {
    create_dir(path);
    let data = fs::write(path, data);
    match data {
        Ok(_) => {
            return true;
        }
        Err(e) => {
            error!("{}", e);
            return false;
        }
    }
}

/// 写入文件内容
pub fn file_content_put_u8(path: &str, data: Vec<u8>) -> bool {
    create_dir(path);
    let data = fs::write(path, data);
    match data {
        Ok(_) => {
            return true;
        }
        Err(e) => {
            error!("{}", e);
            return false;
        }
    }
}

/// 判断文件是否存在
pub fn is_file(file: &str) -> bool {
    let o = Path::new(file);
    return o.is_file();
}

/// 判断文件夹是否存在
pub fn is_dir(path: &str) -> bool {
    let o = Path::new(path);
    return o.is_dir();
}

/// 删除文件
pub fn remove(path: &str) -> bool {
    let res = remove_file(path);
    match res {
        Ok(_) => {
            true
        }
        Err(e) => {
            error!("remove file error:{}", e);
            false
        }
    }
}