use std::fmt;
use super::dot_table::{DotTable};
pub struct DotFile {
header : String,
dot_tables: Vec<DotTable>,
relations: Vec<String>,
footer : String,
dark_mode: bool
}
impl fmt::Display for DotFile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{0}\n{1}\n{2}\n\n{3}",
self.header,
self.dot_tables.iter().map(|s| s.to_string()).collect::<Vec<String>>().join("\n"),
self.relations.join("\n"),
self.footer
)
}
}
impl DotFile {
pub fn new(filename : &str, dark_mode : bool) -> DotFile {
DotFile {
header : init_dot(filename, dark_mode),
dot_tables: Vec::new(),
relations: Vec::new(),
footer: String::from("}"),
dark_mode
}
}
pub fn add_table(&mut self, table : DotTable) {
self.dot_tables.push(table);
}
pub fn add_relation(&mut self, table_name: &str, table_end: &str, key: &str, refered: &str){
self.relations.push(generate_relation(table_name, table_end, key, refered, self.dark_mode))
}
}
fn init_dot(filename: &str, dark_mode: bool) -> String {
let bg_color : &str = match dark_mode {
true => "bgcolor= black;",
false => "",
};
format!("//This file has been generated with doteur, enjoy!
digraph {0} {{\n
{1}
node [\n
shape = \"plaintext\"
]\n\n", filename, bg_color)
}
fn generate_relation(table_name: &str, table_end: &str, key: &str, refered: &str, dark_mode: bool) -> String {
let color_scheme : &str = match dark_mode {
true => "fontcolor=white, color=white",
false => ""
};
let refer : &str = match cfg!(unix) {
true => "\u{27A1}",
_ => "refers"
};
format!("\t{0} -> {1} [label=<<I>{2} {3} {4}</I>>, arrowhead = \"dot\", fontsize=\"12.0\", {5}]", table_name, table_end, key, refer, refered, color_scheme)
}