1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use lexoffice::model::Page;
use lexoffice::request::RequestWithState;
use lexoffice::request::{Endpoint, Paginated};
use lexoffice::Result;
use serde::de::DeserializeOwned;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
pub struct PaginatedOpt {
    /// page to retrieve
    #[structopt(short, long)]
    page: Option<usize>,
    /// number of items in a page
    #[structopt(short, long)]
    size: Option<usize>,
}

impl PaginatedOpt {
    pub async fn exec<T, U>(
        &self,
        request: RequestWithState<T, U>,
    ) -> Result<Page<T>>
    where
        RequestWithState<T, U>: Paginated + Endpoint + Send + Sync + Clone,
        T: DeserializeOwned + Send + Sync + Clone + 'static,
        U: Send + Sync + Clone + 'static,
    {
        let page = self.page.unwrap_or(0);
        if let Some(size) = self.size {
            request.page_size(page, size).await
        } else {
            request.page(page).await
        }
    }
}