use std::collections::HashMap;
use std::sync::OnceLock;
pub struct ContextHelp {
help_database: HashMap<String, HelpEntry>,
}
#[derive(Debug, Clone)]
pub struct HelpEntry {
pub topic: String,
pub description: String,
pub usage: String,
pub examples: Vec<String>,
pub related_topics: Vec<String>,
pub troubleshooting: Vec<String>,
}
impl Default for ContextHelp {
fn default() -> Self {
Self::new()
}
}
impl ContextHelp {
pub fn new() -> Self {
let mut help_database = HashMap::new();
help_database.insert(
"debug_session".to_string(),
HelpEntry {
topic: "Debug Session".to_string(),
description: "Main debugging session that coordinates all debugging tools"
.to_string(),
usage: "let mut session = debug_session(); session.start().await?;".to_string(),
examples: vec![
"Basic usage: debug_session()".to_string(),
"Custom config: debug_session_with_config(config)".to_string(),
],
related_topics: vec!["debug_config".to_string(), "debug_report".to_string()],
troubleshooting: vec![
"If start() fails, check your configuration".to_string(),
"Ensure async runtime is available".to_string(),
],
},
);
help_database.insert(
"quick_debug".to_string(),
HelpEntry {
topic: "Quick Debug".to_string(),
description: "One-line debugging with smart defaults".to_string(),
usage: "let result = debug(&model).await?;".to_string(),
examples: vec![
"Basic: debug(&model).await?".to_string(),
"With level: quick_debug(&model, QuickDebugLevel::Light).await?".to_string(),
],
related_topics: vec!["debug_levels".to_string(), "simplified_results".to_string()],
troubleshooting: vec![
"Use Light level for better performance".to_string(),
"Check has_critical_issues() for problems".to_string(),
],
},
);
Self { help_database }
}
pub fn get_help(&self, topic: &str) -> Option<&HelpEntry> {
self.help_database.get(topic)
}
pub fn search(&self, query: &str) -> Vec<&HelpEntry> {
self.help_database
.values()
.filter(|entry| {
entry.topic.to_lowercase().contains(&query.to_lowercase())
|| entry.description.to_lowercase().contains(&query.to_lowercase())
})
.collect()
}
pub fn available_topics(&self) -> Vec<String> {
self.help_database.keys().cloned().collect()
}
pub fn contextual_help(&self, context: &str) -> Vec<&HelpEntry> {
match context.to_lowercase().as_str() {
"gradient" => self.search("gradient"),
"memory" => self.search("memory"),
"performance" => self.search("performance"),
"anomaly" => self.search("anomaly"),
_ => vec![],
}
}
}
static CONTEXT_HELP: OnceLock<ContextHelp> = OnceLock::new();
pub fn context_help() -> &'static ContextHelp {
CONTEXT_HELP.get_or_init(ContextHelp::new)
}
pub fn help(topic: &str) {
if let Some(entry) = context_help().get_help(topic) {
println!("=== {} ===", entry.topic);
println!("{}", entry.description);
println!("\nUsage:");
println!("{}", entry.usage);
println!("\nExamples:");
for example in &entry.examples {
println!(" {}", example);
}
if !entry.related_topics.is_empty() {
println!("\nRelated topics:");
for topic in &entry.related_topics {
println!(" {}", topic);
}
}
if !entry.troubleshooting.is_empty() {
println!("\nTroubleshooting:");
for tip in &entry.troubleshooting {
println!(" {}", tip);
}
}
} else {
println!("Help topic '{}' not found.", topic);
println!("Available topics:");
for topic in context_help().available_topics() {
println!(" {}", topic);
}
}
}
pub fn help_search(query: &str) {
let results = context_help().search(query);
if results.is_empty() {
println!("No help topics found for '{}'", query);
} else {
println!("Help topics matching '{}':", query);
for entry in results {
println!(" {} - {}", entry.topic, entry.description);
}
}
}