1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use serde::{Deserialize, Serialize};
use std::{
borrow::Cow,
fmt::{Display, Formatter, Write},
};
use super::{Action, ActionName};
#[derive(Debug, Serialize, Deserialize)]
pub struct Statement {
pub resources: Vec<ResourceName<'static>>,
pub actions: ActionNameList,
pub allowed: bool,
}
#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum Identifier<'a> {
Any,
Integer(u64),
String(Cow<'a, str>),
}
impl<'a> Identifier<'a> {
#[must_use]
pub fn to_owned(&self) -> Identifier<'static> {
match self {
Self::Any => Identifier::Any,
Self::Integer(value) => Identifier::Integer(*value),
Self::String(value) => Identifier::String(Cow::Owned(value.to_string())),
}
}
}
impl<'a> Display for Identifier<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Any => f.write_char('*'),
Self::Integer(integer) => integer.fmt(f),
Self::String(string) => string.fmt(f),
}
}
}
impl<'a> From<u64> for Identifier<'a> {
fn from(id: u64) -> Self {
Self::Integer(id)
}
}
impl<'a> From<&'a str> for Identifier<'a> {
fn from(id: &'a str) -> Self {
Self::String(Cow::Borrowed(id))
}
}
impl<'a> From<&'a String> for Identifier<'a> {
fn from(id: &'a String) -> Self {
Self::from(id.as_str())
}
}
impl<'a> From<String> for Identifier<'a> {
fn from(id: String) -> Self {
Self::String(Cow::Owned(id))
}
}
#[derive(Debug, Serialize, Deserialize)]
pub enum ActionNameList {
List(Vec<ActionName>),
All,
}
impl<T> From<T> for ActionNameList
where
T: Action,
{
fn from(action: T) -> Self {
Self::List(vec![action.name()])
}
}
impl From<ActionName> for ActionNameList {
fn from(name: ActionName) -> Self {
Self::List(vec![name])
}
}
impl From<Vec<ActionName>> for ActionNameList {
fn from(names: Vec<ActionName>) -> Self {
Self::List(names)
}
}
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct ResourceName<'a>(Vec<Identifier<'a>>);
impl<'a> ResourceName<'a> {
#[must_use]
pub fn to_owned(&self) -> ResourceName<'static> {
ResourceName(self.0.iter().map(Identifier::to_owned).collect())
}
}
impl<'a> Display for ResourceName<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
for (index, identifier) in self.0.iter().enumerate() {
if index > 0 {
f.write_char('.')?;
}
identifier.fmt(f)?;
}
Ok(())
}
}
impl<'a> ResourceName<'a> {
#[must_use]
pub fn any() -> Self {
Self::named(Identifier::Any)
}
#[must_use]
pub fn named<I: Into<Identifier<'a>>>(name: I) -> Self {
Self(vec![name.into()])
}
#[must_use]
pub fn and<I: Into<Identifier<'a>>>(mut self, name: I) -> Self {
self.0.push(name.into());
self
}
}
impl<'a> AsRef<[Identifier<'a>]> for ResourceName<'a> {
fn as_ref(&self) -> &[Identifier<'a>] {
&self.0
}
}
impl<'a> IntoIterator for ResourceName<'a> {
type Item = Identifier<'a>;
type IntoIter = std::vec::IntoIter<Identifier<'a>>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}