use std::path::Path;
use crate::connection::ConnectionRegistry;
pub(crate) struct SessionFacts<'a> {
pub(crate) registry: &'a ConnectionRegistry,
pub(crate) workspace_root: Option<&'a Path>,
}
pub(crate) const SESSION_FACTS_HEADING: &str = "Session facts";
pub(crate) fn session_facts_text(facts: &SessionFacts<'_>) -> Option<String> {
let mut lines = vec![SESSION_FACTS_HEADING.to_string()];
let mut has_facts = false;
for (name, entry) in facts.registry.entries() {
lines.push(format!("- Database `{name}` ({}).", entry.dialect.as_str()));
has_facts = true;
}
match facts.workspace_root {
Some(root) => {
lines.push(format!("- Workspace root: {}.", root.display()));
has_facts = true;
}
None => {
if !facts.registry.is_empty() {
lines.push("- No workspace is bound.".to_string());
has_facts = true;
}
}
}
if facts.registry.is_empty() && facts.workspace_root.is_some() {
lines.push("- No database is connected.".to_string());
}
if has_facts {
Some(lines.join("\n"))
} else {
None
}
}