surrealdb/api/method/
merge.rs1use crate::api::conn::Command;
2use crate::api::method::BoxFuture;
3use crate::api::opt::Resource;
4use crate::api::Connection;
5use crate::api::Result;
6use crate::method::OnceLockExt;
7use crate::value::Value;
8use crate::Surreal;
9use serde::de::DeserializeOwned;
10use serde::Serialize;
11use std::borrow::Cow;
12use std::future::IntoFuture;
13use std::marker::PhantomData;
14use surrealdb_core::sql::{to_value as to_core_value, Value as CoreValue};
15
16#[derive(Debug)]
18#[must_use = "futures do nothing unless you `.await` or poll them"]
19pub struct Merge<'r, C: Connection, D, R> {
20 pub(super) client: Cow<'r, Surreal<C>>,
21 pub(super) resource: Result<Resource>,
22 pub(super) content: D,
23 pub(super) response_type: PhantomData<R>,
24}
25
26impl<C, D, R> Merge<'_, C, D, R>
27where
28 C: Connection,
29{
30 pub fn into_owned(self) -> Merge<'static, C, D, R> {
32 Merge {
33 client: Cow::Owned(self.client.into_owned()),
34 ..self
35 }
36 }
37}
38
39macro_rules! into_future {
40 ($method:ident) => {
41 fn into_future(self) -> Self::IntoFuture {
42 let Merge {
43 client,
44 resource,
45 content,
46 ..
47 } = self;
48 let content = to_core_value(content);
49 Box::pin(async move {
50 let content = match content? {
51 CoreValue::None | CoreValue::Null => None,
52 x => Some(x),
53 };
54
55 let router = client.router.extract()?;
56 let cmd = Command::Merge {
57 what: resource?,
58 data: content,
59 };
60 router.$method(cmd).await
61 })
62 }
63 };
64}
65
66impl<'r, Client, D> IntoFuture for Merge<'r, Client, D, Value>
67where
68 Client: Connection,
69 D: Serialize + 'static,
70{
71 type Output = Result<Value>;
72 type IntoFuture = BoxFuture<'r, Self::Output>;
73
74 into_future! {execute_value}
75}
76
77impl<'r, Client, D, R> IntoFuture for Merge<'r, Client, D, Option<R>>
78where
79 Client: Connection,
80 D: Serialize + 'static,
81 R: DeserializeOwned,
82{
83 type Output = Result<Option<R>>;
84 type IntoFuture = BoxFuture<'r, Self::Output>;
85
86 into_future! {execute_opt}
87}
88
89impl<'r, Client, D, R> IntoFuture for Merge<'r, Client, D, Vec<R>>
90where
91 Client: Connection,
92 D: Serialize + 'static,
93 R: DeserializeOwned,
94{
95 type Output = Result<Vec<R>>;
96 type IntoFuture = BoxFuture<'r, Self::Output>;
97
98 into_future! {execute_vec}
99}