Skip to main content

amaters_net/
lib.rs

1//! Network layer for AmateRS (Musubi - The Knot)
2//!
3//! This crate provides the gRPC-based networking layer for AmateRS,
4//! implementing secure communication over QUIC with mTLS.
5//!
6//! # Features
7//!
8//! - gRPC service for AQL queries
9//! - Request/response handling with streaming support
10//! - Error handling and retry strategies
11//! - Connection state management
12//!
13//! # Architecture
14//!
15//! The networking layer consists of:
16//! - Protocol definitions (.proto files)
17//! - Server implementation (gRPC service)
18//! - Client implementation (connection management)
19//! - Error types and conversions
20//!
21//! # Example
22//!
23//! ```rust,ignore
24//! use amaters_net::client::AqlClient;
25//! use amaters_core::{Key, CipherBlob};
26//!
27//! #[tokio::main]
28//! async fn main() -> anyhow::Result<()> {
29//!     let client = AqlClient::connect("http://localhost:50051").await?;
30//!
31//!     let key = Key::from_str("my_key");
32//!     let value = CipherBlob::new(vec![1, 2, 3]);
33//!
34//!     client.set("my_collection", key, value).await?;
35//!
36//!     Ok(())
37//! }
38//! ```
39//!
40//! ## Network Architecture
41//!
42//! ```text
43//!  Client → gRPC/tonic → AqlService (GrpcService)
44//!                              │
45//!                       LoadBalancer ──→ [EndpointPool]
46//!                              │
47//!                       RateLimiter / CircuitBreaker
48//!                              │
49//!                       QUIC/mTLS transport
50//! ```
51
52#![allow(dead_code)]
53#![allow(clippy::type_complexity)]
54#![allow(clippy::too_many_arguments)]
55
56pub mod auth;
57pub mod balancer;
58pub mod circuit_breaker;
59pub mod circuit_cache;
60pub mod client;
61pub mod config;
62pub mod convert;
63pub mod error;
64pub mod grpc_service;
65pub mod logging_layer;
66pub mod metrics_layer;
67#[cfg(feature = "telemetry")]
68pub mod otel_propagator;
69pub mod pool;
70pub mod rate_limiter;
71pub mod server;
72pub mod server_admin;
73pub mod server_builder;
74pub mod server_types;
75pub mod tracing_middleware;
76
77// mTLS module (feature-gated)
78#[cfg(feature = "mtls")]
79pub mod mtls;
80#[cfg(feature = "mtls")]
81pub mod ocsp;
82#[cfg(feature = "mtls")]
83pub mod tls;
84#[cfg(feature = "mtls")]
85pub mod tls_acceptor;
86#[cfg(feature = "mtls")]
87pub mod tls_crypto;
88
89// Include the generated protocol buffer code
90pub mod proto {
91    pub mod types {
92        #![allow(clippy::all)]
93        #![allow(warnings)]
94        include!(concat!(env!("OUT_DIR"), "/amaters.types.rs"));
95    }
96    pub mod query {
97        #![allow(clippy::all)]
98        #![allow(warnings)]
99        include!(concat!(env!("OUT_DIR"), "/amaters.query.rs"));
100    }
101    pub mod errors {
102        #![allow(clippy::all)]
103        #![allow(warnings)]
104        include!(concat!(env!("OUT_DIR"), "/amaters.errors.rs"));
105    }
106    pub mod aql {
107        #![allow(clippy::all)]
108        #![allow(warnings)]
109        include!(concat!(env!("OUT_DIR"), "/amaters.aql.rs"));
110    }
111}
112
113// Re-exports for convenience
114pub use circuit_cache::{CircuitCache, CircuitCacheConfig, CircuitCacheKey, CircuitCacheStats};
115pub use config::{
116    AuthSection, LogVerbosityWire, LoggingSection, MetricsSection, NetConfig, NetSection,
117    RateLimitSection, TlsSection,
118};
119pub use error::{NetError, NetResult};
120pub use logging_layer::{LogVerbosity, LoggingLayer, LoggingService};
121pub use metrics_layer::{NetMetrics, spawn_metrics_server};
122pub use server::{AqlServerBuilder, AqlServiceImpl};
123pub use server_types::StreamConfig;
124
125// TLS acceptor re-exports (feature-gated)
126#[cfg(feature = "mtls")]
127pub use tls_acceptor::{LiveTlsAcceptor, TlsCredsRef, build_rustls_config};
128
129// OTel propagator re-exports (feature-gated)
130#[cfg(feature = "telemetry")]
131pub use otel_propagator::{TraceparentExtractor, inject_trace_context};
132
133// mTLS re-exports
134#[cfg(feature = "mtls")]
135pub use mtls::{
136    CrlRevocationChecker, HandshakeResult, MtlsClient, MtlsClientVerifier, MtlsConfigBuilder,
137    MtlsServer, MtlsServerVerifier, OcspRevocationChecker, Principal, PrincipalMapper,
138    RevocationChecker, RevocationStatus,
139};
140#[cfg(feature = "mtls")]
141pub use tls::{
142    CertificateFormat, CertificateInfo, CertificateLoader, CertificateStore,
143    HotReloadableCertificates, PrivateKeyLoader, PrivateKeyType, SelfSignedGenerator,
144};
145
146// QUIC transport (feature-gated)
147#[cfg(feature = "quic")]
148pub mod quic_transport;
149#[cfg(feature = "quic")]
150pub use quic_transport::{QuicClient, QuicClientConfig, QuicServer, QuicServerConfig};
151
152/// Library version
153pub const VERSION: &str = env!("CARGO_PKG_VERSION");
154
155/// Protocol version
156pub const PROTOCOL_VERSION: (u32, u32, u32) = (0, 2, 0);
157
158#[cfg(test)]
159mod tests {
160    use super::*;
161
162    #[test]
163    fn test_version() {
164        // VERSION is a compile-time constant from CARGO_PKG_VERSION
165        // It should be in semver format (e.g., "0.1.0")
166        assert!(VERSION.contains('.'), "VERSION should be semver format");
167    }
168
169    #[test]
170    fn test_protocol_version() {
171        assert_eq!(PROTOCOL_VERSION, (0, 2, 0));
172    }
173}