aurelia 0.2.0

Embeddable service mesh for Rust distributed applications.
Documentation
// This file is part of the Aurelia workspace.
// SPDX-FileCopyrightText: 2026 Zivatar Limited
// SPDX-License-Identifier: Apache-2.0

#![allow(unused_imports, dead_code, clippy::module_inception)]
#![warn(missing_docs)]

//! Shared Aurelia domain data and boundary contracts.

use std::fmt;
use std::net::SocketAddr;
use std::path::PathBuf;

use crate::ids::{AureliaError, TabernaId};

/// Network address of a domus peer, abstracting over the supported transports.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum DomusAddr {
    /// TCP socket address (used with mTLS).
    Tcp(SocketAddr),
    /// Unix domain socket path (used with PKCS#8 certificate-backed authentication).
    Socket(PathBuf),
}

/// Tag identifying which transport a [`DomusAddr`] uses.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TransportKind {
    /// TCP transport with mTLS authentication.
    Tcp,
    /// Unix domain socket transport with PKCS#8 certificate-backed authentication.
    Socket,
}

impl DomusAddr {
    /// Returns the transport kind backing this address.
    pub fn kind(&self) -> TransportKind {
        match self {
            DomusAddr::Tcp(_) => TransportKind::Tcp,
            DomusAddr::Socket(_) => TransportKind::Socket,
        }
    }

    /// Returns the underlying [`SocketAddr`] if this is a TCP address.
    pub fn as_tcp(&self) -> Option<SocketAddr> {
        match self {
            DomusAddr::Tcp(addr) => Some(*addr),
            _ => None,
        }
    }
}

impl fmt::Display for DomusAddr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DomusAddr::Tcp(addr) => write!(f, "tcp://{addr}"),
            DomusAddr::Socket(path) => write!(f, "unix://{}", path.to_string_lossy()),
        }
    }
}

impl fmt::Display for TransportKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TransportKind::Tcp => write!(f, "tcp"),
            TransportKind::Socket => write!(f, "socket"),
        }
    }
}

/// Application-supplied resolver that maps a target [`TabernaId`] to the
/// [`DomusAddr`] of the peer hosting it. Aurelia calls this on every send
/// so applications can implement service discovery as they see fit.
#[async_trait::async_trait]
pub trait RouteResolver: Send + Sync {
    /// Resolves `taberna_id` to the [`DomusAddr`] of the peer that hosts it.
    async fn resolve(&self, taberna_id: TabernaId) -> Result<DomusAddr, AureliaError>;
}