use crate::IResult;

pub trait FileExtend {
    /// 带随机数临时文件夹
    fn create_temporary_folder(prefix: impl Into<String>) -> IResult<String>;

    /// 不带随机数临时文件夹
    fn create_temporary_folder2(prefix: impl Into<String>) -> IResult<String>;
}

impl FileExtend for std::fs::File {
    /// prefix = "xxx-temp"
    fn create_temporary_folder(prefix: impl Into<String>) -> IResult<String> {
        let prefix = prefix.into();
        let dir = std::env::temp_dir();
        let dir_str = dir.to_str().unwrap_or_default();
        let mut dir_str = dir_str.to_string();
        dir_str.push_str(&prefix);
        dir_str.push_str("-");
        let uuid_str = uuid::Uuid::new_v4().to_string().replacen("-", "", 4);
        dir_str.push_str(uuid_str.as_ref());
        let _ = std::fs::create_dir(dir_str.clone());
        Ok(dir_str)
    }

    /// 不带随机数据临时文件夹
    fn create_temporary_folder2(prefix: impl Into<String>) -> IResult<String> {
        let prefix = prefix.into();
        let dir = std::env::temp_dir();
        let dir_str = dir.to_str().unwrap_or_default();
        let mut dir_str = dir_str.to_string();
        dir_str.push_str(&prefix);
        let _ = std::fs::create_dir(dir_str.clone());
        Ok(dir_str)
    }
}

/*#[test]
fn test_create_temporary_folder() {
    let result = File::create_temporary_folder(String::from("test_wrwe"));
    println!("{:?}", result);
}*/
