trustformers_debug/help/
context.rs1use std::collections::HashMap;
7use std::sync::OnceLock;
8
9pub struct ContextHelp {
11 help_database: HashMap<String, HelpEntry>,
12}
13
14#[derive(Debug, Clone)]
16pub struct HelpEntry {
17 pub topic: String,
18 pub description: String,
19 pub usage: String,
20 pub examples: Vec<String>,
21 pub related_topics: Vec<String>,
22 pub troubleshooting: Vec<String>,
23}
24
25impl Default for ContextHelp {
26 fn default() -> Self {
27 Self::new()
28 }
29}
30
31impl ContextHelp {
32 pub fn new() -> Self {
34 let mut help_database = HashMap::new();
35
36 help_database.insert(
38 "debug_session".to_string(),
39 HelpEntry {
40 topic: "Debug Session".to_string(),
41 description: "Main debugging session that coordinates all debugging tools"
42 .to_string(),
43 usage: "let mut session = debug_session(); session.start().await?;".to_string(),
44 examples: vec![
45 "Basic usage: debug_session()".to_string(),
46 "Custom config: debug_session_with_config(config)".to_string(),
47 ],
48 related_topics: vec!["debug_config".to_string(), "debug_report".to_string()],
49 troubleshooting: vec![
50 "If start() fails, check your configuration".to_string(),
51 "Ensure async runtime is available".to_string(),
52 ],
53 },
54 );
55
56 help_database.insert(
57 "quick_debug".to_string(),
58 HelpEntry {
59 topic: "Quick Debug".to_string(),
60 description: "One-line debugging with smart defaults".to_string(),
61 usage: "let result = debug(&model).await?;".to_string(),
62 examples: vec![
63 "Basic: debug(&model).await?".to_string(),
64 "With level: quick_debug(&model, QuickDebugLevel::Light).await?".to_string(),
65 ],
66 related_topics: vec!["debug_levels".to_string(), "simplified_results".to_string()],
67 troubleshooting: vec![
68 "Use Light level for better performance".to_string(),
69 "Check has_critical_issues() for problems".to_string(),
70 ],
71 },
72 );
73
74 Self { help_database }
77 }
78
79 pub fn get_help(&self, topic: &str) -> Option<&HelpEntry> {
81 self.help_database.get(topic)
82 }
83
84 pub fn search(&self, query: &str) -> Vec<&HelpEntry> {
86 self.help_database
87 .values()
88 .filter(|entry| {
89 entry.topic.to_lowercase().contains(&query.to_lowercase())
90 || entry.description.to_lowercase().contains(&query.to_lowercase())
91 })
92 .collect()
93 }
94
95 pub fn available_topics(&self) -> Vec<String> {
97 self.help_database.keys().cloned().collect()
98 }
99
100 pub fn contextual_help(&self, context: &str) -> Vec<&HelpEntry> {
102 match context.to_lowercase().as_str() {
103 "gradient" => self.search("gradient"),
104 "memory" => self.search("memory"),
105 "performance" => self.search("performance"),
106 "anomaly" => self.search("anomaly"),
107 _ => vec![],
108 }
109 }
110}
111
112static CONTEXT_HELP: OnceLock<ContextHelp> = OnceLock::new();
114
115pub fn context_help() -> &'static ContextHelp {
117 CONTEXT_HELP.get_or_init(ContextHelp::new)
118}
119
120pub fn help(topic: &str) {
122 if let Some(entry) = context_help().get_help(topic) {
123 println!("=== {} ===", entry.topic);
124 println!("{}", entry.description);
125 println!("\nUsage:");
126 println!("{}", entry.usage);
127 println!("\nExamples:");
128 for example in &entry.examples {
129 println!(" {}", example);
130 }
131 if !entry.related_topics.is_empty() {
132 println!("\nRelated topics:");
133 for topic in &entry.related_topics {
134 println!(" {}", topic);
135 }
136 }
137 if !entry.troubleshooting.is_empty() {
138 println!("\nTroubleshooting:");
139 for tip in &entry.troubleshooting {
140 println!(" {}", tip);
141 }
142 }
143 } else {
144 println!("Help topic '{}' not found.", topic);
145 println!("Available topics:");
146 for topic in context_help().available_topics() {
147 println!(" {}", topic);
148 }
149 }
150}
151
152pub fn help_search(query: &str) {
154 let results = context_help().search(query);
155 if results.is_empty() {
156 println!("No help topics found for '{}'", query);
157 } else {
158 println!("Help topics matching '{}':", query);
159 for entry in results {
160 println!(" {} - {}", entry.topic, entry.description);
161 }
162 }
163}