summaryrefslogtreecommitdiff
path: root/lib/Tiled2.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Tiled2.hs')
-rw-r--r--lib/Tiled2.hs39
1 files changed, 24 insertions, 15 deletions
diff --git a/lib/Tiled2.hs b/lib/Tiled2.hs
index f1cca2e..a729083 100644
--- a/lib/Tiled2.hs
+++ b/lib/Tiled2.hs
@@ -10,22 +10,27 @@
-- those you should read the TMX documentation at
-- http://doc.mapeditor.org/en/latest/reference/tmx-map-format/
{-# LANGUAGE NamedFieldPuns #-}
+{-# LANGUAGE ScopedTypeVariables #-}
module Tiled2 where
-import Control.Applicative ((<|>))
-import Control.Monad (forM)
-import Data.Aeson hiding (Object)
-import qualified Data.Aeson as A
-import Data.Aeson.Types (Parser, typeMismatch)
-import qualified Data.ByteString.Lazy.Char8 as C8
-import Data.Functor ((<&>))
-import Data.Map (Map)
-import qualified Data.Map as M
-import Data.Maybe (fromMaybe)
-import Data.Text (Text)
-import Data.Vector (Vector)
-import GHC.Exts (fromList, toList)
-import GHC.Generics (Generic)
+import Control.Applicative ((<|>))
+import Control.Exception (try)
+import Control.Exception.Base (SomeException)
+import Control.Monad (forM)
+import Data.Aeson hiding (Object)
+import qualified Data.Aeson as A
+import Data.Aeson.Types (Parser, typeMismatch)
+import qualified Data.ByteString as BS
+import qualified Data.ByteString.Lazy as LB
+import Data.Either.Combinators (mapLeft)
+import Data.Functor ((<&>))
+import Data.Map (Map)
+import qualified Data.Map as M
+import Data.Maybe (fromMaybe)
+import Data.Text (Text)
+import Data.Vector (Vector)
+import GHC.Exts (fromList, toList)
+import GHC.Generics (Generic)
-- | A globally indexed identifier.
@@ -439,4 +444,8 @@ instance ToJSON Tiledmap where
-- | Load a Tiled map from the given 'FilePath'.
loadTiledmap :: FilePath -> IO (Either String Tiledmap)
-loadTiledmap = fmap eitherDecode . C8.readFile
+loadTiledmap path = do
+ res <- try (BS.readFile path)
+ pure $ case res of
+ Right file -> mapLeft ("Json decode error or not a Tiled map: " <>) . eitherDecode . LB.fromStrict $ file
+ Left (err :: SomeException) -> Left $ "IO Error: " <> show err