crates_docs/cli/
test_cmd.rs1use rust_mcp_sdk::schema::ContentBlock;
4use std::sync::Arc;
5
6#[allow(clippy::too_many_arguments)]
8pub async fn run_test_command(
9 tool: &str,
10 crate_name: Option<&str>,
11 item_path: Option<&str>,
12 query: Option<&str>,
13 sort: Option<&str>,
14 version: Option<&str>,
15 limit: u32,
16 format: &str,
17) -> Result<(), Box<dyn std::error::Error>> {
18 tracing::info!("Testing tool: {}", tool);
19
20 let cache_config = crate::cache::CacheConfig::default();
22
23 let cache = crate::cache::create_cache(&cache_config)?;
24 let cache_arc: Arc<dyn crate::cache::Cache> = Arc::from(cache);
25
26 let doc_service = Arc::new(crate::tools::docs::DocService::new(cache_arc)?);
28
29 let registry = crate::tools::create_default_registry(&doc_service);
31
32 match tool {
33 "lookup_crate" => {
34 execute_lookup_crate(crate_name, version, format, ®istry).await?;
35 }
36 "search_crates" => {
37 execute_search_crates(query, sort, limit, format, ®istry).await?;
38 }
39 "lookup_item" => {
40 execute_lookup_item(crate_name, item_path, version, format, ®istry).await?;
41 }
42 "health_check" => {
43 execute_health_check(®istry).await?;
44 }
45 _ => {
46 return Err(format!("Unknown tool: {tool}").into());
47 }
48 }
49
50 println!("Tool test completed");
51 Ok(())
52}
53
54async fn execute_lookup_crate(
56 crate_name: Option<&str>,
57 version: Option<&str>,
58 format: &str,
59 registry: &crate::tools::ToolRegistry,
60) -> Result<(), Box<dyn std::error::Error>> {
61 if let Some(name) = crate_name {
62 println!("Testing crate lookup: {name} (version: {version:?})");
63 println!("Output format: {format}");
64
65 let mut arguments = serde_json::json!({
67 "crate_name": name,
68 "format": format
69 });
70
71 if let Some(v) = version {
72 arguments["version"] = serde_json::Value::String(v.to_string());
73 }
74
75 match registry.execute_tool("lookup_crate", arguments).await {
77 Ok(result) => print_tool_result(&result),
78 Err(e) => eprintln!("Tool execution failed: {e}"),
79 }
80 } else {
81 return Err("lookup_crate requires --crate-name parameter".into());
82 }
83 Ok(())
84}
85
86async fn execute_search_crates(
88 query: Option<&str>,
89 sort: Option<&str>,
90 limit: u32,
91 format: &str,
92 registry: &crate::tools::ToolRegistry,
93) -> Result<(), Box<dyn std::error::Error>> {
94 if let Some(q) = query {
95 println!("Testing crate search: {q} (limit: {limit})");
96 println!("Sort order: {}", sort.unwrap_or("relevance"));
97 println!("Output format: {format}");
98
99 let mut arguments = serde_json::json!({
101 "query": q,
102 "limit": limit,
103 "format": format
104 });
105
106 if let Some(sort) = sort {
107 arguments["sort"] = serde_json::Value::String(sort.to_string());
108 }
109
110 match registry.execute_tool("search_crates", arguments).await {
112 Ok(result) => print_tool_result(&result),
113 Err(e) => eprintln!("Tool execution failed: {e}"),
114 }
115 } else {
116 return Err("search_crates requires --query parameter".into());
117 }
118 Ok(())
119}
120
121async fn execute_lookup_item(
123 crate_name: Option<&str>,
124 item_path: Option<&str>,
125 version: Option<&str>,
126 format: &str,
127 registry: &crate::tools::ToolRegistry,
128) -> Result<(), Box<dyn std::error::Error>> {
129 if let (Some(name), Some(path)) = (crate_name, item_path) {
130 println!("Testing item lookup: {name}::{path} (version: {version:?})");
131 println!("Output format: {format}");
132
133 let mut arguments = serde_json::json!({
135 "crate_name": name,
136 "item_path": path,
137 "format": format
138 });
139
140 if let Some(v) = version {
141 arguments["version"] = serde_json::Value::String(v.to_string());
142 }
143
144 match registry.execute_tool("lookup_item", arguments).await {
146 Ok(result) => print_tool_result(&result),
147 Err(e) => eprintln!("Tool execution failed: {e}"),
148 }
149 } else {
150 return Err("lookup_item requires --crate-name and --item-path parameters".into());
151 }
152 Ok(())
153}
154
155async fn execute_health_check(
157 registry: &crate::tools::ToolRegistry,
158) -> Result<(), Box<dyn std::error::Error>> {
159 println!("Testing health check");
160
161 let arguments = serde_json::json!({
163 "check_type": "all",
164 "verbose": true
165 });
166
167 match registry.execute_tool("health_check", arguments).await {
169 Ok(result) => print_tool_result(&result),
170 Err(e) => eprintln!("Tool execution failed: {e}"),
171 }
172 Ok(())
173}
174
175fn print_tool_result(result: &rust_mcp_sdk::schema::CallToolResult) {
177 println!("Tool executed successfully:");
178 if let Some(content) = result.content.first() {
179 match content {
180 ContentBlock::TextContent(text_content) => {
181 println!("{}", text_content.text);
182 }
183 other => {
184 println!("Non-text content: {other:?}");
185 }
186 }
187 }
188}