surrealdb_core/dbs/
response.rs

1use crate::err::Error;
2use crate::sql::Value as CoreValue;
3use revision::revisioned;
4use revision::Revisioned;
5use serde::ser::SerializeStruct;
6use serde::Deserialize;
7use serde::Serialize;
8use std::time::Duration;
9
10pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Response";
11
12#[derive(Debug)]
13#[non_exhaustive]
14pub enum QueryType {
15	// Any kind of query
16	Other,
17	// Indicates that the response live query id must be tracked
18	Live,
19	// Indicates that the live query should be removed from tracking
20	Kill,
21}
22
23/// The return value when running a query set on the database.
24#[derive(Debug)]
25#[non_exhaustive]
26pub struct Response {
27	pub time: Duration,
28	pub result: Result<CoreValue, Error>,
29	// Record the query type in case processing the response is necessary (such as tracking live queries).
30	pub query_type: QueryType,
31}
32
33impl Response {
34	/// Return the transaction duration as a string
35	pub fn speed(&self) -> String {
36		format!("{:?}", self.time)
37	}
38
39	/// Retrieve the response as a normal result
40	pub fn output(self) -> Result<CoreValue, Error> {
41		self.result
42	}
43}
44
45#[revisioned(revision = 1)]
46#[derive(Debug, Serialize, Deserialize)]
47#[serde(rename_all = "UPPERCASE")]
48#[non_exhaustive]
49pub enum Status {
50	Ok,
51	Err,
52}
53
54impl Serialize for Response {
55	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
56	where
57		S: serde::Serializer,
58	{
59		let mut val = serializer.serialize_struct(TOKEN, 3)?;
60		val.serialize_field("time", self.speed().as_str())?;
61		match &self.result {
62			Ok(v) => {
63				val.serialize_field("status", &Status::Ok)?;
64				val.serialize_field("result", v)?;
65			}
66			Err(e) => {
67				val.serialize_field("status", &Status::Err)?;
68				val.serialize_field("result", &CoreValue::from(e.to_string()))?;
69			}
70		}
71		val.end()
72	}
73}
74
75#[revisioned(revision = 1)]
76#[derive(Debug, Serialize, Deserialize)]
77#[non_exhaustive]
78pub struct QueryMethodResponse {
79	pub time: String,
80	pub status: Status,
81	pub result: CoreValue,
82}
83
84impl From<&Response> for QueryMethodResponse {
85	fn from(res: &Response) -> Self {
86		let time = res.speed();
87		let (status, result) = match &res.result {
88			Ok(value) => (Status::Ok, value.clone()),
89			Err(error) => (Status::Err, CoreValue::from(error.to_string())),
90		};
91		Self {
92			status,
93			result,
94			time,
95		}
96	}
97}
98
99impl Revisioned for Response {
100	#[inline]
101	fn serialize_revisioned<W: std::io::Write>(
102		&self,
103		writer: &mut W,
104	) -> std::result::Result<(), revision::Error> {
105		QueryMethodResponse::from(self).serialize_revisioned(writer)
106	}
107
108	#[inline]
109	fn deserialize_revisioned<R: std::io::Read>(
110		_reader: &mut R,
111	) -> std::result::Result<Self, revision::Error> {
112		unreachable!("deserialising `Response` directly is not supported")
113	}
114
115	fn revision() -> u16 {
116		1
117	}
118}