MCPcopy Create free account
hub / github.com/dfinity/orbit / paginated_items

Function paginated_items

core/station/impl/src/core/utils.rs:34–72  ·  view source on GitHub ↗

Paginates a list of items based on limit and offset.

(
    args: PaginatedItemsArgs<'_, T>,
)

Source from the content-addressed store, hash-verified

32
33/// Paginates a list of items based on limit and offset.
34pub fn paginated_items<T>(
35 args: PaginatedItemsArgs<'_, T>,
36) -> Result<PaginatedData<T>, PaginationError>
37where
38 T: Clone,
39{
40 let offset = args.offset.unwrap_or(0) as usize;
41
42 if let (Some(max_limit), Some(limit)) = (args.max_limit, args.limit) {
43 if limit > max_limit {
44 Err(PaginationError::MaxLimitExceeded { max: max_limit })?;
45 }
46 }
47
48 let default_limit = args.default_limit.unwrap_or(match args.max_limit {
49 Some(max_limit) => max_limit,
50 None => DEFAULT_PAGINATION_LIMIT,
51 });
52 let limit = args.limit.unwrap_or(default_limit) as usize;
53
54 let total = args.items.len();
55
56 let next_offset = match (offset + limit) < total {
57 true => Some((offset + limit) as u64),
58 false => None,
59 };
60
61 let items = args
62 .items
63 .get(offset..std::cmp::min(offset + limit, total))
64 .unwrap_or(&[])
65 .to_vec();
66
67 Ok(PaginatedData {
68 items,
69 next_offset,
70 total: total as u64,
71 })
72}
73
74/// Calculates the minimum threshold for a given percentage and total value.
75///

Calls 3

to_vecMethod · 0.80
lenMethod · 0.45
getMethod · 0.45