reifydb_core/interface/catalog/
policy.rs1use serde::{Deserialize, Serialize};
5
6pub type PolicyId = u64;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
9pub enum DataOp {
10 From,
11 Insert,
12 Update,
13 Delete,
14}
15
16impl DataOp {
17 pub const ALL: &'static [DataOp] = &[DataOp::From, DataOp::Insert, DataOp::Update, DataOp::Delete];
18
19 pub fn as_str(&self) -> &'static str {
20 match self {
21 Self::From => "from",
22 Self::Insert => "insert",
23 Self::Update => "update",
24 Self::Delete => "delete",
25 }
26 }
27
28 pub fn parse(s: &str) -> Option<Self> {
29 match s {
30 "from" => Some(Self::From),
31 "insert" => Some(Self::Insert),
32 "update" => Some(Self::Update),
33 "delete" => Some(Self::Delete),
34 _ => None,
35 }
36 }
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
40pub enum CallableOp {
41 Call,
42}
43
44impl CallableOp {
45 pub const ALL: &'static [CallableOp] = &[CallableOp::Call];
46
47 pub fn as_str(&self) -> &'static str {
48 match self {
49 Self::Call => "call",
50 }
51 }
52
53 pub fn parse(s: &str) -> Option<Self> {
54 match s {
55 "call" => Some(Self::Call),
56 _ => None,
57 }
58 }
59}
60
61#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
62pub enum SessionOp {
63 Admin,
64 Command,
65 Query,
66 Subscription,
67}
68
69impl SessionOp {
70 pub const ALL: &'static [SessionOp] =
71 &[SessionOp::Admin, SessionOp::Command, SessionOp::Query, SessionOp::Subscription];
72
73 pub fn as_str(&self) -> &'static str {
74 match self {
75 Self::Admin => "admin",
76 Self::Command => "command",
77 Self::Query => "query",
78 Self::Subscription => "subscription",
79 }
80 }
81
82 pub fn parse(s: &str) -> Option<Self> {
83 match s {
84 "admin" => Some(Self::Admin),
85 "command" => Some(Self::Command),
86 "query" => Some(Self::Query),
87 "subscription" => Some(Self::Subscription),
88 _ => None,
89 }
90 }
91}
92
93#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
94pub enum PolicyTargetType {
95 Table,
96 Column,
97 Namespace,
98 Procedure,
99 Function,
100 Subscription,
101 Series,
102 Dictionary,
103 Session,
104 Feature,
105 View,
106 RingBuffer,
107 Queue,
108}
109
110impl PolicyTargetType {
111 pub fn as_str(&self) -> &'static str {
112 match self {
113 Self::Table => "table",
114 Self::Column => "column",
115 Self::Namespace => "namespace",
116 Self::Procedure => "procedure",
117 Self::Function => "function",
118 Self::Subscription => "subscription",
119 Self::Series => "series",
120 Self::Dictionary => "dictionary",
121 Self::Session => "session",
122 Self::Feature => "feature",
123 Self::View => "view",
124 Self::RingBuffer => "ringbuffer",
125 Self::Queue => "queue",
126 }
127 }
128
129 pub fn is_valid_operation(&self, op: &str) -> bool {
130 match self {
131 Self::Table
132 | Self::View
133 | Self::Series
134 | Self::RingBuffer
135 | Self::Dictionary
136 | Self::Column
137 | Self::Namespace => DataOp::parse(op).is_some(),
138 Self::Procedure | Self::Function => CallableOp::parse(op).is_some(),
139 Self::Session => SessionOp::parse(op).is_some(),
140 Self::Subscription | Self::Feature | Self::Queue => false,
141 }
142 }
143
144 pub fn valid_operation_names(&self) -> &'static [&'static str] {
145 match self {
146 Self::Table
147 | Self::View
148 | Self::Series
149 | Self::RingBuffer
150 | Self::Dictionary
151 | Self::Column
152 | Self::Namespace => &["from", "insert", "update", "delete"],
153 Self::Procedure | Self::Function => &["call"],
154 Self::Session => &["admin", "command", "query", "subscription"],
155 Self::Subscription | Self::Feature | Self::Queue => &[],
156 }
157 }
158}
159
160#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
161pub struct Policy {
162 pub id: PolicyId,
163 pub name: Option<String>,
164 pub target_type: PolicyTargetType,
165 pub target_namespace: Option<String>,
166 pub target_object: Option<String>,
167 pub enabled: bool,
168}
169
170#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
171pub struct PolicyOperation {
172 pub policy_id: PolicyId,
173 pub operation: String,
174 pub body_source: String,
175}
176
177pub struct PolicyToCreate {
178 pub name: Option<String>,
179 pub target_type: PolicyTargetType,
180 pub target_namespace: Option<String>,
181 pub target_object: Option<String>,
182 pub operations: Vec<PolicyOpToCreate>,
183}
184
185pub struct PolicyOpToCreate {
186 pub operation: String,
187 pub body_source: String,
188}