surrealdb/api/method/
signup.rs

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