1use thiserror::Error;
7
8use crate::api::ApiError;
9
10#[derive(Clone, Debug)]
12pub struct PageLimit(pub u16);
13
14impl PageLimit {
15 pub fn new(limit: u16) -> Result<Self, ApiError<PaginationError>> {
17 if limit <= 1000 {
18 Ok(Self(limit))
19 } else {
20 Err(ApiError::Pagination {
21 source: PaginationError::ExceedLimit,
22 })
23 }
24 }
25}
26
27#[derive(Debug, Error)]
29#[non_exhaustive]
30pub enum PaginationError {
31 #[error("pagination exceeds limit error")]
33 ExceedLimit,
34}
35
36#[cfg(test)]
37mod tests {
38 use super::PageLimit;
39
40 #[test]
41 fn test_new() {
42 let limit = PageLimit::new(5);
43 assert!(limit.is_ok());
44
45 assert_eq!(limit.unwrap().0, 5);
46 }
47
48 #[test]
49 fn test_over_limit() {
50 let limit = PageLimit::new(9999);
51 assert!(limit.is_err());
52
53 let err_message = limit.err().unwrap();
54 assert_eq!(
55 err_message.to_string(),
56 "pagination error: pagination exceeds limit error"
57 );
58 }
59}