rusty_cdk_core/stack/
builder.rs1use crate::stack::{Resource, Stack};
2use std::error::Error;
3use std::fmt::{Debug, Display, Formatter};
4
5#[derive(Debug)]
6pub enum StackBuilderError {
7 MissingPermissionsForRole(Vec<String>),
8}
9
10impl Display for StackBuilderError {
11 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
12 match self {
13 StackBuilderError::MissingPermissionsForRole(info) => {
14 let gathered_info = info.join(";");
15 f.write_fmt(format_args!("one or more roles seem to be missing permission to access services: `{}`?", gathered_info))
16 }
17 }
18 }
19}
20
21impl Error for StackBuilderError {}
22
23pub struct StackBuilder {
51 resources: Vec<Resource>,
52 tags: Vec<(String, String)>,
53}
54
55impl Default for StackBuilder {
56 fn default() -> Self {
57 Self::new()
58 }
59}
60
61impl StackBuilder {
62 pub fn new() -> Self {
63 Self { resources: vec![], tags: vec![] }
64 }
65
66 pub fn add_resource<T: Into<Resource>>(&mut self, resource: T) {
67 let resource = resource.into();
68 self.resources.push(resource);
69 }
70
71 pub fn add_tag<T: Into<String>>(mut self, key: T, value: T) -> Self {
72 self.tags.push((key.into(), value.into()));
73 self
74 }
75
76 pub fn build(self) -> Result<Stack, StackBuilderError> {
80 let metadata = self
81 .resources
82 .iter()
83 .map(|r| (r.get_id().to_string(), r.get_resource_id().to_string()))
84 .collect();
85
86 let roles_with_potentially_missing_services: Vec<_> = self.resources.iter().filter_map(|r| {
87 match r {
88 Resource::Role(r) => {
89 if !r.potentially_missing_services.is_empty() {
90 Some(format!("{}: {}", r.resource_id, r.potentially_missing_services.join(",")))
91 } else {
92 None
93 }
94 },
95 _ => None
96 }
97 }).collect();
98
99 if !roles_with_potentially_missing_services.is_empty() {
100 return Err(StackBuilderError::MissingPermissionsForRole(roles_with_potentially_missing_services))
101 }
102
103 let resources = self.resources.into_iter().map(|r| (r.get_resource_id().to_string(), r)).collect();
104 Ok(Stack {
105 to_replace: vec![],
106 tags: self.tags,
107 resources,
108 metadata,
109 })
110 }
111}