rusty_cdk_core/shared/
id.rs

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