use std::fmt;
use super::super::add_traits::{Trim};
use super::attribute::{Attribute, KeyValueMap};
pub struct DotTable {
header: String,
attributes: Vec<Attribute>,
footer: String,
dark_mode: bool
}
impl fmt::Display for DotTable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{0}\n{1}\n\n\t{2}\n", self.header, self.attributes.iter().map(|s| s.to_string()).collect::<Vec<String>>().join("\n"), self.footer)
}
}
impl DotTable {
pub fn new(table_name: &str, dark_mode: bool) -> DotTable {
let header : String = generate_table_header(table_name, dark_mode);
DotTable {
header,
attributes: Vec::new(),
footer: String::from("</TABLE> >]"),
dark_mode
}
}
pub fn add_attribute(&mut self, title: &str, desc : &str) {
self.attributes.push(Attribute::new_col_def(title.to_string(), desc.to_string(), self.dark_mode));
}
pub fn add_attribute_pk(&mut self, key: &str, desc: &str) {
self.attributes.push_or_replace_attribute(Attribute::new_pk(key.to_string(), desc.to_string(), self.dark_mode));
}
pub fn add_fk_nature_to_attribute(&mut self, key: &str, fk_table : &str, fk_col : &str) -> Result<usize, &'static str> {
self.attributes.add_fk_nature_to_attribute(key, fk_table, fk_col)
}
pub fn add_pk_nature_to_attribute(&mut self, key: &str) -> Result<usize, &'static str>{
self.attributes.add_pk_nature_to_attribute(key)
}
}
fn generate_table_header(name: &str, dark_mode: bool) -> String {
let styles : (&str, &str) = match dark_mode {
true => ("grey20", "grey10"),
false => ("grey95", "indigo")
};
format!("
{0} [label=<
<TABLE BGCOLOR=\"{1}\" BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\">
<TR><TD COLSPAN=\"2\" CELLPADDING=\"5\" ALIGN=\"CENTER\" BGCOLOR=\"{2}\">
<FONT FACE=\"Roboto\" COLOR=\"white\" POINT-SIZE=\"12\">
<B>{0}</B>
</FONT></TD></TR>", name.trim_leading_trailing(), styles.0, styles.1)
}