rusty_cdk_core/shared/
mod.rs

1use std::fmt::{Display, Formatter};
2use std::ops::Deref;
3
4pub mod http;
5pub mod macros;
6
7/// `Id` is a unique identifier for a resource within a stack, chosen by the user (if it's a root resource), or by the library itself.
8/// E.g., when a user adds a bucket, he/she will pick the id of that bucket.
9/// But when we automatically generate additional resources for that bucket (a policy, for example), the library chooses those additional ids, often by adding a suffix.
10/// 
11/// These ids differ from 'resource ids', which are the names given to resources in the CloudFormation template, randomly generated during synth.
12///
13/// Ids make sure we do not replace existing resources when we're dealing with an existing stack.
14/// Because they are unique and should not change, we can link a chosen id with an existing resource id.
15/// 
16/// A user should not change an id if he/she does not want to replace an existing resource.
17/// This behavior is similar to that of the AWS CDK
18#[derive(Debug, Clone)]
19pub struct Id(pub String);
20
21impl Deref for Id {
22    type Target = str;
23
24    fn deref(&self) -> &Self::Target {
25        self.0.as_str()
26    }
27}
28
29impl Display for Id {
30    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
31        f.write_str(self.0.as_str())
32    }
33}
34
35impl Id {
36    pub fn generate_id(id: &Id, suffix: &str) -> Id {
37        Id(format!("{}{}", id.0, suffix))
38    }
39    
40    pub fn combine_ids(first: &Id, second: &Id) -> Id {
41        Id(format!("{}{}", first.0, second.0))
42    }    
43    
44    pub fn combine_with_resource_id(first: &Id, second: &str) -> Id {
45        Id(format!("{}{}", first.0, second))
46    }
47}