surrealdb_core/rpc/format/msgpack/
mod.rs

1mod convert;
2
3use crate::rpc::format::ResTrait;
4use crate::rpc::RpcError;
5pub use convert::Pack;
6
7use crate::rpc::request::Request;
8use crate::sql::Value;
9
10pub fn parse_value(val: Vec<u8>) -> Result<Value, RpcError> {
11	let pack = rmpv::decode::read_value(&mut val.as_slice())
12		.map_err(|_| RpcError::ParseError)
13		.map(Pack)?;
14
15	Value::try_from(pack).map_err(|v: &str| RpcError::Thrown(v.into()))
16}
17
18pub fn req(val: Vec<u8>) -> Result<Request, RpcError> {
19	parse_value(val)?.try_into()
20}
21
22pub fn res(res: impl ResTrait) -> Result<Vec<u8>, RpcError> {
23	// Convert the response into a value
24	let val: Value = res.into();
25	let val: Pack = val.try_into()?;
26	// Create a new vector for encoding output
27	let mut res = Vec::new();
28	// Serialize the value into MsgPack binary data
29	rmpv::encode::write_value(&mut res, &val.0).unwrap();
30	Ok(res)
31}