2022-07-04 01:03:26 +00:00
|
|
|
use serde::Serialize;
|
|
|
|
use std::borrow::Cow;
|
2022-07-07 10:25:22 +00:00
|
|
|
use surrealdb::channel::Sender;
|
2022-10-25 13:38:23 +00:00
|
|
|
use surrealdb::sql::serde::serialize_internal;
|
2022-07-04 01:03:26 +00:00
|
|
|
use surrealdb::sql::Value;
|
2022-07-07 10:25:22 +00:00
|
|
|
use warp::ws::Message;
|
2022-07-04 01:03:26 +00:00
|
|
|
|
2022-10-25 13:19:44 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum Output {
|
|
|
|
Json, // JSON
|
|
|
|
Cbor, // CBOR
|
2023-03-27 12:52:28 +00:00
|
|
|
Pack, // MessagePack
|
2022-10-25 13:19:44 +00:00
|
|
|
Full, // Full type serialization
|
2022-07-04 01:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
2022-10-19 22:54:41 +00:00
|
|
|
pub struct Response<T> {
|
2022-10-25 13:23:45 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2022-10-19 22:54:41 +00:00
|
|
|
id: Option<Value>,
|
2022-07-04 01:03:26 +00:00
|
|
|
#[serde(flatten)]
|
2022-10-19 22:54:41 +00:00
|
|
|
content: Content<T>,
|
2022-07-04 01:03:26 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 13:19:44 +00:00
|
|
|
#[derive(Serialize)]
|
|
|
|
enum Content<T> {
|
|
|
|
#[serde(rename = "result")]
|
|
|
|
Success(T),
|
|
|
|
#[serde(rename = "error")]
|
|
|
|
Failure(Failure),
|
|
|
|
}
|
|
|
|
|
2022-10-19 22:54:41 +00:00
|
|
|
impl<T: Serialize> Response<T> {
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Send the response to the channel
|
2022-10-25 13:19:44 +00:00
|
|
|
pub async fn send(self, out: Output, chn: Sender<Message>) {
|
|
|
|
match out {
|
|
|
|
Output::Json => {
|
|
|
|
let res = serde_json::to_string(&self).unwrap();
|
|
|
|
let res = Message::text(res);
|
|
|
|
let _ = chn.send(res).await;
|
|
|
|
}
|
|
|
|
Output::Cbor => {
|
|
|
|
let res = serde_cbor::to_vec(&self).unwrap();
|
|
|
|
let res = Message::binary(res);
|
|
|
|
let _ = chn.send(res).await;
|
|
|
|
}
|
|
|
|
Output::Pack => {
|
|
|
|
let res = serde_pack::to_vec(&self).unwrap();
|
|
|
|
let res = Message::binary(res);
|
|
|
|
let _ = chn.send(res).await;
|
|
|
|
}
|
2022-10-25 13:38:23 +00:00
|
|
|
Output::Full => {
|
2023-03-31 22:36:07 +00:00
|
|
|
let res = serialize_internal(|| bung::to_vec(&self).unwrap());
|
2022-10-25 13:38:23 +00:00
|
|
|
let res = Message::binary(res);
|
|
|
|
let _ = chn.send(res).await;
|
|
|
|
}
|
2022-10-25 13:19:44 +00:00
|
|
|
}
|
2022-07-07 10:25:22 +00:00
|
|
|
}
|
2022-07-04 01:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize)]
|
|
|
|
pub struct Failure {
|
|
|
|
code: i64,
|
|
|
|
message: Cow<'static, str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Failure {
|
|
|
|
pub const PARSE_ERROR: Failure = Failure {
|
|
|
|
code: -32700,
|
|
|
|
message: Cow::Borrowed("Parse error"),
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const INVALID_REQUEST: Failure = Failure {
|
|
|
|
code: -32600,
|
|
|
|
message: Cow::Borrowed("Invalid Request"),
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const METHOD_NOT_FOUND: Failure = Failure {
|
|
|
|
code: -32601,
|
|
|
|
message: Cow::Borrowed("Method not found"),
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const INVALID_PARAMS: Failure = Failure {
|
|
|
|
code: -32602,
|
|
|
|
message: Cow::Borrowed("Invalid params"),
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const INTERNAL_ERROR: Failure = Failure {
|
|
|
|
code: -32603,
|
|
|
|
message: Cow::Borrowed("Internal error"),
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn custom<S>(message: S) -> Failure
|
|
|
|
where
|
|
|
|
Cow<'static, str>: From<S>,
|
|
|
|
{
|
|
|
|
Failure {
|
|
|
|
code: -32000,
|
|
|
|
message: message.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-19 22:54:41 +00:00
|
|
|
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Create a JSON RPC result response
|
2022-10-19 22:54:41 +00:00
|
|
|
pub fn success<S: Serialize>(id: Option<Value>, val: S) -> Response<S> {
|
|
|
|
Response {
|
|
|
|
id,
|
|
|
|
content: Content::Success(val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Create a JSON RPC failure response
|
2022-10-19 22:54:41 +00:00
|
|
|
pub fn failure(id: Option<Value>, err: Failure) -> Response<Value> {
|
|
|
|
Response {
|
|
|
|
id,
|
|
|
|
content: Content::Failure(err),
|
|
|
|
}
|
|
|
|
}
|