mongodb/action/
session.rs

1use crate::{
2    client::options::{SessionOptions, TransactionOptions},
3    error::Result,
4    Client,
5    ClientSession,
6};
7
8use super::{action_impl, deeplink, export_doc, option_setters, options_doc};
9
10impl Client {
11    /// Starts a new [`ClientSession`].
12    ///
13    /// `await` will return d[`Result<ClientSession>`].
14    #[deeplink]
15    #[options_doc(start_session)]
16    pub fn start_session(&self) -> StartSession<'_> {
17        StartSession {
18            client: self,
19            options: None,
20        }
21    }
22}
23
24#[cfg(feature = "sync")]
25impl crate::sync::Client {
26    /// Starts a new [`ClientSession`].
27    ///
28    /// [run](StartSession::run) will return d[`Result<crate::sync::ClientSession>`].
29    #[deeplink]
30    #[options_doc(start_session, "run")]
31    pub fn start_session(&self) -> StartSession<'_> {
32        self.async_client.start_session()
33    }
34}
35
36/// Starts a new [`ClientSession`].  Construct with [`Client::start_session`].
37#[must_use]
38pub struct StartSession<'a> {
39    client: &'a Client,
40    options: Option<SessionOptions>,
41}
42
43#[option_setters(crate::client::options::SessionOptions)]
44#[export_doc(start_session)]
45impl StartSession<'_> {}
46
47#[action_impl(sync = crate::sync::ClientSession)]
48impl<'a> Action for StartSession<'a> {
49    type Future = StartSessionFuture;
50
51    async fn execute(self) -> Result<ClientSession> {
52        if let Some(options) = &self.options {
53            options.validate()?;
54        }
55        Ok(ClientSession::new(self.client.clone(), self.options, false).await)
56    }
57}