mecha10_cli/dev/
validation.rs

1//! Validation functions for development mode
2
3use anyhow::{anyhow, Context, Result};
4use std::path::Path;
5
6/// Check if the current directory is a Mecha10 project
7pub fn check_project_initialized(project_root: &Path) -> Result<()> {
8    let config_file = project_root.join("mecha10.json");
9
10    if !config_file.exists() {
11        return Err(anyhow!(
12            "Not in a Mecha10 project directory.\n\
13             Run 'mecha10 init' to create a new project."
14        ));
15    }
16
17    Ok(())
18}
19
20/// Check Redis connection
21#[allow(dead_code)] // Planned for future use
22pub async fn check_redis_connection(redis_url: &str) -> Result<()> {
23    let client = redis::Client::open(redis_url).context("Failed to create Redis client")?;
24
25    let mut conn = client
26        .get_multiplexed_async_connection()
27        .await
28        .context("Failed to connect to Redis")?;
29
30    // Test connection with a ping
31    redis::cmd("PING")
32        .query_async::<String>(&mut conn)
33        .await
34        .context("Failed to ping Redis")?;
35
36    Ok(())
37}
38
39/// Validate node names against available nodes
40pub fn validate_node_names(requested_nodes: &[String], available_nodes: &[String]) -> Result<Vec<String>> {
41    let mut invalid = Vec::new();
42
43    for node in requested_nodes {
44        if !available_nodes.contains(node) {
45            invalid.push(node.clone());
46        }
47    }
48
49    if !invalid.is_empty() {
50        return Err(anyhow!(
51            "Unknown nodes: {}\nAvailable nodes: {}",
52            invalid.join(", "),
53            available_nodes.join(", ")
54        ));
55    }
56
57    Ok(requested_nodes.to_vec())
58}