rusty_cdk_core/shared/
macros.rs

1/// Generated some methods that all resources have by passing in the resource name
2#[macro_export]
3macro_rules! dto_methods {
4    ($name:ident) => {
5        impl $name {
6            #[allow(dead_code)]
7            pub fn get_id(&self) -> &Id {
8                &self.id
9            }
10        
11            #[allow(dead_code)]
12            pub fn get_resource_id(&self) -> &str {
13                self.resource_id.as_str()
14            }
15        }
16    };
17}
18
19/// Generated a ref struct, which is used to reference a given resource when you need it as a dependency of some other resource.
20/// To allow the other resources to depend on this one, the ref struct has methods for retrieving the Ref, ARN, and other attributes.
21#[macro_export]
22macro_rules! ref_struct {
23    ($name:ident) => {
24        #[derive(Debug,Clone)]
25        pub struct $name {
26            resource_id: String,
27        }
28        
29        impl $name {
30            #[allow(dead_code)]
31            pub fn new(resource_id: String) -> Self {
32                Self {
33                    resource_id,
34                }
35            }
36        
37            #[allow(dead_code)]
38            pub fn get_resource_id(&self) -> &str {
39                self.resource_id.as_str()
40            }
41        
42            #[allow(dead_code)]
43            pub fn get_ref(&self) -> Value {
44                $crate::intrinsic::get_ref(self.get_resource_id())
45            }
46            
47            #[allow(dead_code)]
48            pub fn get_arn(&self) -> Value {
49                $crate::intrinsic::get_arn(self.get_resource_id())
50            }
51            
52            #[allow(dead_code)]
53            pub fn get_att(&self, id: &str) -> Value {
54                $crate::intrinsic::get_att(self.get_resource_id(), id)
55            }
56        }
57    };
58}
59
60/// Generated a ref struct, which is used to reference a given resource when you need it as a dependency of some other resource.
61/// To allow the other resources to depend on this one, the ref struct has methods for retrieving the Ref, ARN, and other attributes.
62/// 
63/// This macro also generates a method to retrieve the `id` field of the original resource, which is sometimes needed when generating custom ids based on a pre-existing id.
64/// For example, SNS subscriptions have Lambdas as a subscription destination. SNS generates an additional resource for the subscription, with an id based on the Lambda id.
65#[macro_export]
66macro_rules! ref_struct_with_id_methods {
67    ($name:ident) => {
68        pub struct $name {
69            id: Id,
70            resource_id: String,
71        }
72        
73        impl $name {
74            #[allow(dead_code)]
75            pub fn new(id: Id, resource_id: String) -> Self {
76                Self {
77                    id,
78                    resource_id,
79                }
80            }
81
82            #[allow(dead_code)]
83            pub fn get_id(&self) -> &Id {
84                &self.id
85            }
86        
87            #[allow(dead_code)]
88            pub fn get_resource_id(&self) -> &str {
89                self.resource_id.as_str()
90            }
91        
92            #[allow(dead_code)]
93            pub fn get_ref(&self) -> Value {
94                $crate::intrinsic::get_ref(self.get_resource_id())
95            }
96            
97            #[allow(dead_code)]
98            pub fn get_arn(&self) -> Value {
99                $crate::intrinsic::get_arn(self.get_resource_id())
100            }
101            
102            #[allow(dead_code)]
103            pub fn get_att(&self, id: &str) -> Value {
104                $crate::intrinsic::get_att(self.get_resource_id(), id)
105            }
106        }
107    };
108}
109
110/// Pass in the trait name, followed by names for the state structs that should implement it
111/// Values separated by commas
112#[macro_export]
113macro_rules! type_state {
114    ($state_trait:ident,$($structs:ident,)*) => {
115        pub trait $state_trait {}
116        $(pub struct $structs {})*
117        $(impl $state_trait for $structs {})*
118    };
119}