use crate::api::conn::Command;
use crate::api::method::BoxFuture;
use crate::api::method::Content;
use crate::api::method::Merge;
use crate::api::method::Patch;
use crate::api::opt::PatchOp;
use crate::api::opt::Resource;
use crate::api::Connection;
use crate::api::Result;
use crate::method::OnceLockExt;
use crate::opt::KeyRange;
use crate::Surreal;
use crate::Value;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::borrow::Cow;
use std::future::IntoFuture;
use std::marker::PhantomData;
use surrealdb_core::sql::{to_value as to_core_value, Value as CoreValue};
use super::validate_data;
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Update<'r, C: Connection, R> {
pub(super) client: Cow<'r, Surreal<C>>,
pub(super) resource: Result<Resource>,
pub(super) response_type: PhantomData<R>,
}
impl<C, R> Update<'_, C, R>
where
C: Connection,
{
pub fn into_owned(self) -> Update<'static, C, R> {
Update {
client: Cow::Owned(self.client.into_owned()),
..self
}
}
}
macro_rules! into_future {
($method:ident) => {
fn into_future(self) -> Self::IntoFuture {
let Update {
client,
resource,
..
} = self;
Box::pin(async move {
let router = client.router.extract()?;
router
.$method(Command::Update {
what: resource?,
data: None,
})
.await
})
}
};
}
impl<'r, Client> IntoFuture for Update<'r, Client, Value>
where
Client: Connection,
{
type Output = Result<Value>;
type IntoFuture = BoxFuture<'r, Self::Output>;
into_future! {execute_value}
}
impl<'r, Client, R> IntoFuture for Update<'r, Client, Option<R>>
where
Client: Connection,
R: DeserializeOwned,
{
type Output = Result<Option<R>>;
type IntoFuture = BoxFuture<'r, Self::Output>;
into_future! {execute_opt}
}
impl<'r, Client, R> IntoFuture for Update<'r, Client, Vec<R>>
where
Client: Connection,
R: DeserializeOwned,
{
type Output = Result<Vec<R>>;
type IntoFuture = BoxFuture<'r, Self::Output>;
into_future! {execute_vec}
}
impl<C> Update<'_, C, Value>
where
C: Connection,
{
pub fn range(mut self, range: impl Into<KeyRange>) -> Self {
self.resource = self.resource.and_then(|x| x.with_range(range.into()));
self
}
}
impl<C, R> Update<'_, C, Vec<R>>
where
C: Connection,
{
pub fn range(mut self, range: impl Into<KeyRange>) -> Self {
self.resource = self.resource.and_then(|x| x.with_range(range.into()));
self
}
}
impl<'r, C, R> Update<'r, C, R>
where
C: Connection,
R: DeserializeOwned,
{
pub fn content<D>(self, data: D) -> Content<'r, C, R>
where
D: Serialize + 'static,
{
Content::from_closure(self.client, || {
let data = to_core_value(data)?;
validate_data(&data, "Tried to update non-object-like data as content, only structs and objects are supported")?;
let what = self.resource?;
let data = match data {
CoreValue::None => None,
content => Some(content),
};
Ok(Command::Update {
what,
data,
})
})
}
pub fn merge<D>(self, data: D) -> Merge<'r, C, D, R>
where
D: Serialize,
{
Merge {
client: self.client,
resource: self.resource,
content: data,
upsert: false,
response_type: PhantomData,
}
}
pub fn patch(self, patch: impl Into<PatchOp>) -> Patch<'r, C, R> {
let PatchOp(result) = patch.into();
let patches = match result {
Ok(serde_content::Value::Seq(values)) => values.into_iter().map(Ok).collect(),
Ok(value) => vec![Ok(value)],
Err(error) => vec![Err(error)],
};
Patch {
patches,
client: self.client,
resource: self.resource,
upsert: false,
response_type: PhantomData,
}
}
}