Skip to main content

four_iam/property/
statement.rs

1use serde::Serialize;
2
3use crate::property::{action::Action, effect::Effect, principal::Principal};
4
5#[derive(Debug, Clone, Serialize)]
6#[serde(rename_all = "PascalCase")]
7pub struct Statement {
8    effect: Effect,
9
10    #[serde(flatten)]
11    action: ActionOr,
12
13    #[serde(flatten)]
14    principal: PrincipalOr,
15}
16
17impl Statement {
18    pub fn allow() -> StatementBuilder1 {
19        StatementBuilder1 {
20            effect: Effect::Allow,
21        }
22    }
23
24    pub fn deny() -> StatementBuilder1 {
25        StatementBuilder1 {
26            effect: Effect::Deny,
27        }
28    }
29}
30
31pub struct StatementBuilder1 {
32    effect: Effect,
33}
34
35impl StatementBuilder1 {
36    pub fn action(self, action: Vec<Box<dyn Action>>) -> StatementBuilder2 {
37        StatementBuilder2 {
38            effect: self.effect,
39            action: ActionOr::Action(action),
40        }
41    }
42
43    pub fn not_action(self, action: Vec<Box<dyn Action>>) -> StatementBuilder2 {
44        StatementBuilder2 {
45            effect: self.effect,
46            action: ActionOr::NotAction(action),
47        }
48    }
49}
50
51pub struct StatementBuilder2 {
52    effect: Effect,
53    action: ActionOr,
54}
55
56impl StatementBuilder2 {
57    pub fn principal(self, principal: Principal) -> Statement {
58        Statement {
59            effect: self.effect,
60            action: self.action,
61            principal: PrincipalOr::Principal(principal),
62        }
63    }
64
65    pub fn no_principal(self, principal: Principal) -> Statement {
66        Statement {
67            effect: self.effect,
68            action: self.action,
69            principal: PrincipalOr::NotPrincipal(principal),
70        }
71    }
72}
73
74#[derive(Debug, Clone, Serialize)]
75pub enum ActionOr {
76    Action(Vec<Box<dyn Action>>),
77    NotAction(Vec<Box<dyn Action>>),
78}
79
80#[derive(Debug, Clone, Serialize)]
81pub enum PrincipalOr {
82    Principal(Principal),
83    NotPrincipal(Principal),
84}