Skip to main content

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#[macro_export]
20macro_rules! internal_ref_struct_methods {
21    () => {
22        #[allow(dead_code)]
23        pub(crate) fn get_resource_id(&self) -> &str {
24            self.resource_id.as_str()
25        }
26    
27        #[allow(dead_code)]
28        pub fn get_ref(&self) -> Value {
29            if let Some(val) = &self.ref_name {
30                Value::String(val.to_string())
31            } else {
32                $crate::intrinsic::get_ref(self.get_resource_id())
33            }
34        }
35        
36        #[allow(dead_code)]
37        pub fn get_arn(&self) -> Value {
38            if let Some(val) = &self.arn_value {
39                Value::String(val.to_string())
40            } else {
41                $crate::intrinsic::get_arn(self.get_resource_id())    
42            }
43        }
44        
45        #[allow(dead_code)]
46        pub fn get_att(&self, id: &str) -> Value {
47            if self.ref_name.is_some() && self.arn_value.is_some() {
48                unimplemented!("get att is not supported for an imported RoleRef")
49            } else {
50                $crate::intrinsic::get_att(self.get_resource_id(), id)
51            }
52        }
53    };
54}
55
56/// Generated a ref struct, which is used to reference a given resource when you need it as a dependency of some other resource.
57/// To allow the other resources to depend on this one, the ref struct has methods for retrieving the Ref, ARN, and other attributes.
58#[macro_export]
59macro_rules! ref_struct {
60    ($name:ident) => {
61        #[derive(Debug,Clone)]
62        pub struct $name {
63            resource_id: String,
64            ref_name: Option<String>,
65            arn_value: Option<String>,
66        }
67        
68        impl $name {
69            #[allow(dead_code)]
70            pub(crate) fn internal_new(resource_id: String) -> Self {
71                Self {
72                    resource_id,
73                    ref_name: None,
74                    arn_value: None,
75                }
76            }
77            
78            #[allow(dead_code)]
79            pub fn new(resource_id: &str, ref_name: &str, arn_value: &str) -> Self {
80                Self {
81                    resource_id: resource_id.to_string(),
82                    ref_name: Some(ref_name.to_string()),
83                    arn_value: Some(arn_value.to_string()),
84                }
85            }
86            
87            crate::internal_ref_struct_methods!();
88        }
89    };
90}
91
92/// Generated a ref struct, which is used to reference a given resource when you need it as a dependency of some other resource.
93/// To allow the other resources to depend on this one, the ref struct has methods for retrieving the Ref, ARN, and other attributes.
94/// 
95/// 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.
96/// 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.
97#[macro_export]
98macro_rules! ref_struct_with_id_methods {
99    ($name:ident) => {
100        pub struct $name {
101            id: Id,
102            resource_id: String,
103            ref_name: Option<String>,
104            arn_value: Option<String>,
105        }
106        
107        impl $name {
108            #[allow(dead_code)]
109            pub(crate) fn internal_new(id: Id, resource_id: String) -> Self {
110                Self {
111                    id,
112                    resource_id,
113                    ref_name: None,
114                    arn_value: None,
115                }
116            }
117            
118            #[allow(dead_code)]
119            pub fn new(id: &str, resource_id: &str, ref_name: &str, arn_value: &str) -> Self {
120                Self {
121                    id: Id(id.to_string()),
122                    resource_id: resource_id.to_string(),
123                    ref_name: Some(ref_name.to_string()),
124                    arn_value: Some(arn_value.to_string()),
125                }
126            }
127
128            #[allow(dead_code)]
129            pub(crate) fn get_id(&self) -> &Id {
130                &self.id
131            }
132        
133            crate::internal_ref_struct_methods!();
134        }
135    };
136}
137
138/// Pass in the trait name, followed by names for the state structs that should implement it
139/// Values separated by commas
140#[macro_export]
141macro_rules! type_state {
142    ($state_trait:ident,$($structs:ident,)*) => {
143        pub trait $state_trait {}
144        $(pub struct $structs {})*
145        $(impl $state_trait for $structs {})*
146    };
147}