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
use std::env; use std::path::Path; use rand::{thread_rng, Rng}; use rand::distributions::Alphanumeric; use std::fs::{create_dir_all,File,read_to_string,remove_file}; use std::io::Write; #[allow(dead_code)] pub fn cwd() -> String { match env::current_dir() { Ok(path)=>{ match path.to_str() { Some(str)=>{ return str.to_string(); }, None=>{ return String::new(); } } }, Err(_)=>{ return String::new(); } } } #[allow(dead_code)] pub fn ensure_dir(path:&String) -> bool { match create_dir_all(path) { Ok(_)=>{ return true; }, Err(_)=>{ return false; } } } #[allow(dead_code)] pub fn check_path(path:&String) -> bool { Path::new(path).exists() } #[allow(dead_code)] pub fn write_control(path:String,data:Vec<u8>) -> Result<(),String> { match File::create(&path) { Ok(mut file)=>{ match file.write(&data) { Ok(_)=>{ return Ok(()); }, Err(e)=>{ let error = format!("failed-write_data-write_control=>{}",e); return Err(error); } } }, Err(_)=>{ return Err("failed-open_file-write_control".to_string()); } } } #[allow(dead_code)] pub fn get_random_file_name() -> String { let connection_id: String = thread_rng() .sample_iter(&Alphanumeric) .take(32) .collect(); return connection_id; } #[allow(dead_code)] pub fn new_file(path:&String) -> bool { match File::create(path){ Ok(_)=>{ return true; }, Err(_)=>{ return false; } } } #[allow(dead_code)] pub fn read(path:&String) -> Result<String,String> { match read_to_string(path){ Ok(str)=>{ return Ok(str); }, Err(_)=>{ return Err("failed to read the file as string".to_string()); } } } pub fn delete_file(path:String) -> Result<(),String> { match remove_file(path) { Ok(_)=>{ return Ok(()); }, Err(e)=>{ let error = format!("failed-delete_file=>{}",e); return Err(error); } } }