use serde_json::Value;
use super::{
MAX_INPUT_LENGTH, MAX_PATH_LENGTH,
response::{ToolResult, error_result},
};
pub(super) fn validate_required_string(value: Option<&Value>, name: &str) -> Option<ToolResult> {
let Some(value) = value.and_then(Value::as_str) else {
return Some(error_result(&format!("{name} must be a non-empty string")));
};
if value.is_empty() {
return Some(error_result(&format!("{name} must be a non-empty string")));
}
if value.len() > MAX_INPUT_LENGTH {
return Some(error_result(&format!(
"{name} exceeds maximum length of {MAX_INPUT_LENGTH} characters (got {})",
value.len()
)));
}
None
}
pub(super) fn validate_optional_path(value: Option<&Value>, name: &str) -> Option<ToolResult> {
let value = value?;
if value.is_null() {
return None;
}
let Some(value) = value.as_str() else {
return Some(error_result(&format!("{name} must be a string")));
};
if value.len() > MAX_PATH_LENGTH {
return Some(error_result(&format!(
"{name} exceeds maximum length of {MAX_PATH_LENGTH} characters (got {})",
value.len()
)));
}
None
}
pub(super) fn not_indexed_message(searched: Option<&str>) -> String {
let searched = searched.unwrap_or(".");
format!(
"No RustCodeGraph project is loaded for this session.\nSearched for a .rustcodegraph/ directory starting from: {searched}\nIf this project is indexed, pass projectPath to the tool call or add --path to the MCP server config. If the project has no index, continue with built-in tools; indexing is the user's decision."
)
}
pub(super) fn unindexed_project_path_message(project_path: &str) -> String {
format!(
"Project path {project_path} isn't indexed.\nRun `rustcodegraph init -i` in that project to enable RustCodeGraph, or continue with built-in tools; indexing is the user's decision."
)
}