surrealdb/api/method/
import.rs

1use crate::api::conn::Method;
2use crate::api::conn::MlConfig;
3use crate::api::conn::Param;
4use crate::api::Connection;
5use crate::api::Error;
6use crate::api::ExtraFeatures;
7use crate::api::Result;
8use crate::method::Model;
9use crate::method::OnceLockExt;
10use crate::Surreal;
11use std::borrow::Cow;
12use std::future::Future;
13use std::future::IntoFuture;
14use std::marker::PhantomData;
15use std::path::PathBuf;
16use std::pin::Pin;
17
18/// An database import future
19#[derive(Debug)]
20#[must_use = "futures do nothing unless you `.await` or poll them"]
21pub struct Import<'r, C: Connection, T = ()> {
22	pub(super) client: Cow<'r, Surreal<C>>,
23	pub(super) file: PathBuf,
24	pub(super) ml_config: Option<MlConfig>,
25	pub(super) import_type: PhantomData<T>,
26}
27
28impl<'r, C> Import<'r, C>
29where
30	C: Connection,
31{
32	/// Import machine learning model
33	pub fn ml(self) -> Import<'r, C, Model> {
34		Import {
35			client: self.client,
36			file: self.file,
37			ml_config: Some(MlConfig::Import),
38			import_type: PhantomData,
39		}
40	}
41}
42
43impl<'r, C, T> Import<'r, C, T>
44where
45	C: Connection,
46{
47	/// Converts to an owned type which can easily be moved to a different thread
48	pub fn into_owned(self) -> Import<'static, C, T> {
49		Import {
50			client: Cow::Owned(self.client.into_owned()),
51			..self
52		}
53	}
54}
55
56impl<'r, Client, T> IntoFuture for Import<'r, Client, T>
57where
58	Client: Connection,
59{
60	type Output = Result<()>;
61	type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + Sync + 'r>>;
62
63	fn into_future(self) -> Self::IntoFuture {
64		Box::pin(async move {
65			let router = self.client.router.extract()?;
66			if !router.features.contains(&ExtraFeatures::Backup) {
67				return Err(Error::BackupsNotSupported.into());
68			}
69			let mut conn = Client::new(Method::Import);
70			let mut param = Param::file(self.file);
71			param.ml_config = self.ml_config;
72			conn.execute_unit(router, param).await
73		})
74	}
75}