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
$cshowsPrec :: Int -> CacheSize -> ShowS
showsPrec :: Int -> CacheSize -> ShowS
$cshow :: CacheSize -> String
show :: CacheSize -> String
$cshowList :: [CacheSize] -> ShowS
showList :: [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
$creadsPrec :: Int -> ReadS CacheSize
readsPrec :: Int -> ReadS CacheSize
$creadList :: ReadS [CacheSize]
readList :: ReadS [CacheSize]
$creadPrec :: ReadPrec CacheSize
readPrec :: ReadPrec CacheSize
$creadListPrec :: ReadPrec [CacheSize]
readListPrec :: ReadPrec [CacheSize]
Read, CacheSize -> CacheSize -> Bool
(CacheSize -> CacheSize -> Bool)
-> (CacheSize -> CacheSize -> Bool) -> Eq CacheSize
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CacheSize -> CacheSize -> Bool
== :: CacheSize -> CacheSize -> Bool
$c/= :: CacheSize -> CacheSize -> Bool
/= :: 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
$ccompare :: CacheSize -> CacheSize -> Ordering
compare :: CacheSize -> CacheSize -> Ordering
$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
>= :: CacheSize -> CacheSize -> Bool
$cmax :: CacheSize -> CacheSize -> CacheSize
max :: CacheSize -> CacheSize -> CacheSize
$cmin :: CacheSize -> CacheSize -> CacheSize
min :: CacheSize -> CacheSize -> CacheSize
Ord, CacheSize
CacheSize -> CacheSize -> Bounded CacheSize
forall a. a -> a -> Bounded a
$cminBound :: CacheSize
minBound :: CacheSize
$cmaxBound :: CacheSize
maxBound :: 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
$c+ :: CacheSize -> CacheSize -> CacheSize
+ :: CacheSize -> CacheSize -> CacheSize
$c- :: CacheSize -> CacheSize -> CacheSize
- :: CacheSize -> CacheSize -> CacheSize
$c* :: CacheSize -> CacheSize -> CacheSize
* :: CacheSize -> CacheSize -> CacheSize
$cnegate :: CacheSize -> CacheSize
negate :: CacheSize -> CacheSize
$cabs :: CacheSize -> CacheSize
abs :: CacheSize -> CacheSize
$csignum :: CacheSize -> CacheSize
signum :: CacheSize -> CacheSize
$cfromInteger :: Integer -> CacheSize
fromInteger :: Integer -> 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
$ctoRational :: CacheSize -> Rational
toRational :: CacheSize -> Rational
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
$cquot :: CacheSize -> CacheSize -> CacheSize
quot :: CacheSize -> CacheSize -> CacheSize
$crem :: CacheSize -> CacheSize -> CacheSize
rem :: CacheSize -> CacheSize -> CacheSize
$cdiv :: CacheSize -> CacheSize -> CacheSize
div :: CacheSize -> CacheSize -> CacheSize
$cmod :: CacheSize -> CacheSize -> CacheSize
mod :: CacheSize -> CacheSize -> CacheSize
$cquotRem :: CacheSize -> CacheSize -> (CacheSize, CacheSize)
quotRem :: CacheSize -> CacheSize -> (CacheSize, CacheSize)
$cdivMod :: CacheSize -> CacheSize -> (CacheSize, CacheSize)
divMod :: CacheSize -> CacheSize -> (CacheSize, CacheSize)
$ctoInteger :: CacheSize -> Integer
toInteger :: CacheSize -> Integer
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
$csucc :: CacheSize -> CacheSize
succ :: CacheSize -> CacheSize
$cpred :: CacheSize -> CacheSize
pred :: CacheSize -> CacheSize
$ctoEnum :: Int -> CacheSize
toEnum :: Int -> CacheSize
$cfromEnum :: CacheSize -> Int
fromEnum :: CacheSize -> Int
$cenumFrom :: CacheSize -> [CacheSize]
enumFrom :: CacheSize -> [CacheSize]
$cenumFromThen :: CacheSize -> CacheSize -> [CacheSize]
enumFromThen :: CacheSize -> CacheSize -> [CacheSize]
$cenumFromTo :: CacheSize -> CacheSize -> [CacheSize]
enumFromTo :: CacheSize -> CacheSize -> [CacheSize]
$cenumFromThenTo :: CacheSize -> CacheSize -> CacheSize -> [CacheSize]
enumFromThenTo :: CacheSize -> CacheSize -> 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
$ctoJSON :: CacheSize -> Value
toJSON :: CacheSize -> Value
$ctoEncoding :: CacheSize -> Encoding
toEncoding :: CacheSize -> Encoding
$ctoJSONList :: [CacheSize] -> Value
toJSONList :: [CacheSize] -> Value
$ctoEncodingList :: [CacheSize] -> Encoding
toEncodingList :: [CacheSize] -> Encoding
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
$cparseJSON :: Value -> Parser CacheSize
parseJSON :: Value -> Parser CacheSize
$cparseJSONList :: Value -> Parser [CacheSize]
parseJSONList :: 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 a. a -> Either String a
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 a. String -> Either String a
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