trace2e_core/transport/nop.rs
1//! # No-Op Transport Implementation
2//!
3//! This module provides a stub transport implementation that handles M2M requests
4//! without performing any actual network communication. It's designed for scenarios
5//! where distributed functionality is disabled or not needed, such as single-node
6//! deployments or testing isolated middleware behavior.
7//!
8//! ## Behavior
9//!
10//! The no-op transport responds to all M2M requests with sensible default values:
11//!
12//! - **Destination Compliance**: Returns default policy (public, integrity 0, not deleted, consent given)
13//! - **Source Compliance**: Returns empty policy map (no source policies)
14//! - **Update Provenance**: Acknowledges the request without action
15//!
16//! ## Use Cases
17//!
18//! - Single-node deployments where distributed features are unnecessary
19//! - Testing middleware logic in isolation without network dependencies
20//! - Fallback transport when network communication is disabled
21//! - Development environments where distributed setup is not feasible
22
23use std::{collections::HashMap, future::Future, pin::Pin, task::Poll};
24
25use tower::Service;
26
27use crate::traceability::{
28 api::{M2mRequest, M2mResponse},
29 core::compliance::Policy,
30 error::TraceabilityError,
31};
32
33/// No-operation transport service for M2M communication.
34///
35/// `M2mNop` provides a stub implementation of the M2M transport interface
36/// that responds to all requests with default values without performing
37/// any actual network communication or distributed coordination.
38///
39/// ## Response Behavior
40///
41/// - **GetDestinationCompliance**: Always returns a default policy
42/// - **GetSourceCompliance**: Always returns an empty policy map
43/// - **UpdateProvenance**: Always acknowledges without action
44///
45/// This transport is useful for single-node deployments or testing
46/// scenarios where distributed functionality is not required.
47#[derive(Clone, Default)]
48pub struct M2mNop;
49
50/// Implementation of the M2M service interface for the no-op transport.
51///
52/// This implementation handles all M2M request types by returning appropriate
53/// default responses without performing any actual distributed operations.
54/// All requests complete immediately and successfully.
55impl Service<M2mRequest> for M2mNop {
56 type Response = M2mResponse;
57 type Error = TraceabilityError;
58 type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
59
60 /// Always reports ready to handle requests immediately.
61 fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
62 Poll::Ready(Ok(()))
63 }
64
65 /// Handles M2M requests by returning appropriate default responses.
66 ///
67 /// # Request Handling
68 ///
69 /// - **GetDestinationCompliance**: Returns a default policy with public
70 /// confidentiality, zero integrity, not deleted, and consent given
71 /// - **GetSourceCompliance**: Returns an empty policy map indicating
72 /// no source policies are available
73 /// - **UpdateProvenance**: Acknowledges the request without performing
74 /// any provenance updates
75 fn call(&mut self, request: M2mRequest) -> Self::Future {
76 Box::pin(async move {
77 Ok(match request {
78 M2mRequest::GetDestinationCompliance { .. } => {
79 M2mResponse::DestinationCompliance(Policy::default())
80 }
81 M2mRequest::GetSourceCompliance { .. } => {
82 M2mResponse::SourceCompliance(HashMap::new())
83 }
84 M2mRequest::UpdateProvenance { .. } => M2mResponse::Ack,
85 })
86 })
87 }
88}