{-# LANGUAGE UndecidableInstances #-}
module Shrun.Configuration.Data.CommandLogging.ReadSize
( ReadSize (..),
parseReadSize,
)
where
import Shrun.Configuration.Default (Default (def))
import Shrun.Prelude
import Shrun.Utils qualified as U
newtype ReadSize = MkReadSize {ReadSize -> Bytes 'B Int
unReadSize :: Bytes B Int}
deriving stock (ReadSize -> ReadSize -> Bool
(ReadSize -> ReadSize -> Bool)
-> (ReadSize -> ReadSize -> Bool) -> Eq ReadSize
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ReadSize -> ReadSize -> Bool
== :: ReadSize -> ReadSize -> Bool
$c/= :: ReadSize -> ReadSize -> Bool
/= :: ReadSize -> ReadSize -> Bool
Eq, Int -> ReadSize -> ShowS
[ReadSize] -> ShowS
ReadSize -> String
(Int -> ReadSize -> ShowS)
-> (ReadSize -> String) -> ([ReadSize] -> ShowS) -> Show ReadSize
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ReadSize -> ShowS
showsPrec :: Int -> ReadSize -> ShowS
$cshow :: ReadSize -> String
show :: ReadSize -> String
$cshowList :: [ReadSize] -> ShowS
showList :: [ReadSize] -> ShowS
Show)
instance
( k ~ An_Iso,
a ~ Bytes B Int,
b ~ Bytes B Int
) =>
LabelOptic
"unReadSize"
k
ReadSize
ReadSize
a
b
where
labelOptic :: Optic k NoIx ReadSize ReadSize a b
labelOptic = (ReadSize -> a) -> (b -> ReadSize) -> Iso ReadSize ReadSize a b
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (\(MkReadSize Bytes 'B Int
x) -> a
Bytes 'B Int
x) b -> ReadSize
Bytes 'B Int -> ReadSize
MkReadSize
instance Default ReadSize where
def :: ReadSize
def = Bytes 'B Int -> ReadSize
MkReadSize (Bytes 'B Int -> ReadSize) -> Bytes 'B Int -> ReadSize
forall a b. (a -> b) -> a -> b
$ Int -> Bytes 'B Int
forall (s :: Size) n. n -> Bytes s n
MkBytes Int
16_000
instance DecodeTOML ReadSize where
tomlDecoder :: Decoder ReadSize
tomlDecoder = Decoder Text -> Decoder ReadSize
forall (m :: Type -> Type). MonadFail m => m Text -> m ReadSize
parseReadSize Decoder Text
forall a. DecodeTOML a => Decoder a
tomlDecoder
instance Pretty ReadSize where
pretty :: forall ann. ReadSize -> Doc ann
pretty (MkReadSize Bytes 'B Int
b) = Bytes 'B Int -> Doc ann
forall a (s :: Size) ann.
(BaseFormatter a ~ IntegralFormatter, Fromℤ a, MGroup a, Normed a,
Ord a, PrintfArg a, SingI s) =>
Bytes s a -> Doc ann
prettyBytesInt Bytes 'B Int
b
parseReadSize :: (MonadFail m) => m Text -> m ReadSize
parseReadSize :: forall (m :: Type -> Type). MonadFail m => m Text -> m ReadSize
parseReadSize m Text
getTxt = do
Text
byteTxt <- m Text
getTxt
case Text -> Either Text (Bytes 'B Natural)
U.parseByteText Text
byteTxt of
Right Bytes 'B Natural
b -> case (Natural -> Either String Int)
-> Bytes 'B Natural -> Either String (Bytes 'B Int)
forall (t :: Type -> Type) (f :: Type -> Type) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: Type -> Type) a b.
Applicative f =>
(a -> f b) -> Bytes 'B a -> f (Bytes 'B b)
traverse Natural -> Either String Int
forall a b.
(Bits a, Bits b, Integral a, Integral b, Show a, Typeable a,
Typeable b) =>
a -> Either String b
convertIntegral Bytes 'B Natural
b of
Left String
err -> String -> m ReadSize
forall a. String -> m a
forall (m :: Type -> Type) a. MonadFail m => String -> m a
fail (String -> m ReadSize) -> String -> m ReadSize
forall a b. (a -> b) -> a -> b
$ String
"Could not convert read-size: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
err
Right Bytes 'B Int
b' -> ReadSize -> m ReadSize
forall a. a -> m a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (ReadSize -> m ReadSize) -> ReadSize -> m ReadSize
forall a b. (a -> b) -> a -> b
$ Bytes 'B Int -> ReadSize
MkReadSize Bytes 'B Int
b'
Left Text
err -> String -> m ReadSize
forall a. String -> m a
forall (m :: Type -> Type) a. MonadFail m => String -> m a
fail (String -> m ReadSize) -> String -> m ReadSize
forall a b. (a -> b) -> a -> b
$ String
"Could not parse --command-log-read-size size: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
unpack Text
err
{-# INLINEABLE parseReadSize #-}