module Codec.Tiled.Object.Template.IO ( TemplateError(..) , readFile , writeFile ) where import Prelude hiding (readFile, writeFile) import Control.Exception (Exception, throwIO) import Control.Monad.IO.Class (MonadIO(..)) import Data.Aeson qualified as Aeson import Data.ByteString (ByteString) import Data.ByteString qualified as ByteString import Data.Text (Text) import Data.Text qualified as Text import Codec.Tiled.Object.Template (Template) newtype TemplateError = TemplateError Text deriving (TemplateError -> TemplateError -> Bool (TemplateError -> TemplateError -> Bool) -> (TemplateError -> TemplateError -> Bool) -> Eq TemplateError forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a /= :: TemplateError -> TemplateError -> Bool $c/= :: TemplateError -> TemplateError -> Bool == :: TemplateError -> TemplateError -> Bool $c== :: TemplateError -> TemplateError -> Bool Eq, Int -> TemplateError -> ShowS [TemplateError] -> ShowS TemplateError -> String (Int -> TemplateError -> ShowS) -> (TemplateError -> String) -> ([TemplateError] -> ShowS) -> Show TemplateError forall a. (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a showList :: [TemplateError] -> ShowS $cshowList :: [TemplateError] -> ShowS show :: TemplateError -> String $cshow :: TemplateError -> String showsPrec :: Int -> TemplateError -> ShowS $cshowsPrec :: Int -> TemplateError -> ShowS Show) instance Exception TemplateError readFile :: MonadIO m => FilePath -> m Template readFile :: String -> m Template readFile String source = IO Template -> m Template forall (m :: * -> *) a. MonadIO m => IO a -> m a liftIO do ByteString bytes <- String -> IO ByteString ByteString.readFile String source case ByteString -> Either String Template decodeMap ByteString bytes of Left String msg -> TemplateError -> IO Template forall e a. Exception e => e -> IO a throwIO (TemplateError -> IO Template) -> TemplateError -> IO Template forall a b. (a -> b) -> a -> b $ Text -> TemplateError TemplateError (String -> Text Text.pack String msg) Right Template res -> Template -> IO Template forall (f :: * -> *) a. Applicative f => a -> f a pure Template res decodeMap :: ByteString -> Either String Template decodeMap :: ByteString -> Either String Template decodeMap = ByteString -> Either String Template forall a. FromJSON a => ByteString -> Either String a Aeson.eitherDecodeStrict' writeFile :: MonadIO m => FilePath -> Template -> m () writeFile :: String -> Template -> m () writeFile String destination = IO () -> m () forall (m :: * -> *) a. MonadIO m => IO a -> m a liftIO (IO () -> m ()) -> (Template -> IO ()) -> Template -> m () forall b c a. (b -> c) -> (a -> b) -> a -> c . String -> Template -> IO () forall a. ToJSON a => String -> a -> IO () Aeson.encodeFile String destination