use super::dependency;
use super::escape::encode_segment;
use super::layout::{document, link, list, paragraph, row, section, table, text_cell};
use super::{Info, Module};
const DETAILS_HEADERS: [&str; 2] = ["Ключ", "Значение"];
pub(super) fn render(name: &str, info: &Info) -> Option<String> {
let module = info
.modules
.iter()
.find(|candidate| candidate.name.as_str() == name)?;
let mut body = String::new();
body.push_str(¶graph(&format!("Фаза: {}", module.phase)));
body.push_str(§ion("Проблемы", &problems_block(module)));
body.push_str(§ion("Зависимости", &dependencies_block(module)));
body.push_str(§ion("Сведения", &details_block(module)));
if let Some(config) = module.config.as_deref() {
let href = format!("/config/{}", encode_segment(config));
let label = format!("Конфигурация: {config}");
body.push_str(&format!("<p>{}</p>\n", link(&href, &label)));
}
Some(document(
&format!("Модуль {name}"),
&body,
true,
&info.platform_version,
))
}
fn problems_block(module: &Module) -> String {
if module.problems.is_empty() {
return paragraph("проблем нет");
}
let items: Vec<String> = module
.problems
.iter()
.map(|(source, text)| escape_item(source, text))
.collect();
list(&items)
}
fn dependencies_block(module: &Module) -> String {
if module.dependencies.is_empty() {
return paragraph("зависимостей нет");
}
let rows: Vec<String> = module
.dependencies
.iter()
.map(|(name, dep)| dependency::row_cells(None, name, dep))
.collect();
table(&dependency::HEADERS, &rows)
}
fn details_block(module: &Module) -> String {
if module.details.is_empty() {
return paragraph("сведений нет");
}
let rows: Vec<String> = module
.details
.iter()
.map(|(key, value)| row(&[text_cell(key), text_cell(value)]))
.collect();
table(&DETAILS_HEADERS, &rows)
}
fn escape_item(source: &str, text: &str) -> String {
super::escape::escape_text(&format!("{source}: {text}"))
}