1use std::{collections::HashMap, time::Duration};
11
12use reifydb_runtime::actor::{reply::Reply, system::ActorHandle};
13use reifydb_type::{
14 error::Diagnostic,
15 params::Params,
16 value::{frame::frame::Frame, identity::IdentityId},
17};
18
19use crate::metric::ExecutionMetrics;
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
23pub enum Operation {
24 Query,
25 Command,
26 Admin,
27 Subscribe,
28}
29
30pub type ServerHandle = ActorHandle<ServerMessage>;
32
33pub enum ServerMessage {
35 Query {
37 identity: IdentityId,
38 rql: String,
39 params: Params,
40 reply: Reply<ServerResponse>,
41 },
42 Command {
44 identity: IdentityId,
45 rql: String,
46 params: Params,
47 reply: Reply<ServerResponse>,
48 },
49 Admin {
51 identity: IdentityId,
52 rql: String,
53 params: Params,
54 reply: Reply<ServerResponse>,
55 },
56 Subscribe {
58 identity: IdentityId,
59 rql: String,
60 reply: Reply<ServerSubscribeResponse>,
61 },
62 Authenticate {
64 method: String,
65 credentials: HashMap<String, String>,
66 reply: Reply<ServerAuthResponse>,
67 },
68 Logout {
70 token: String,
71 reply: Reply<ServerLogoutResponse>,
72 },
73}
74
75pub enum ServerResponse {
77 Success {
79 frames: Vec<Frame>,
80 duration: Duration,
81 metrics: ExecutionMetrics,
82 },
83 EngineError {
85 diagnostic: Box<Diagnostic>,
86 rql: String,
87 },
88}
89
90pub enum ServerAuthResponse {
92 Authenticated {
94 identity: IdentityId,
95 token: String,
96 },
97 Challenge {
99 challenge_id: String,
100 payload: HashMap<String, String>,
101 },
102 Failed {
104 reason: String,
105 },
106 Error(String),
108}
109
110pub enum ServerLogoutResponse {
112 Ok,
114 InvalidToken,
116 Error(String),
118}
119
120pub enum ServerSubscribeResponse {
122 Subscribed {
124 frames: Vec<Frame>,
125 duration: Duration,
126 metrics: ExecutionMetrics,
127 },
128 EngineError {
130 diagnostic: Box<Diagnostic>,
131 rql: String,
132 },
133}
134
135pub fn build_server_message(
140 operation: Operation,
141 identity: IdentityId,
142 rql: String,
143 params: Params,
144 reply: Reply<ServerResponse>,
145) -> ServerMessage {
146 match operation {
147 Operation::Query => ServerMessage::Query {
148 identity,
149 rql,
150 params,
151 reply,
152 },
153 Operation::Command => ServerMessage::Command {
154 identity,
155 rql,
156 params,
157 reply,
158 },
159 Operation::Admin => ServerMessage::Admin {
160 identity,
161 rql,
162 params,
163 reply,
164 },
165 Operation::Subscribe => unreachable!("subscribe uses a different dispatch path"),
166 }
167}