recast_lang/ast/action/
mod.rs

1//! Actions available in the AST.
2
3use crate::ast::protocol::RapidRecastProtocolType;
4use serde::{Deserialize, Serialize};
5use std::borrow::Cow;
6use std::collections::BTreeMap;
7
8/// Actions that one can make on any event
9#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
10pub enum RapidRecastAction<'a> {
11    /// An action that is related to authentication
12    AuthBasedAction(AuthBasedAction<'a>),
13    /// An action that is based on logic
14    LogicBasedAction(LogicBasedAction<'a>),
15}
16
17/// Actions that resolve to a logic related change
18#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
19pub enum LogicBasedAction<'a> {
20    /// If-condition-style blocks
21    ConditionBlock {
22        /// The condition type associated with the if block
23        condition: ConditionStatement<'a>,
24        /// The code that is executed if the statement is true
25        if_true: Box<RapidRecastAction<'a>>,
26        /// THe code that is executed if the statement is false
27        if_false: Option<Box<RapidRecastAction<'a>>>,
28    },
29}
30
31/// A value that can be used in statements
32#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
33pub enum RecastValue<'a> {
34    /// A pre-declared variable reference
35    Param(Cow<'a, str>),
36    /// A string literal
37    String(Cow<'a, str>),
38    /// A number literal
39    Number(f64),
40}
41
42/// A condition statement
43#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
44pub enum ConditionStatement<'a> {
45    /// Left == Right
46    Equals(RecastValue<'a>, RecastValue<'a>),
47    /// Left != Right
48    NotEquals(RecastValue<'a>, RecastValue<'a>),
49    /// Left > Right
50    GreaterThan(RecastValue<'a>, RecastValue<'a>),
51    /// Left < Right
52    LessThan(RecastValue<'a>, RecastValue<'a>),
53    /// Left >= Right
54    GreaterThanOrEqual(RecastValue<'a>, RecastValue<'a>),
55    /// Left <= Right
56    LessThanOrEqual(RecastValue<'a>, RecastValue<'a>),
57    /// Left && Right
58    And(RecastValue<'a>, RecastValue<'a>),
59    /// Left || Right
60    Or(RecastValue<'a>, RecastValue<'a>),
61    /// !Value
62    Not(RecastValue<'a>),
63}
64
65/// Actions that resolve to an authentication related change
66#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
67pub enum AuthBasedAction<'a> {
68    /// Creates a new user with the specified password
69    CreateUser {
70        /// The user or role
71        subject: UserIdentifier<'a>,
72        /// The password for the user
73        password: Option<Cow<'a, str>>,
74    },
75    /// Adds non-system metadata that will be accessible to the system for the user
76    AddMetadataToUser {
77        /// The user or role
78        subject: UserIdentifier<'a>,
79        /// Metadata to be added to a subject (user or role)
80        metadata: BTreeMap<Cow<'a, str>, Cow<'a, str>>,
81    },
82    /// Adds a policy to the system
83    GrantPermissions {
84        /// The user or role
85        subject: UserIdentifier<'a>,
86        /// Policies to be added to a subject (user or role)
87        policy: Vec<RapidRecastRbacPolicy<'a>>,
88    },
89}
90
91/// Convenience struct user data for actions
92#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
93pub struct UserIdentifier<'a> {
94    /// The namespace the user belongs to
95    pub namespace: Cow<'a, str>,
96    /// The username within that namespace
97    pub username: Cow<'a, str>,
98}
99
100/// A way of declaring a RapidRecast RBAC policy
101#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
102pub struct RapidRecastRbacPolicy<'a> {
103    /// The subject of the policy
104    pub subject: RapidRecastRbacSubject<'a>,
105    /// The object of the policy
106    pub object: RapidRecastRbacObject<'a>,
107    /// The action of the policy
108    pub action: RapidRecastRbacAction,
109}
110
111/// The subjects available in RapidRecast RBAC
112#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
113pub enum RapidRecastRbacSubject<'a> {
114    /// The system administrator
115    Admin,
116    /// An anonymous user
117    Anon,
118    /// A specific user on the given namespace
119    /// (namespace, user)
120    UserOrRole(Cow<'a, str>, Cow<'a, str>),
121}
122
123/// The objects available in RapidRecast RBAC
124#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
125pub enum RapidRecastRbacObject<'a> {
126    /// A Namespace within RapidRecast
127    Namespace(NamespaceObject<'a>),
128    /// A Protocol provided by within RapidRecast
129    Protocol(RapidRecastProtocolType),
130    /// A Topic within RapidRecast
131    Topic(TopicObject<'a>),
132    /// A client available to RapidRecast
133    Client(ClientObject),
134    /// A RapidRecast Definition Language object
135    Model(ModelObject),
136}
137
138/// A Namespace within RapidRecast
139#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
140pub enum NamespaceObject<'a> {
141    /// A namespace that exists
142    ExistingNamespace(Cow<'a, str>),
143    /// A namespace that does not exist
144    NonExistingNamespace(Cow<'a, str>),
145}
146
147/// A topic within RapidRecast
148#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
149pub enum TopicObject<'a> {
150    /// A topic that does not exist
151    NonExistingTopic(Cow<'a, str>),
152    /// A topic that exists
153    NamespaceTopic(Cow<'a, str>, Cow<'a, str>),
154}
155
156/// The clients available to the system. Cached or otherwise to-be created.
157#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
158pub enum ClientObject {
159    /// An http 1 client
160    Http1,
161    /// An http 2 client
162    Http2,
163    /// An http 3 client
164    Http3,
165    /// A kafka client
166    Kafka,
167    /// An mqtt client
168    Mqtt,
169}
170
171/// A reference to a RapidRecast Definition Language object
172#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
173pub enum ModelObject {}
174
175/// The actions available in RapidRecast RBAC
176#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
177pub enum RapidRecastRbacAction {
178    /// Allows the creation of a resource
179    Create,
180    /// Allows reading from the resource
181    Read,
182    /// Allows updating the resource (metadata)
183    Update,
184    /// Allows deleting the resource
185    Delete,
186    /// Allows writing to the resource
187    Write,
188    /// Allows listing the resource (reading metadata)
189    List,
190    /// Allows renaming the resource
191    Rename,
192}