use core::{convert::TryInto, str::FromStr};
use serde::{Deserialize, Serialize};
use crate::Error;
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Paging {
Default,
All,
Specific {
page_number: PageNumber,
per_page: PerPage,
},
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct PageNumber(usize);
impl FromStr for PageNumber {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let raw = i64::from_str(s).map_err(Error::parse_int)?;
let raw_usize: usize = raw.try_into().map_err(Error::out_of_range)?;
Ok(raw_usize.into())
}
}
impl core::fmt::Display for PageNumber {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<usize> for PageNumber {
fn from(value: usize) -> Self {
Self(value)
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct PerPage(u8);
impl FromStr for PerPage {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let raw = i64::from_str(s).map_err(Error::parse_int)?;
let raw_u8: u8 = raw.try_into().map_err(Error::out_of_range)?;
Ok(raw_u8.into())
}
}
impl core::fmt::Display for PerPage {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<u8> for PerPage {
fn from(value: u8) -> Self {
Self(value)
}
}