finish up annotate and cleanup project

This commit is contained in:
2025-04-16 14:49:47 +04:00
parent 1a745ff17f
commit 2b668ba89f
18 changed files with 161 additions and 793 deletions

View File

@@ -1,5 +1,6 @@
use color_eyre::Result;
use minijinja::Environment;
use serde::Serialize;
use crate::generator::modules::{
discovery::{db::DbType, table::Table},
@@ -7,13 +8,26 @@ use crate::generator::modules::{
};
use comfy_table::{Cell, ContentArrangement, Table as CTable};
use super::{AnnotateCommentConfig, COMMENTBODY, COMMENTHEAD, COMMENTTAIL};
use super::{
AnnotateCommentConfig, COMMENTBODY, COMMENTHEAD, COMMENTTAIL, HEADER, SETTINGSDELIMITER,
};
#[derive(Debug, Serialize)]
struct CommentContext<'a> {
pub table_name: &'a str,
pub config: &'a AnnotateCommentConfig,
pub column_info_table: String,
pub comment_config: Option<String>,
pub config_delimiter: &'a str,
}
pub fn generate_comment(
table: &Table,
config: &AnnotateCommentConfig,
environment: &Environment<'static>,
db_type: &DbType,
date_time_crate: &DateTimeCrate,
comment_config: Option<AnnotateCommentConfig>,
) -> Result<String> {
let mut column_info_table = CTable::new();
let mut header = Vec::new();
@@ -50,38 +64,35 @@ pub fn generate_comment(
row.push(Cell::new(column_type));
}
if config.column_attributes.unwrap() {
let attrs_string = column.attrs_to_string();
let exclude = config.column_exclude_attributes.clone().unwrap();
let filter: Box<dyn Fn(&String) -> bool> = Box::new(move |f| {
let exclude = exclude.clone();
!exclude.contains(f)
});
let attrs_string = column.attrs_to_string(Some(filter));
row.push(Cell::new(attrs_string));
}
column_info_table.add_row(row);
}
// column_info_table.to_string()
// let config_part = match parsed_settings {
// Some(settings) => {
// let settings_str = serde_yaml::to_string(&settings)?;
// let settings_str = settings_str
// .lines()
// .map(|line| format!(" {}", line))
// .collect::<Vec<_>>()
// .join("\n");
// format!(
// "{SETTINGSDELIMITER}\n{}\n{SETTINGSDELIMITER}\n\n",
// settings_str
// )
// }
// None => String::new(),
// };
let context = CommentContext {
table_name: &table.name,
config,
column_info_table: column_info_table.to_string(),
comment_config: comment_config
.and_then(|f| toml::to_string_pretty(&f).ok())
.map(|s| {
s.lines()
.map(|line| format!(" {}", line))
.collect::<Vec<_>>()
.join("\n")
}),
config_delimiter: SETTINGSDELIMITER,
};
let template = environment.get_template("annotate.comment")?;
let rendered_data = template.render(&context)?;
// let table_name = &table.name;
// let table_name_str = if config.table_name {
// format!("Table: {}\n", table_name)
// } else {
// String::new()
// };
// let string = format!("{HEADER}\n{config_part}{table_name_str}\n{column_info_table}");
// let padded_string = Self::pad_comment(&string);
Ok(String::new())
Ok(pad_comment(&rendered_data))
}
pub fn pad_comment(s: &str) -> String {
@@ -90,7 +101,7 @@ pub fn pad_comment(s: &str) -> String {
for (index, part) in parts.iter().enumerate() {
let first = index == 0;
let comment = match first {
true => COMMENTHEAD.to_string(),
true => format!("{} {}\n{}", COMMENTHEAD, HEADER, COMMENTBODY),
false => COMMENTBODY.to_string(),
};
let padded_part = format!("{} {}\n", comment, part);
@@ -99,3 +110,12 @@ pub fn pad_comment(s: &str) -> String {
padded.push_str(COMMENTTAIL);
padded
}
pub fn find_settings_block(file_content: &str) -> Option<String> {
let delimiter_length = SETTINGSDELIMITER.len();
let start_pos = file_content.find(SETTINGSDELIMITER)?;
let end_pos = file_content[start_pos + delimiter_length..].find(SETTINGSDELIMITER)?;
let content = &file_content[start_pos + delimiter_length..start_pos + end_pos];
let content = content.replace(&format!("\n{COMMENTBODY}"), "\n");
Some(content)
}