aws_sdk_bedrockagentruntime/operation/rerank/
paginator.rs

1// Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
2/// Paginator for [`Rerank`](crate::operation::rerank::Rerank)
3pub struct RerankPaginator {
4    handle: std::sync::Arc<crate::client::Handle>,
5    builder: crate::operation::rerank::builders::RerankInputBuilder,
6    stop_on_duplicate_token: bool,
7}
8
9impl RerankPaginator {
10    /// Create a new paginator-wrapper
11    pub(crate) fn new(handle: std::sync::Arc<crate::client::Handle>, builder: crate::operation::rerank::builders::RerankInputBuilder) -> Self {
12        Self {
13            handle,
14            builder,
15            stop_on_duplicate_token: true,
16        }
17    }
18
19    /// Create a flattened paginator
20    ///
21    /// This paginator automatically flattens results using `results`. Queries to the underlying service
22    /// are dispatched lazily.
23    pub fn items(self) -> crate::operation::rerank::paginator::RerankPaginatorItems {
24        crate::operation::rerank::paginator::RerankPaginatorItems(self)
25    }
26
27    /// Stop paginating when the service returns the same pagination token twice in a row.
28    ///
29    /// Defaults to true.
30    ///
31    /// For certain operations, it may be useful to continue on duplicate token. For example,
32    /// if an operation is for tailing a log file in real-time, then continuing may be desired.
33    /// This option can be set to `false` to accommodate these use cases.
34    pub fn stop_on_duplicate_token(mut self, stop_on_duplicate_token: bool) -> Self {
35        self.stop_on_duplicate_token = stop_on_duplicate_token;
36        self
37    }
38
39    /// Create the pagination stream
40    ///
41    /// _Note:_ No requests will be dispatched until the stream is used
42    /// (e.g. with the [`.next().await`](aws_smithy_async::future::pagination_stream::PaginationStream::next) method).
43    pub fn send(
44        self,
45    ) -> ::aws_smithy_async::future::pagination_stream::PaginationStream<
46        ::std::result::Result<
47            crate::operation::rerank::RerankOutput,
48            ::aws_smithy_runtime_api::client::result::SdkError<
49                crate::operation::rerank::RerankError,
50                ::aws_smithy_runtime_api::client::orchestrator::HttpResponse,
51            >,
52        >,
53    > {
54        // Move individual fields out of self for the borrow checker
55        let builder = self.builder;
56        let handle = self.handle;
57        let runtime_plugins =
58            crate::operation::rerank::Rerank::operation_runtime_plugins(handle.runtime_plugins.clone(), &handle.conf, ::std::option::Option::None)
59                .with_operation_plugin(crate::sdk_feature_tracker::paginator::PaginatorFeatureTrackerRuntimePlugin::new());
60        ::aws_smithy_async::future::pagination_stream::PaginationStream::new(::aws_smithy_async::future::pagination_stream::fn_stream::FnStream::new(
61            move |tx| {
62                ::std::boxed::Box::pin(async move {
63                    // Build the input for the first time. If required fields are missing, this is where we'll produce an early error.
64                    let mut input = match builder
65                        .build()
66                        .map_err(::aws_smithy_runtime_api::client::result::SdkError::construction_failure)
67                    {
68                        ::std::result::Result::Ok(input) => input,
69                        ::std::result::Result::Err(e) => {
70                            let _ = tx.send(::std::result::Result::Err(e)).await;
71                            return;
72                        }
73                    };
74                    loop {
75                        let resp = crate::operation::rerank::Rerank::orchestrate(&runtime_plugins, input.clone()).await;
76                        // If the input member is None or it was an error
77                        let done = match resp {
78                            ::std::result::Result::Ok(ref resp) => {
79                                let new_token = crate::lens::reflens_rerank_output_output_next_token(resp);
80                                // Pagination is exhausted when the next token is an empty string
81                                let is_empty = new_token.map(|token| token.is_empty()).unwrap_or(true);
82                                if !is_empty && new_token == input.next_token.as_ref() && self.stop_on_duplicate_token {
83                                    true
84                                } else {
85                                    input.next_token = new_token.cloned();
86                                    is_empty
87                                }
88                            }
89                            ::std::result::Result::Err(_) => true,
90                        };
91                        if tx.send(resp).await.is_err() {
92                            // receiving end was dropped
93                            return;
94                        }
95                        if done {
96                            return;
97                        }
98                    }
99                })
100            },
101        ))
102    }
103}
104
105/// Flattened paginator for `RerankPaginator`
106///
107/// This is created with [`.items()`](RerankPaginator::items)
108pub struct RerankPaginatorItems(RerankPaginator);
109
110impl RerankPaginatorItems {
111    /// Create the pagination stream
112    ///
113    /// _Note_: No requests will be dispatched until the stream is used
114    /// (e.g. with the [`.next().await`](aws_smithy_async::future::pagination_stream::PaginationStream::next) method).
115    ///
116    /// To read the entirety of the paginator, use [`.collect::<Result<Vec<_>, _>()`](aws_smithy_async::future::pagination_stream::PaginationStream::collect).
117    pub fn send(
118        self,
119    ) -> ::aws_smithy_async::future::pagination_stream::PaginationStream<
120        ::std::result::Result<
121            crate::types::RerankResult,
122            ::aws_smithy_runtime_api::client::result::SdkError<
123                crate::operation::rerank::RerankError,
124                ::aws_smithy_runtime_api::client::orchestrator::HttpResponse,
125            >,
126        >,
127    > {
128        ::aws_smithy_async::future::pagination_stream::TryFlatMap::new(self.0.send())
129            .flat_map(|page| crate::lens::lens_rerank_output_output_results(page).unwrap_or_default().into_iter())
130    }
131}