1 {-# LANGUAGE OverloadedStrings #-}
    2 module Databrary.Model.Paginate
    3   ( Paginate(..)
    4   , def
    5   ) where
    6 
    7 import Data.Default.Class (Default(..))
    8 import Data.Int (Int32)
    9 
   10 -- | Denote information for retrieving one page, most likely from a query result
   11 data Paginate = Paginate
   12   { paginateOffset :: !Int32 -- ^ number of rows to skip before starting
   13   , paginateLimit :: !Int32 -- ^ max number of rows to retrieve after offset
   14   } -- deriving (Eq, Ord, Show)
   15 
   16 -- | A page must have at least one row requested, and can only request up to 129 rows in one page
   17 instance Bounded Paginate where
   18   minBound = Paginate 0 1
   19   maxBound = Paginate maxBound 129
   20 
   21 -- | Default paging starts from the beginning and retrieves up to 25 rows
   22 instance Default Paginate where
   23   def = Paginate 0 25
   24 
   25 -- | ways to traverse forward and backwards between pages, using limit
   26 instance Enum Paginate where
   27   succ (Paginate o l) = Paginate (o+l) l
   28   pred (Paginate 0 _) = error "pred Paginate: invalid argument"
   29   pred (Paginate o l) = Paginate (o-l `max` 0) l
   30   -- | toEnum seems to map to intuitive concept of a page
   31   toEnum i = Paginate (d*fromIntegral i) d where d = paginateLimit def
   32   -- | fromEnum seems to map to an individual row, instead of a page
   33   fromEnum (Paginate o l) = fromIntegral $ o + l - 1 `div` l