Skip to main content

rustack_logs_http/
dispatch.rs

1//! CloudWatch Logs handler trait and operation dispatch.
2
3use std::{future::Future, pin::Pin};
4
5use bytes::Bytes;
6use rustack_logs_model::{error::LogsError, operations::LogsOperation};
7
8use crate::body::LogsResponseBody;
9
10/// Trait that the CloudWatch Logs business logic provider must implement.
11///
12/// The handler receives a parsed operation enum and the raw JSON body bytes,
13/// and returns a complete HTTP response. This trait serves as the boundary
14/// between the HTTP transport layer and the business logic layer.
15pub trait LogsHandler: Send + Sync + 'static {
16    /// Handle a CloudWatch Logs operation and produce an HTTP response.
17    fn handle_operation(
18        &self,
19        op: LogsOperation,
20        body: Bytes,
21    ) -> Pin<Box<dyn Future<Output = Result<http::Response<LogsResponseBody>, LogsError>> + Send>>;
22}
23
24/// Dispatch a CloudWatch Logs operation to the handler.
25pub async fn dispatch_operation<H: LogsHandler>(
26    handler: &H,
27    op: LogsOperation,
28    body: Bytes,
29) -> Result<http::Response<LogsResponseBody>, LogsError> {
30    tracing::debug!(operation = %op, "dispatching CloudWatch Logs operation");
31    handler.handle_operation(op, body).await
32}
33
34/// Default handler that returns an error for all operations.
35#[derive(Debug, Clone, Default)]
36pub struct NotImplementedHandler;
37
38impl LogsHandler for NotImplementedHandler {
39    fn handle_operation(
40        &self,
41        op: LogsOperation,
42        _body: Bytes,
43    ) -> Pin<Box<dyn Future<Output = Result<http::Response<LogsResponseBody>, LogsError>> + Send>>
44    {
45        Box::pin(async move { Err(LogsError::not_implemented(op.as_str())) })
46    }
47}