atomr_persistence_aws/
config.rs1use std::env;
4
5#[derive(Debug, Clone)]
6pub struct DynamoConfig {
7 pub table_name: String,
8 pub endpoint_url: Option<String>,
10 pub region: Option<String>,
11 pub auto_create_table: bool,
12}
13
14impl DynamoConfig {
15 pub fn new(table_name: impl Into<String>) -> Self {
16 Self { table_name: table_name.into(), endpoint_url: None, region: None, auto_create_table: true }
17 }
18
19 pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
20 self.endpoint_url = Some(endpoint.into());
21 self
22 }
23
24 pub fn with_region(mut self, region: impl Into<String>) -> Self {
25 self.region = Some(region.into());
26 self
27 }
28
29 pub fn with_auto_create(mut self, create: bool) -> Self {
30 self.auto_create_table = create;
31 self
32 }
33
34 pub fn from_env() -> Self {
38 let table =
39 env::var("ATOMR_PERSISTENCE_DYNAMO_TABLE").unwrap_or_else(|_| "atomr_persistence".to_string());
40 let endpoint = env::var("ATOMR_PERSISTENCE_DYNAMO_ENDPOINT")
41 .or_else(|_| env::var("ATOMR_IT_DYNAMO_ENDPOINT"))
42 .ok();
43 let region = env::var("AWS_REGION").ok();
44 let mut cfg = Self::new(table);
45 if let Some(e) = endpoint {
46 cfg = cfg.with_endpoint(e);
47 }
48 if let Some(r) = region {
49 cfg = cfg.with_region(r);
50 }
51 cfg
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn builder_chain() {
61 let cfg = DynamoConfig::new("t")
62 .with_endpoint("http://localhost:8000")
63 .with_region("us-east-1")
64 .with_auto_create(false);
65 assert_eq!(cfg.table_name, "t");
66 assert_eq!(cfg.endpoint_url.as_deref(), Some("http://localhost:8000"));
67 assert!(!cfg.auto_create_table);
68 }
69}