surrealdb_core/rpc/
response.rs

1use crate::dbs;
2use crate::dbs::Notification;
3use crate::sql;
4use crate::sql::Value;
5use revision::revisioned;
6use serde::Serialize;
7
8/// The data returned by the database
9// The variants here should be in exactly the same order as `crate::engine::remote::ws::Data`
10// In future, they will possibly be merged to avoid having to keep them in sync.
11#[revisioned(revision = 1)]
12#[derive(Debug, Serialize)]
13#[non_exhaustive]
14pub enum Data {
15	/// Generally methods return a `sql::Value`
16	Other(Value),
17	/// The query methods, `query` and `query_with` return a `Vec` of responses
18	Query(Vec<dbs::Response>),
19	/// Live queries return a notification
20	Live(Notification),
21	// Add new variants here
22}
23
24impl From<Value> for Data {
25	fn from(v: Value) -> Self {
26		Data::Other(v)
27	}
28}
29
30impl From<String> for Data {
31	fn from(v: String) -> Self {
32		Data::Other(Value::from(v))
33	}
34}
35
36impl From<Notification> for Data {
37	fn from(n: Notification) -> Self {
38		Data::Live(n)
39	}
40}
41
42impl From<Vec<dbs::Response>> for Data {
43	fn from(v: Vec<dbs::Response>) -> Self {
44		Data::Query(v)
45	}
46}
47
48impl From<Data> for Value {
49	fn from(val: Data) -> Self {
50		match val {
51			Data::Query(v) => sql::to_value(v).unwrap(),
52			Data::Live(v) => sql::to_value(v).unwrap(),
53			Data::Other(v) => v,
54		}
55	}
56}