Skip to main content

adapter_aws/
common.rs

1use async_trait::async_trait;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use thiserror::Error;
5
6pub type Result<T> = std::result::Result<T, AdapterError>;
7
8#[derive(Error, Debug)]
9pub enum AdapterError {
10    #[error("AWS SQS error: {0}")]
11    AwsSqs(String),
12
13    #[error("AWS EventBridge error: {0}")]
14    AwsEventBridge(String),
15
16    #[error("Queue not found: {0}")]
17    QueueNotFound(String),
18
19    #[error("Serialization error: {0}")]
20    Serialization(#[from] serde_json::Error),
21
22    #[error("Invalid message format: {0}")]
23    InvalidMessage(String),
24
25    #[error("Configuration error: {0}")]
26    Configuration(String),
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct Message {
31    pub topic: String,
32    pub payload: serde_json::Value,
33    pub timestamp: String,
34    pub metadata: HashMap<String, String>,
35}
36
37impl Message {
38    pub fn new(topic: impl Into<String>, payload: serde_json::Value) -> Self {
39        use std::time::SystemTime;
40        Self {
41            topic: topic.into(),
42            payload,
43            timestamp: SystemTime::now()
44                .duration_since(SystemTime::UNIX_EPOCH)
45                .unwrap()
46                .as_secs()
47                .to_string(),
48            metadata: HashMap::new(),
49        }
50    }
51
52    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
53        self.metadata.insert(key.into(), value.into());
54        self
55    }
56}
57
58#[async_trait]
59pub trait MessageHandler: Send + Sync {
60    async fn handle(&self, message: Message) -> Result<()>;
61}
62
63#[derive(Debug, Clone)]
64pub struct AwsConfig {
65    pub region: String,
66    pub queue_prefix: Option<String>, // For SQS
67    pub event_bus_name: Option<String>, // For EventBridge (default: "default")
68    pub source: Option<String>, // For EventBridge (default: "rohas")
69    pub visibility_timeout_seconds: Option<i32>, // For SQS
70    pub message_retention_seconds: Option<i32>, // For SQS
71    pub receive_wait_time_seconds: Option<i32>, // For SQS (long polling)
72}
73
74impl Default for AwsConfig {
75    fn default() -> Self {
76        Self {
77            region: "us-east-1".to_string(),
78            queue_prefix: Some("rohas-".to_string()),
79            event_bus_name: None, // Use default event bus
80            source: Some("rohas".to_string()),
81            visibility_timeout_seconds: Some(30),
82            message_retention_seconds: Some(345600), // 4 days
83            receive_wait_time_seconds: Some(20), // Long polling
84        }
85    }
86}
87