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