mongodb/action/gridfs/
find.rs

1use std::time::Duration;
2
3use crate::bson::Document;
4
5use crate::{
6    action::{action_impl, deeplink, export_doc, option_setters, options_doc},
7    coll::options::{FindOneOptions, FindOptions},
8    error::Result,
9    gridfs::{FilesCollectionDocument, GridFsBucket, GridFsFindOneOptions, GridFsFindOptions},
10    Cursor,
11};
12
13impl GridFsBucket {
14    /// Finds and returns the [`FilesCollectionDocument`]s within this bucket that match the given
15    /// filter.
16    ///
17    /// `await` will return d[`Result<Cursor<FilesCollectionDocument>>`].
18    #[deeplink]
19    #[options_doc(find)]
20    pub fn find(&self, filter: Document) -> Find<'_> {
21        Find {
22            bucket: self,
23            filter,
24            options: None,
25        }
26    }
27
28    /// Finds and returns a single [`FilesCollectionDocument`] within this bucket that matches the
29    /// given filter.
30    ///
31    /// `await` will return d[`Result<Option<FilesCollectionDocument>>`].
32    #[deeplink]
33    #[options_doc(find_one)]
34    pub fn find_one(&self, filter: Document) -> FindOne<'_> {
35        FindOne {
36            bucket: self,
37            filter,
38            options: None,
39        }
40    }
41}
42
43#[cfg(feature = "sync")]
44impl crate::sync::gridfs::GridFsBucket {
45    /// Finds and returns the [`FilesCollectionDocument`]s within this bucket that match the given
46    /// filter.
47    ///
48    /// [`run`](Find::run) will return d[`Result<crate::sync::Cursor<FilesCollectionDocument>>`].
49    #[deeplink]
50    #[options_doc(find, "run")]
51    pub fn find(&self, filter: Document) -> Find<'_> {
52        self.async_bucket.find(filter)
53    }
54
55    /// Finds and returns a single [`FilesCollectionDocument`] within this bucket that matches the
56    /// given filter.
57    ///
58    /// [`run`](FindOne::run) will return d[`Result<Option<FilesCollectionDocument>>`].
59    #[deeplink]
60    #[options_doc(find_one, "run")]
61    pub fn find_one(&self, filter: Document) -> FindOne<'_> {
62        self.async_bucket.find_one(filter)
63    }
64}
65
66/// Finds and returns the [`FilesCollectionDocument`]s within a bucket that match a given
67/// filter.  Construct with [`GridFsBucket::find`].
68#[must_use]
69pub struct Find<'a> {
70    bucket: &'a GridFsBucket,
71    filter: Document,
72    options: Option<GridFsFindOptions>,
73}
74
75#[option_setters(crate::gridfs::options::GridFsFindOptions)]
76#[export_doc(find)]
77impl Find<'_> {}
78
79#[action_impl(sync = crate::sync::Cursor<FilesCollectionDocument>)]
80impl<'a> Action for Find<'a> {
81    type Future = FindFuture;
82
83    async fn execute(self) -> Result<Cursor<FilesCollectionDocument>> {
84        let find_options = self.options.map(FindOptions::from);
85        self.bucket
86            .files()
87            .find(self.filter)
88            .with_options(find_options)
89            .await
90    }
91}
92
93/// Finds and returns a single [`FilesCollectionDocument`] within a bucket that matches a
94/// given filter.  Construct with [`GridFsBucket::find_one`].
95#[must_use]
96pub struct FindOne<'a> {
97    bucket: &'a GridFsBucket,
98    filter: Document,
99    options: Option<GridFsFindOneOptions>,
100}
101
102#[option_setters(crate::gridfs::options::GridFsFindOneOptions)]
103#[export_doc(find_one)]
104impl FindOne<'_> {}
105
106#[action_impl]
107impl<'a> Action for FindOne<'a> {
108    type Future = FindOneFuture;
109
110    async fn execute(self) -> Result<Option<FilesCollectionDocument>> {
111        let find_options = self.options.map(FindOneOptions::from);
112        self.bucket
113            .files()
114            .find_one(self.filter)
115            .with_options(find_options)
116            .await
117    }
118}