module Toml.Codec.Code
(
decode
, decodeExact
, decodeValidation
, decodeFileEither
, decodeFile
, decodeFileExact
, encode
, encodeToFile
, runTomlCodec
, execTomlCodec
) where
import Control.Exception (throwIO)
import Control.Monad.IO.Class (MonadIO (..))
import Data.Text (Text)
import Validation (Validation (..), validationToEither)
import Toml.Codec.Error (LoadTomlException (..), TomlDecodeError (..), prettyTomlDecodeErrors)
import Toml.Codec.Types (Codec (..), TomlCodec, TomlState (..))
import Toml.Parser (parse)
import Toml.Type (TOML (..), tomlDiff)
import Toml.Type.Printer (pretty)
import qualified Data.Text.IO as TIO
decodeValidation :: TomlCodec a -> Text -> Validation [TomlDecodeError] a
decodeValidation :: forall a. TomlCodec a -> Text -> Validation [TomlDecodeError] a
decodeValidation TomlCodec a
codec Text
text = case Text -> Either TomlParseError TOML
parse Text
text of
Left TomlParseError
err -> forall e a. e -> Validation e a
Failure [TomlParseError -> TomlDecodeError
ParseError TomlParseError
err]
Right TOML
toml -> forall a. TomlCodec a -> TOML -> Validation [TomlDecodeError] a
runTomlCodec TomlCodec a
codec TOML
toml
decode :: TomlCodec a -> Text -> Either [TomlDecodeError] a
decode :: forall a. TomlCodec a -> Text -> Either [TomlDecodeError] a
decode TomlCodec a
codec = forall e a. Validation e a -> Either e a
validationToEither forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. TomlCodec a -> Text -> Validation [TomlDecodeError] a
decodeValidation TomlCodec a
codec
decodeExact :: TomlCodec a -> Text -> Either [TomlDecodeError] a
decodeExact :: forall a. TomlCodec a -> Text -> Either [TomlDecodeError] a
decodeExact TomlCodec a
codec Text
text = case Text -> Either TomlParseError TOML
parse Text
text of
Left TomlParseError
err -> forall a b. a -> Either a b
Left [TomlParseError -> TomlDecodeError
ParseError TomlParseError
err]
Right TOML
toml -> case forall a. TomlCodec a -> TOML -> Validation [TomlDecodeError] a
runTomlCodec TomlCodec a
codec TOML
toml of
Failure [TomlDecodeError]
errs -> forall a b. a -> Either a b
Left [TomlDecodeError]
errs
Success a
a ->
let tomlExpected :: TOML
tomlExpected = forall a. TomlCodec a -> a -> TOML
execTomlCodec TomlCodec a
codec a
a
aDiff :: TOML
aDiff = TOML -> TOML -> TOML
tomlDiff TOML
toml TOML
tomlExpected in
if TOML
aDiff forall a. Eq a => a -> a -> Bool
== forall a. Monoid a => a
mempty
then forall a b. b -> Either a b
Right a
a
else forall a b. a -> Either a b
Left [TOML -> TomlDecodeError
NotExactDecode TOML
aDiff]
decodeFileValidation
:: forall a m . (MonadIO m)
=> TomlCodec a
-> FilePath
-> m (Validation [TomlDecodeError] a)
decodeFileValidation :: forall a (m :: * -> *).
MonadIO m =>
TomlCodec a -> FilePath -> m (Validation [TomlDecodeError] a)
decodeFileValidation TomlCodec a
codec = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. TomlCodec a -> Text -> Validation [TomlDecodeError] a
decodeValidation TomlCodec a
codec) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO Text
TIO.readFile
decodeFileEither
:: forall a m . (MonadIO m)
=> TomlCodec a
-> FilePath
-> m (Either [TomlDecodeError] a)
decodeFileEither :: forall a (m :: * -> *).
MonadIO m =>
TomlCodec a -> FilePath -> m (Either [TomlDecodeError] a)
decodeFileEither TomlCodec a
codec = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall e a. Validation e a -> Either e a
validationToEither forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (m :: * -> *).
MonadIO m =>
TomlCodec a -> FilePath -> m (Validation [TomlDecodeError] a)
decodeFileValidation TomlCodec a
codec
decodeFileExact
:: forall a m . (MonadIO m)
=> TomlCodec a
-> FilePath
-> m (Either [TomlDecodeError] a)
decodeFileExact :: forall a (m :: * -> *).
MonadIO m =>
TomlCodec a -> FilePath -> m (Either [TomlDecodeError] a)
decodeFileExact TomlCodec a
codec = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. TomlCodec a -> Text -> Either [TomlDecodeError] a
decodeExact TomlCodec a
codec) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO Text
TIO.readFile
decodeFile :: forall a m . (MonadIO m) => TomlCodec a -> FilePath -> m a
decodeFile :: forall a (m :: * -> *). MonadIO m => TomlCodec a -> FilePath -> m a
decodeFile TomlCodec a
codec FilePath
filePath = forall a (m :: * -> *).
MonadIO m =>
TomlCodec a -> FilePath -> m (Either [TomlDecodeError] a)
decodeFileEither TomlCodec a
codec FilePath
filePath forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either [TomlDecodeError] a -> m a
errorWhenLeft
where
errorWhenLeft :: Either [TomlDecodeError] a -> m a
errorWhenLeft :: Either [TomlDecodeError] a -> m a
errorWhenLeft (Left [TomlDecodeError]
errs) =
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
forall a b. (a -> b) -> a -> b
$ forall e a. Exception e => e -> IO a
throwIO
forall a b. (a -> b) -> a -> b
$ FilePath -> Text -> LoadTomlException
LoadTomlException FilePath
filePath
forall a b. (a -> b) -> a -> b
$ [TomlDecodeError] -> Text
prettyTomlDecodeErrors [TomlDecodeError]
errs
errorWhenLeft (Right a
pc) = forall (f :: * -> *) a. Applicative f => a -> f a
pure a
pc
encode :: TomlCodec a -> a -> Text
encode :: forall a. TomlCodec a -> a -> Text
encode TomlCodec a
codec a
obj = TOML -> Text
pretty forall a b. (a -> b) -> a -> b
$ forall a. TomlCodec a -> a -> TOML
execTomlCodec TomlCodec a
codec a
obj
encodeToFile :: forall a m . (MonadIO m) => TomlCodec a -> FilePath -> a -> m Text
encodeToFile :: forall a (m :: * -> *).
MonadIO m =>
TomlCodec a -> FilePath -> a -> m Text
encodeToFile TomlCodec a
codec FilePath
filePath a
obj = Text
content forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (FilePath -> Text -> IO ()
TIO.writeFile FilePath
filePath Text
content)
where
content :: Text
content :: Text
content = forall a. TomlCodec a -> a -> Text
encode TomlCodec a
codec a
obj
runTomlCodec :: TomlCodec a -> TOML -> Validation [TomlDecodeError] a
runTomlCodec :: forall a. TomlCodec a -> TOML -> Validation [TomlDecodeError] a
runTomlCodec = forall i o. Codec i o -> TomlEnv o
codecRead
execTomlCodec :: TomlCodec a -> a -> TOML
execTomlCodec :: forall a. TomlCodec a -> a -> TOML
execTomlCodec TomlCodec a
codec a
obj = forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. TomlState a -> TOML -> (Maybe a, TOML)
unTomlState (forall i o. Codec i o -> i -> TomlState o
codecWrite TomlCodec a
codec a
obj) forall a. Monoid a => a
mempty