{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
module Monatone.Types
( ParseError(..)
, Parser
, readInt
, readText
, formatError
) where
import Control.Monad.Except (ExceptT)
import Data.ByteString (ByteString)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import GHC.Generics (Generic)
data ParseError
= UnsupportedFormat Text
| CorruptedFile Text
| IOError Text
| PartialParse Text
| InvalidEncoding Text
deriving (Int -> ParseError -> ShowS
[ParseError] -> ShowS
ParseError -> String
(Int -> ParseError -> ShowS)
-> (ParseError -> String)
-> ([ParseError] -> ShowS)
-> Show ParseError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ParseError -> ShowS
showsPrec :: Int -> ParseError -> ShowS
$cshow :: ParseError -> String
show :: ParseError -> String
$cshowList :: [ParseError] -> ShowS
showList :: [ParseError] -> ShowS
Show, ParseError -> ParseError -> Bool
(ParseError -> ParseError -> Bool)
-> (ParseError -> ParseError -> Bool) -> Eq ParseError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ParseError -> ParseError -> Bool
== :: ParseError -> ParseError -> Bool
$c/= :: ParseError -> ParseError -> Bool
/= :: ParseError -> ParseError -> Bool
Eq, (forall x. ParseError -> Rep ParseError x)
-> (forall x. Rep ParseError x -> ParseError) -> Generic ParseError
forall x. Rep ParseError x -> ParseError
forall x. ParseError -> Rep ParseError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ParseError -> Rep ParseError x
from :: forall x. ParseError -> Rep ParseError x
$cto :: forall x. Rep ParseError x -> ParseError
to :: forall x. Rep ParseError x -> ParseError
Generic)
formatError :: ParseError -> Text
formatError :: ParseError -> Text
formatError (UnsupportedFormat Text
detail) = Text
"Unsupported format: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
detail
formatError (CorruptedFile Text
detail) = Text
"Corrupted file: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
detail
formatError (IOError Text
detail) = Text
"IO error: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
detail
formatError (PartialParse Text
detail) = Text
"Warning: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
detail Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (partial data recovered)"
formatError (InvalidEncoding Text
detail) = Text
"Encoding error: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
detail
type Parser = ExceptT ParseError IO
readInt :: Text -> Maybe Int
readInt :: Text -> Maybe Int
readInt Text
s = case ReadS Int
forall a. Read a => ReadS a
reads (Text -> String
T.unpack Text
s) of
[(Int
x, String
"")] -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
x
[(Int, String)]
_ -> Maybe Int
forall a. Maybe a
Nothing
readText :: ByteString -> Maybe Text
readText :: ByteString -> Maybe Text
readText ByteString
bs = case ByteString -> Either UnicodeException Text
TE.decodeUtf8' ByteString
bs of
Left UnicodeException
_ -> Maybe Text
forall a. Maybe a
Nothing
Right Text
txt -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
txt