Skip to main content

reifydb_core/interface/catalog/
policy.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use 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}
108
109impl PolicyTargetType {
110	pub fn as_str(&self) -> &'static str {
111		match self {
112			Self::Table => "table",
113			Self::Column => "column",
114			Self::Namespace => "namespace",
115			Self::Procedure => "procedure",
116			Self::Function => "function",
117			Self::Subscription => "subscription",
118			Self::Series => "series",
119			Self::Dictionary => "dictionary",
120			Self::Session => "session",
121			Self::Feature => "feature",
122			Self::View => "view",
123			Self::RingBuffer => "ringbuffer",
124		}
125	}
126
127	pub fn is_valid_operation(&self, op: &str) -> bool {
128		match self {
129			Self::Table
130			| Self::View
131			| Self::Series
132			| Self::RingBuffer
133			| Self::Dictionary
134			| Self::Column
135			| Self::Namespace => DataOp::parse(op).is_some(),
136			Self::Procedure | Self::Function => CallableOp::parse(op).is_some(),
137			Self::Session => SessionOp::parse(op).is_some(),
138			Self::Subscription | Self::Feature => false,
139		}
140	}
141
142	pub fn valid_operation_names(&self) -> &'static [&'static str] {
143		match self {
144			Self::Table
145			| Self::View
146			| Self::Series
147			| Self::RingBuffer
148			| Self::Dictionary
149			| Self::Column
150			| Self::Namespace => &["from", "insert", "update", "delete"],
151			Self::Procedure | Self::Function => &["call"],
152			Self::Session => &["admin", "command", "query", "subscription"],
153			Self::Subscription | Self::Feature => &[],
154		}
155	}
156}
157
158#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
159pub struct Policy {
160	pub id: PolicyId,
161	pub name: Option<String>,
162	pub target_type: PolicyTargetType,
163	pub target_namespace: Option<String>,
164	pub target_shape: Option<String>,
165	pub enabled: bool,
166}
167
168#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
169pub struct PolicyOperation {
170	pub policy_id: PolicyId,
171	pub operation: String,
172	pub body_source: String,
173}
174
175pub struct PolicyToCreate {
176	pub name: Option<String>,
177	pub target_type: PolicyTargetType,
178	pub target_namespace: Option<String>,
179	pub target_shape: Option<String>,
180	pub operations: Vec<PolicyOpToCreate>,
181}
182
183pub struct PolicyOpToCreate {
184	pub operation: String,
185	pub body_source: String,
186}