surrealdb_core/rpc/format/
mod.rs1pub mod bincode;
2pub mod cbor;
3pub mod json;
4pub mod msgpack;
5pub mod revision;
6
7use ::revision::Revisioned;
8use serde::Serialize;
9
10use super::{request::Request, RpcError};
11use crate::sql::Value;
12
13pub const PROTOCOLS: [&str; 5] = [
14 "json", "cbor", "msgpack", "bincode", "revision", ];
20
21#[derive(Debug, Clone, Copy, Eq, PartialEq)]
22#[non_exhaustive]
23pub enum Format {
24 Json, Cbor, Msgpack, Bincode, Revision, Unsupported, }
31
32pub trait ResTrait: Serialize + Into<Value> + Revisioned {}
33
34impl<T: Serialize + Into<Value> + Revisioned> ResTrait for T {}
35
36impl From<&str> for Format {
37 fn from(v: &str) -> Self {
38 match v {
39 s if s == PROTOCOLS[0] => Format::Json,
40 s if s == PROTOCOLS[1] => Format::Cbor,
41 s if s == PROTOCOLS[2] => Format::Msgpack,
42 s if s == PROTOCOLS[3] => Format::Bincode,
43 s if s == PROTOCOLS[4] => Format::Revision,
44 _ => Format::Unsupported,
45 }
46 }
47}
48
49impl Format {
50 pub fn req(&self, val: impl Into<Vec<u8>>) -> Result<Request, RpcError> {
52 let val = val.into();
53 match self {
54 Self::Json => json::req(&val),
55 Self::Cbor => cbor::req(val),
56 Self::Msgpack => msgpack::req(val),
57 Self::Bincode => bincode::req(&val),
58 Self::Revision => revision::req(val),
59 Self::Unsupported => Err(RpcError::InvalidRequest),
60 }
61 }
62
63 pub fn res(&self, val: impl ResTrait) -> Result<Vec<u8>, RpcError> {
65 match self {
66 Self::Json => json::res(val),
67 Self::Cbor => cbor::res(val),
68 Self::Msgpack => msgpack::res(val),
69 Self::Bincode => bincode::res(val),
70 Self::Revision => revision::res(val),
71 Self::Unsupported => Err(RpcError::InvalidRequest),
72 }
73 }
74
75 pub fn parse_value(&self, val: impl Into<Vec<u8>>) -> Result<Value, RpcError> {
77 let val = val.into();
78 match self {
79 Self::Json => json::parse_value(&val),
80 Self::Cbor => cbor::parse_value(val),
81 Self::Msgpack => msgpack::parse_value(val),
82 Self::Bincode => bincode::parse_value(&val),
83 Self::Revision => revision::parse_value(val),
84 Self::Unsupported => Err(RpcError::InvalidRequest),
85 }
86 }
87}