module Codec.Tiled.World.IO ( WorldError(..) , 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.World (World) newtype WorldError = WorldError Text deriving (WorldError -> WorldError -> Bool forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a /= :: WorldError -> WorldError -> Bool $c/= :: WorldError -> WorldError -> Bool == :: WorldError -> WorldError -> Bool $c== :: WorldError -> WorldError -> Bool Eq, Int -> WorldError -> ShowS [WorldError] -> ShowS WorldError -> String forall a. (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a showList :: [WorldError] -> ShowS $cshowList :: [WorldError] -> ShowS show :: WorldError -> String $cshow :: WorldError -> String showsPrec :: Int -> WorldError -> ShowS $cshowsPrec :: Int -> WorldError -> ShowS Show) instance Exception WorldError readFile :: MonadIO m => FilePath -> m World readFile :: forall (m :: * -> *). MonadIO m => String -> m World readFile String source = forall (m :: * -> *) a. MonadIO m => IO a -> m a liftIO do ByteString bytes <- String -> IO ByteString ByteString.readFile String source case ByteString -> Either String World decodeMap ByteString bytes of Left String msg -> forall e a. Exception e => e -> IO a throwIO forall a b. (a -> b) -> a -> b $ Text -> WorldError WorldError (String -> Text Text.pack String msg) Right World res -> forall (f :: * -> *) a. Applicative f => a -> f a pure World res decodeMap :: ByteString -> Either String World decodeMap :: ByteString -> Either String World decodeMap = forall a. FromJSON a => ByteString -> Either String a Aeson.eitherDecodeStrict' writeFile :: MonadIO m => FilePath -> World -> m () writeFile :: forall (m :: * -> *). MonadIO m => String -> World -> m () writeFile String destination = forall (m :: * -> *) a. MonadIO m => IO a -> m a liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c . forall a. ToJSON a => String -> a -> IO () Aeson.encodeFile String destination