//extern crate termion; //use crate::tf::Name::SwaggerPetstore; use std::env; use std::ffi::OsStr; use std::fmt::Debug; use std::fs; use std::fs::File; use std::io::prelude::*; use std::path::Path; use std::path::PathBuf; use std::process::exit; use std::string; use termion::color; use uuid::Uuid; //use uuid; const SWAGGER_PETSTORE_NAME: &str = "swagger-petstore.yaml"; const SWAGGER_PETSTORE_BYTES: &[u8] = include_bytes!("testfiles/swagger-petstore.yaml"); #[derive(Debug, Copy, Clone)] pub struct Info { pub name: Name, pub name_str: &'static str, pub data: &'static [u8], } impl Info { pub fn new(name: Name, name_str: &'static str, data: &'static [u8]) -> Info { Info { name, name_str, data, } } } #[derive(Debug, Copy, Clone)] pub enum Name { SwaggerPetstore, } pub fn file_info(file: Name) -> Info { match file { Name::SwaggerPetstore => { return Info::new( Name::SwaggerPetstore, SWAGGER_PETSTORE_NAME, SWAGGER_PETSTORE_BYTES, ) } } } pub struct Temp { pub path: String, pub info: Info, } impl Drop for Temp { fn drop(&mut self) { let result = std::fs::remove_file(self.path.as_str()); if result.is_err() { eprintln!( "{}{}Unable to drop the temp file.{}", color::Fg(color::Red), line!(), color::Fg(color::Reset) ); } } } /// Saves the test file into a temp directory and return the absolute path to the saved file pub fn create_temp(file: Name) -> Temp { let info = file_info(file); let temp_path_buf = std::env::temp_dir(); let uid = Uuid::new_v4(); // let uid_str = format!("{}", uid); // let temp_path_buf = temp_path_buf.join(uid_str.as_str()); let temp_path_buf = temp_path_buf.join(info.name_str); let maybe_path = temp_path_buf.to_str(); if maybe_path.is_none() { eprintln!( "{}Error creating the temp path.{}", color::Fg(color::Red), color::Fg(color::Reset) ); return Temp { path: String::from("pathfailure"), info, }; } let path = maybe_path.unwrap(); let t = Temp { path: String::from(path), info, }; let mut file_result = File::create(t.path.as_str()); if file_result.is_err() { eprintln!( "{}Error creating the temp file.{}", color::Fg(color::Red), color::Fg(color::Reset) ); return Temp { path: String::from("filefailure"), info, }; } let mut file = file_result.unwrap(); let op_result = file.write_all(t.info.data); if op_result.is_err() { eprintln!( "{}Error writing to the temp file.{}", color::Fg(color::Red), color::Fg(color::Reset) ); return Temp { path: String::from("writefailure"), info, }; } t // // temp_path_buf.join(filename) // // let this_file_buf = PathBuf::from(file!()); // let this_dir = this_file_buf.parent(); // if this_dir.is_none() { // eprintln!( // "{}Error finding the sourcefile path.{}", // color::Fg(color::Red), // color::Fg(color::Reset) // ); // exit(1); // return String::from("fail"); // } // let path_buf = Path::new(this_dir.unwrap()) // .join("..") // .join("..") // .join(filename); // let path_opt = path_buf.to_str(); // if path_opt.is_none() { // eprintln!( // "{}Error concatenating the testfile path.{}", // color::Fg(color::Red), // color::Fg(color::Reset) // ); // exit(1); // return String::from("fail"); // } // // String::from(path_opt.unwrap()) }