1use std::{collections::HashMap, time::Duration};
5
6use reifydb_runtime::actor::{reply::Reply, system::ActorHandle};
7use reifydb_type::{
8 error::Diagnostic,
9 params::Params,
10 value::{frame::frame::Frame, identity::IdentityId},
11};
12
13use crate::metric::ExecutionMetrics;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub enum Operation {
18 Query,
19 Command,
20 Admin,
21 Subscribe,
22}
23
24pub type ServerHandle = ActorHandle<ServerMessage>;
26
27pub enum ServerMessage {
29 Query {
31 identity: IdentityId,
32 rql: String,
33 params: Params,
34 reply: Reply<ServerResponse>,
35 },
36 Command {
38 identity: IdentityId,
39 rql: String,
40 params: Params,
41 reply: Reply<ServerResponse>,
42 },
43 Admin {
45 identity: IdentityId,
46 rql: String,
47 params: Params,
48 reply: Reply<ServerResponse>,
49 },
50 Subscribe {
52 identity: IdentityId,
53 rql: String,
54 reply: Reply<ServerSubscribeResponse>,
55 },
56 Authenticate {
58 method: String,
59 credentials: HashMap<String, String>,
60 reply: Reply<ServerAuthResponse>,
61 },
62 Logout {
64 token: String,
65 reply: Reply<ServerLogoutResponse>,
66 },
67}
68
69pub enum ServerResponse {
71 Success {
73 frames: Vec<Frame>,
74 duration: Duration,
75 metrics: ExecutionMetrics,
76 },
77 EngineError {
79 diagnostic: Box<Diagnostic>,
80 rql: String,
81 },
82}
83
84pub enum ServerAuthResponse {
86 Authenticated {
88 identity: IdentityId,
89 token: String,
90 },
91 Challenge {
93 challenge_id: String,
94 payload: HashMap<String, String>,
95 },
96 Failed {
98 reason: String,
99 },
100 Error(String),
102}
103
104pub enum ServerLogoutResponse {
106 Ok,
108 InvalidToken,
110 Error(String),
112}
113
114pub enum ServerSubscribeResponse {
116 Subscribed {
118 frames: Vec<Frame>,
119 duration: Duration,
120 metrics: ExecutionMetrics,
121 },
122 EngineError {
124 diagnostic: Box<Diagnostic>,
125 rql: String,
126 },
127}
128
129pub fn build_server_message(
134 operation: Operation,
135 identity: IdentityId,
136 rql: String,
137 params: Params,
138 reply: Reply<ServerResponse>,
139) -> ServerMessage {
140 match operation {
141 Operation::Query => ServerMessage::Query {
142 identity,
143 rql,
144 params,
145 reply,
146 },
147 Operation::Command => ServerMessage::Command {
148 identity,
149 rql,
150 params,
151 reply,
152 },
153 Operation::Admin => ServerMessage::Admin {
154 identity,
155 rql,
156 params,
157 reply,
158 },
159 Operation::Subscribe => unreachable!("subscribe uses a different dispatch path"),
160 }
161}