module Hasura.Cache.Bounded
( CacheSize (..),
parseCacheSize,
)
where
import Data.Aeson qualified as J
import Data.Word (Word16)
import GHC.Natural (Natural)
import Hasura.Prelude hiding (lookup)
newtype CacheSize = CacheSize {CacheSize -> Word16
unCacheSize :: Word16}
deriving (Int -> CacheSize -> ShowS
[CacheSize] -> ShowS
CacheSize -> String
(Int -> CacheSize -> ShowS)
-> (CacheSize -> String)
-> ([CacheSize] -> ShowS)
-> Show CacheSize
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CacheSize] -> ShowS
$cshowList :: [CacheSize] -> ShowS
show :: CacheSize -> String
$cshow :: CacheSize -> String
showsPrec :: Int -> CacheSize -> ShowS
$cshowsPrec :: Int -> CacheSize -> ShowS
Show, ReadPrec [CacheSize]
ReadPrec CacheSize
Int -> ReadS CacheSize
ReadS [CacheSize]
(Int -> ReadS CacheSize)
-> ReadS [CacheSize]
-> ReadPrec CacheSize
-> ReadPrec [CacheSize]
-> Read CacheSize
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CacheSize]
$creadListPrec :: ReadPrec [CacheSize]
readPrec :: ReadPrec CacheSize
$creadPrec :: ReadPrec CacheSize
readList :: ReadS [CacheSize]
$creadList :: ReadS [CacheSize]
readsPrec :: Int -> ReadS CacheSize
$creadsPrec :: Int -> ReadS CacheSize
Read, CacheSize -> CacheSize -> Bool
(CacheSize -> CacheSize -> Bool)
-> (CacheSize -> CacheSize -> Bool) -> Eq CacheSize
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CacheSize -> CacheSize -> Bool
$c/= :: CacheSize -> CacheSize -> Bool
== :: CacheSize -> CacheSize -> Bool
$c== :: CacheSize -> CacheSize -> Bool
Eq, Eq CacheSize
Eq CacheSize
-> (CacheSize -> CacheSize -> Ordering)
-> (CacheSize -> CacheSize -> Bool)
-> (CacheSize -> CacheSize -> Bool)
-> (CacheSize -> CacheSize -> Bool)
-> (CacheSize -> CacheSize -> Bool)
-> (CacheSize -> CacheSize -> CacheSize)
-> (CacheSize -> CacheSize -> CacheSize)
-> Ord CacheSize
CacheSize -> CacheSize -> Bool
CacheSize -> CacheSize -> Ordering
CacheSize -> CacheSize -> CacheSize
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CacheSize -> CacheSize -> CacheSize
$cmin :: CacheSize -> CacheSize -> CacheSize
max :: CacheSize -> CacheSize -> CacheSize
$cmax :: CacheSize -> CacheSize -> CacheSize
>= :: CacheSize -> CacheSize -> Bool
$c>= :: CacheSize -> CacheSize -> Bool
> :: CacheSize -> CacheSize -> Bool
$c> :: CacheSize -> CacheSize -> Bool
<= :: CacheSize -> CacheSize -> Bool
$c<= :: CacheSize -> CacheSize -> Bool
< :: CacheSize -> CacheSize -> Bool
$c< :: CacheSize -> CacheSize -> Bool
compare :: CacheSize -> CacheSize -> Ordering
$ccompare :: CacheSize -> CacheSize -> Ordering
$cp1Ord :: Eq CacheSize
Ord, CacheSize
CacheSize -> CacheSize -> Bounded CacheSize
forall a. a -> a -> Bounded a
maxBound :: CacheSize
$cmaxBound :: CacheSize
minBound :: CacheSize
$cminBound :: CacheSize
Bounded, Integer -> CacheSize
CacheSize -> CacheSize
CacheSize -> CacheSize -> CacheSize
(CacheSize -> CacheSize -> CacheSize)
-> (CacheSize -> CacheSize -> CacheSize)
-> (CacheSize -> CacheSize -> CacheSize)
-> (CacheSize -> CacheSize)
-> (CacheSize -> CacheSize)
-> (CacheSize -> CacheSize)
-> (Integer -> CacheSize)
-> Num CacheSize
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> CacheSize
$cfromInteger :: Integer -> CacheSize
signum :: CacheSize -> CacheSize
$csignum :: CacheSize -> CacheSize
abs :: CacheSize -> CacheSize
$cabs :: CacheSize -> CacheSize
negate :: CacheSize -> CacheSize
$cnegate :: CacheSize -> CacheSize
* :: CacheSize -> CacheSize -> CacheSize
$c* :: CacheSize -> CacheSize -> CacheSize
- :: CacheSize -> CacheSize -> CacheSize
$c- :: CacheSize -> CacheSize -> CacheSize
+ :: CacheSize -> CacheSize -> CacheSize
$c+ :: CacheSize -> CacheSize -> CacheSize
Num, Num CacheSize
Ord CacheSize
Num CacheSize
-> Ord CacheSize -> (CacheSize -> Rational) -> Real CacheSize
CacheSize -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: CacheSize -> Rational
$ctoRational :: CacheSize -> Rational
$cp2Real :: Ord CacheSize
$cp1Real :: Num CacheSize
Real, Enum CacheSize
Real CacheSize
Real CacheSize
-> Enum CacheSize
-> (CacheSize -> CacheSize -> CacheSize)
-> (CacheSize -> CacheSize -> CacheSize)
-> (CacheSize -> CacheSize -> CacheSize)
-> (CacheSize -> CacheSize -> CacheSize)
-> (CacheSize -> CacheSize -> (CacheSize, CacheSize))
-> (CacheSize -> CacheSize -> (CacheSize, CacheSize))
-> (CacheSize -> Integer)
-> Integral CacheSize
CacheSize -> Integer
CacheSize -> CacheSize -> (CacheSize, CacheSize)
CacheSize -> CacheSize -> CacheSize
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: CacheSize -> Integer
$ctoInteger :: CacheSize -> Integer
divMod :: CacheSize -> CacheSize -> (CacheSize, CacheSize)
$cdivMod :: CacheSize -> CacheSize -> (CacheSize, CacheSize)
quotRem :: CacheSize -> CacheSize -> (CacheSize, CacheSize)
$cquotRem :: CacheSize -> CacheSize -> (CacheSize, CacheSize)
mod :: CacheSize -> CacheSize -> CacheSize
$cmod :: CacheSize -> CacheSize -> CacheSize
div :: CacheSize -> CacheSize -> CacheSize
$cdiv :: CacheSize -> CacheSize -> CacheSize
rem :: CacheSize -> CacheSize -> CacheSize
$crem :: CacheSize -> CacheSize -> CacheSize
quot :: CacheSize -> CacheSize -> CacheSize
$cquot :: CacheSize -> CacheSize -> CacheSize
$cp2Integral :: Enum CacheSize
$cp1Integral :: Real CacheSize
Integral, Int -> CacheSize
CacheSize -> Int
CacheSize -> [CacheSize]
CacheSize -> CacheSize
CacheSize -> CacheSize -> [CacheSize]
CacheSize -> CacheSize -> CacheSize -> [CacheSize]
(CacheSize -> CacheSize)
-> (CacheSize -> CacheSize)
-> (Int -> CacheSize)
-> (CacheSize -> Int)
-> (CacheSize -> [CacheSize])
-> (CacheSize -> CacheSize -> [CacheSize])
-> (CacheSize -> CacheSize -> [CacheSize])
-> (CacheSize -> CacheSize -> CacheSize -> [CacheSize])
-> Enum CacheSize
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: CacheSize -> CacheSize -> CacheSize -> [CacheSize]
$cenumFromThenTo :: CacheSize -> CacheSize -> CacheSize -> [CacheSize]
enumFromTo :: CacheSize -> CacheSize -> [CacheSize]
$cenumFromTo :: CacheSize -> CacheSize -> [CacheSize]
enumFromThen :: CacheSize -> CacheSize -> [CacheSize]
$cenumFromThen :: CacheSize -> CacheSize -> [CacheSize]
enumFrom :: CacheSize -> [CacheSize]
$cenumFrom :: CacheSize -> [CacheSize]
fromEnum :: CacheSize -> Int
$cfromEnum :: CacheSize -> Int
toEnum :: Int -> CacheSize
$ctoEnum :: Int -> CacheSize
pred :: CacheSize -> CacheSize
$cpred :: CacheSize -> CacheSize
succ :: CacheSize -> CacheSize
$csucc :: CacheSize -> CacheSize
Enum, [CacheSize] -> Value
[CacheSize] -> Encoding
CacheSize -> Value
CacheSize -> Encoding
(CacheSize -> Value)
-> (CacheSize -> Encoding)
-> ([CacheSize] -> Value)
-> ([CacheSize] -> Encoding)
-> ToJSON CacheSize
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [CacheSize] -> Encoding
$ctoEncodingList :: [CacheSize] -> Encoding
toJSONList :: [CacheSize] -> Value
$ctoJSONList :: [CacheSize] -> Value
toEncoding :: CacheSize -> Encoding
$ctoEncoding :: CacheSize -> Encoding
toJSON :: CacheSize -> Value
$ctoJSON :: CacheSize -> Value
J.ToJSON, Value -> Parser [CacheSize]
Value -> Parser CacheSize
(Value -> Parser CacheSize)
-> (Value -> Parser [CacheSize]) -> FromJSON CacheSize
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [CacheSize]
$cparseJSONList :: Value -> Parser [CacheSize]
parseJSON :: Value -> Parser CacheSize
$cparseJSON :: Value -> Parser CacheSize
J.FromJSON)
parseCacheSize :: String -> Either String CacheSize
parseCacheSize :: String -> Either String CacheSize
parseCacheSize String
v =
case String -> Maybe Natural
forall a. Read a => String -> Maybe a
readMaybe String
v :: Maybe Natural of
Just Natural
n | Natural
n Natural -> Natural -> Bool
forall a. Ord a => a -> a -> Bool
<= Natural
max16 Bool -> Bool -> Bool
&& Natural
n Natural -> Natural -> Bool
forall a. Ord a => a -> a -> Bool
>= Natural
0 -> CacheSize -> Either String CacheSize
forall (m :: * -> *) a. Monad m => a -> m a
return (Word16 -> CacheSize
CacheSize (Word16 -> CacheSize) -> Word16 -> CacheSize
forall a b. (a -> b) -> a -> b
$ Natural -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Natural
n)
Maybe Natural
_ -> String -> Either String CacheSize
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError String
"cache size must be given as a number between 0 and 65535"
where
max16 :: Natural
max16 = Word16 -> Natural
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16
forall a. Bounded a => a
maxBound :: Word16) :: Natural