rs-zero 0.1.1

Rust-first microservice framework inspired by go-zero engineering practices
Documentation
use crate::discovery::{Discovery, DiscoveryError, DiscoveryResult, ServiceInstance};
use async_trait::async_trait;

use crate::discovery_kube::{KubeDiscoveryConfig, map_endpoints};

/// Kubernetes discovery facade backed by a refreshable fixture snapshot.
#[derive(Debug, Clone)]
pub struct KubeDiscovery {
    config: KubeDiscoveryConfig,
    service: String,
    snapshot: String,
}

impl KubeDiscovery {
    /// Creates a discovery facade from a fixture snapshot.
    pub fn from_snapshot(
        config: KubeDiscoveryConfig,
        service: impl Into<String>,
        snapshot: impl Into<String>,
    ) -> Self {
        Self {
            config,
            service: service.into(),
            snapshot: snapshot.into(),
        }
    }
}

#[async_trait]
impl Discovery for KubeDiscovery {
    async fn discover(&self, service: &str) -> DiscoveryResult<Vec<ServiceInstance>> {
        if service != self.service {
            return Err(DiscoveryError::NoInstances {
                service: service.to_string(),
            });
        }
        let instances = map_endpoints(&self.config, &self.snapshot).map_err(|error| {
            DiscoveryError::Resolve {
                host: "kubernetes".to_string(),
                message: error.to_string(),
            }
        })?;
        if instances.is_empty() {
            Err(DiscoveryError::NoInstances {
                service: service.to_string(),
            })
        } else {
            Ok(instances)
        }
    }
}