use std::fs;
use std::path::{Path, PathBuf};
use super::restriction::{Restriction};
pub const POSSIBLE_DOTS_OUTPUT : [&str; 54] = ["bmp", "canon", "gv", "xdot", "xdot1.2", "xdot1.4",
"cgimage", "cmap", "eps", "eps", "exr", "fig", "gd",
"gd2" , "gif", "gtk", "ico", "imap", "cmapx", "imap_np",
"cmapx_np", "ismap", "jp2", "jpg", "jpeg", "jpe", "json",
"json0", "dot_json", "xdot_json", "pct", "pict","pdf",
"pic", "plain", "plain-ext", "png", "pov", "ps2", "psd",
"sgi", "svg", "svgz", "tga", "tif", "tiff", "tk", "vml",
"vmlz", "vrml", "wbmp", "webp", "xlib", "x11"];
#[derive(Clone)]
pub struct Args {
filename: String,
filecontent: String,
output_filename: String,
restrictions: Option<Restriction>,
dark_mode: bool
}
impl Args {
pub fn new(path_str: Vec<&str>) -> Args {
let filename : String;
if path_str.len() != 1 {
filename = String::from("multifilearg");
} else {
filename = Path::new(path_str.first().unwrap()).file_name().expect("Incorrect file name").to_str().unwrap().to_string();
}
Args {
filename,
filecontent: path_str.iter().map(|path| {
if Path::new(path).is_dir() {
fs::read_dir(path).expect("Directory can't be read").map(|file|
{
let file_path : &PathBuf = &file.unwrap().path();
if Path::new(file_path).is_file() {
fs::read_to_string(file_path).unwrap_or_else(|_| panic!("Something went wrong while reading the file : {}", file_path.as_path().to_str().unwrap_or("**ISSUE**")))
} else {
String::new()
}
}).collect::<Vec<String>>().join("\n")
} else {
fs::read_to_string(path).unwrap_or_else(|_| panic!("Something went wrong while reading the file : {}", path))
}
}).collect::<Vec<String>>().join("\n"),
output_filename: String::from("output.dot"),
restrictions: None,
dark_mode: false
}
}
pub fn get_filename_without_specials(&self) -> String {
self.filename.chars().filter(|c| c.is_ascii_alphanumeric() || c.is_ascii_whitespace()).collect::<String>()
}
pub fn get_output_file_ext(&self) -> &str {
std::path::Path::new(self.output_filename.as_str()).extension().unwrap_or_default().to_str().unwrap_or_default()
}
pub fn ext_supported(ext: &str) -> bool {
POSSIBLE_DOTS_OUTPUT.iter().any(|&i| i == ext)
}
pub fn get_filecontent(&self) -> &str {
self.filecontent.as_str()
}
pub fn get_output_filename(&self) -> &str {
self.output_filename.as_str()
}
pub fn set_output_filename(&mut self, output_filename : String) {
self.output_filename = output_filename;
}
pub fn get_restrictions(&self) -> Option<&Restriction> {
self.restrictions.as_ref()
}
pub fn set_inclusions(&mut self, inclusions : Vec<String>) {
self.restrictions = Some(Restriction::new_inclusion(inclusions));
}
pub fn set_exclusions(&mut self, exclusions : Vec<String>) {
self.restrictions = Some(Restriction::new_exclusion(exclusions));
}
pub fn get_dark_mode(&self) -> bool {
self.dark_mode
}
pub fn set_dark_mode(&mut self, dark_mode: bool){
self.dark_mode = dark_mode;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_file_ext() {
assert_eq!({
let args = Args::new(vec!["./examples/samplefile3.sql"]);
args.clone().get_output_file_ext()
}, "dot", "default value");
assert_eq!({
let mut args = Args::new(vec!["./examples/samplefile3.sql", "./examples/samplefile1.sql"]);
args.set_output_filename("hello.png".to_string());
args.clone().get_output_file_ext()
}, "png", "set value with multifile");
assert_eq!({
let mut args = Args::new(vec!["./examples"]);
args.set_output_filename("./path/to/file/hello.png".to_string());
args.clone().get_output_file_ext()
}, "png", "set value");
}
#[test]
fn test_file_in_list() {
assert!(POSSIBLE_DOTS_OUTPUT.iter().all(|e| Args::ext_supported(e)), "normal use cases");
assert!(!Args::ext_supported("dot"), "normal use case, we don't handle the dot files to the graphviz tool");
assert!(!Args::ext_supported("file.png"), "normal use case, only the extension should be given");
}
}