code wiki / _hdl_build / nx_apistack_paginate.nx
nx_apistack_paginate.nx source
↩ module page · 20 lines · 930 B
1// nx_apistack_paginate.nx -- CAP-API-PAGINATE: deterministic cursor pagination for /api list endpoints (shards,
2// services, audit...). Pure index math over a stable ordered set: pg_count = items on this page, pg_next = the next
3// cursor (or -1 = last page, the Link rel=next terminator). Deterministic over the append-only registries = the
4// sovereign exceed over opaque cloud page-tokens. license_tier: ORIGINAL
5import "nx_syscalls.nx"
6
7// number of items on the page starting at `cursor` (0-based), capped at `limit`.
8func pg_count(total: i64, cursor: i64, limit: i64) -> i64 {
9 if cursor >= total { return 0 }
10 if cursor < 0 { return 0 }
11 let rem: i64 = total - cursor
12 if rem < limit { return rem }
13 return limit
14}
15// next cursor, or -1 if this page is the last.
16func pg_next(total: i64, cursor: i64, limit: i64) -> i64 {
17 let nc: i64 = cursor + limit
18 if nc >= total { return 0 - 1 }
19 return nc
20}