Skip to main content

surrealdb/api/method/
signup.rs

1use std::borrow::Cow;
2use std::future::IntoFuture;
3use std::marker::PhantomData;
4
5use serde::de::DeserializeOwned;
6use serde_content::Value as Content;
7
8use crate::api::conn::Command;
9use crate::api::method::BoxFuture;
10use crate::api::{Connection, Result};
11use crate::error::Api;
12use crate::method::OnceLockExt;
13use crate::{Surreal, api};
14
15/// A signup future
16#[derive(Debug)]
17#[must_use = "futures do nothing unless you `.await` or poll them"]
18pub struct Signup<'r, C: Connection, R> {
19	pub(super) client: Cow<'r, Surreal<C>>,
20	pub(super) credentials: serde_content::Result<Content<'static>>,
21	pub(super) response_type: PhantomData<R>,
22}
23
24impl<C, R> Signup<'_, C, R>
25where
26	C: Connection,
27{
28	/// Converts to an owned type which can easily be moved to a different
29	/// thread
30	pub fn into_owned(self) -> Signup<'static, C, R> {
31		Signup {
32			client: Cow::Owned(self.client.into_owned()),
33			..self
34		}
35	}
36}
37
38impl<'r, Client, R> IntoFuture for Signup<'r, Client, R>
39where
40	Client: Connection,
41	R: DeserializeOwned,
42{
43	type Output = Result<R>;
44	type IntoFuture = BoxFuture<'r, Self::Output>;
45
46	fn into_future(self) -> Self::IntoFuture {
47		let Signup {
48			client,
49			credentials,
50			..
51		} = self;
52		Box::pin(async move {
53			let router = client.inner.router.extract()?;
54			let content =
55				credentials.map_err(|x| crate::error::Api::DeSerializeValue(x.to_string()))?;
56			router
57				.execute(Command::Signup {
58					credentials: api::value::to_core_value(content)?
59						.into_object()
60						.ok_or(Api::CrendentialsNotObject)?,
61				})
62				.await
63		})
64	}
65}