batata-client 0.0.2

Rust client for Batata/Nacos service discovery and configuration management
Documentation
use std::collections::HashMap;
use uuid::Uuid;

/// Generate a unique request ID
pub fn generate_request_id() -> String {
    Uuid::new_v4().to_string()
}

/// Calculate MD5 hash of content
pub fn md5_hash(content: &str) -> String {
    format!("{:x}", md5::compute(content.as_bytes()))
}

/// Build group key for configuration
pub fn build_config_key(data_id: &str, group: &str, namespace: &str) -> String {
    if namespace.is_empty() {
        format!("{}+{}", data_id, group)
    } else {
        format!("{}+{}+{}", data_id, group, namespace)
    }
}

/// Build service key for naming
pub fn build_service_key(service_name: &str, group_name: &str, namespace: &str) -> String {
    format!("{}@@{}@@{}", namespace, group_name, service_name)
}

/// Parse server address to host and port
pub fn parse_server_address(address: &str) -> (String, u16) {
    if let Some((host, port_str)) = address.rsplit_once(':')
        && let Ok(port) = port_str.parse::<u16>()
    {
        return (host.to_string(), port);
    }
    (address.to_string(), super::constants::DEFAULT_SERVER_PORT)
}

/// Get current timestamp in milliseconds
pub fn current_time_millis() -> i64 {
    chrono::Utc::now().timestamp_millis()
}

/// Merge two HashMaps
pub fn merge_maps(
    base: &HashMap<String, String>,
    override_map: &HashMap<String, String>,
) -> HashMap<String, String> {
    let mut result = base.clone();
    for (k, v) in override_map {
        result.insert(k.clone(), v.clone());
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_generate_request_id() {
        let id1 = generate_request_id();
        let id2 = generate_request_id();
        assert_ne!(id1, id2);
        assert!(!id1.is_empty());
    }

    #[test]
    fn test_md5_hash() {
        let hash = md5_hash("test content");
        assert_eq!(hash.len(), 32);
    }

    #[test]
    fn test_build_config_key() {
        assert_eq!(
            build_config_key("dataId", "group", "namespace"),
            "dataId+group+namespace"
        );
        assert_eq!(build_config_key("dataId", "group", ""), "dataId+group");
    }

    #[test]
    fn test_parse_server_address() {
        assert_eq!(
            parse_server_address("localhost:8848"),
            ("localhost".to_string(), 8848)
        );
        assert_eq!(
            parse_server_address("localhost"),
            ("localhost".to_string(), 8848)
        );
    }
}