Skip to main content

koi_common/
integration.rs

1//! Integration traits for cross-crate abstractions.
2//!
3//! These traits define contracts that domain crates implement
4//! and the binary crate wires together, without domain crates
5//! importing each other.
6//!
7//! Domain crates depend only on `koi-common`, never on each other.
8//! The binary crate (or `koi-embedded`) provides bridge implementations
9//! that wrap concrete domain cores and implement these traits.
10
11use std::collections::HashMap;
12use std::net::IpAddr;
13
14use chrono::{DateTime, Utc};
15use serde::{Deserialize, Serialize};
16
17use crate::types::ServiceRecord;
18
19// ── Summary types ──────────────────────────────────────────────────
20
21/// Summary of a certmesh member, projected through the trait boundary.
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct MemberSummary {
24    pub hostname: String,
25    pub sans: Vec<String>,
26    pub cert_expires: Option<DateTime<Utc>>,
27    pub last_seen: Option<DateTime<Utc>>,
28    pub status: String,
29    pub proxy_entries: Vec<ProxyConfigSummary>,
30}
31
32/// Proxy configuration entry projected through the trait boundary.
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct ProxyConfigSummary {
35    pub name: String,
36    pub listen_port: u16,
37    pub backend: String,
38    pub allow_remote: bool,
39}
40
41/// Lightweight proxy entry used by health checks.
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct ProxyEntrySummary {
44    pub name: String,
45    pub listen_port: u16,
46    pub backend: String,
47}
48
49// ── Cross-domain traits ────────────────────────────────────────────
50
51/// Read-only snapshot of the certmesh roster.
52pub trait CertmeshSnapshot: Send + Sync {
53    /// Return summaries of all active members.
54    fn active_members(&self) -> Vec<MemberSummary>;
55}
56
57/// Read-only snapshot of mDNS network state.
58pub trait MdnsSnapshot: Send + Sync {
59    /// Map of hostname → IP derived from mDNS service records.
60    fn host_ips(&self) -> HashMap<String, IpAddr>;
61
62    /// All cached mDNS service records (for DNS alias building).
63    fn cached_records(&self) -> Vec<ServiceRecord>;
64}
65
66/// Resolve a local DNS name without importing the DNS crate.
67pub trait DnsProbe: Send + Sync {
68    /// Resolve a local name to IP addresses (A or AAAA).
69    fn resolve_local(&self, name: &str) -> Option<Vec<IpAddr>>;
70}
71
72/// Read-only snapshot of proxy entries.
73pub trait ProxySnapshot: Send + Sync {
74    /// Return all configured proxy entries.
75    fn entries(&self) -> Vec<ProxyEntrySummary>;
76}
77
78/// Write-back channel for DNS alias feedback to certmesh.
79///
80/// When the DNS resolver discovers mDNS aliases, it can push them
81/// to certmesh so that certificates include the correct SANs.
82pub trait AliasFeedback: Send + Sync {
83    /// Record that `hostname` should have `alias` as a SAN.
84    fn record_alias(&self, hostname: &str, alias: &str);
85}