From e68d652323e454abf7e6c01ecedd919859cf9274 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Thu, 30 Sep 2021 14:01:25 +0200 Subject: nicer json output which leaks less haskell names --- lib/CheckMap.hs | 53 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 21 deletions(-) (limited to 'lib/CheckMap.hs') diff --git a/lib/CheckMap.hs b/lib/CheckMap.hs index 176e3d5..49dcd2e 100644 --- a/lib/CheckMap.hs +++ b/lib/CheckMap.hs @@ -8,23 +8,26 @@ -- | Module that contains the high-level checking functions module CheckMap (loadAndLintMap, MapResult(..)) where -import Data.Aeson (ToJSON) -import Data.Map (Map, fromList, toList) -import Data.Maybe (mapMaybe) -import Data.Text (Text) -import qualified Data.Text as T -import qualified Data.Vector as V -import GHC.Generics (Generic) +import Data.Aeson (ToJSON) +import qualified Data.Aeson as A +import Data.Map (Map, fromList, toList) +import Data.Maybe (mapMaybe) +import Data.Text (Text) +import qualified Data.Text as T +import qualified Data.Vector as V +import GHC.Generics (Generic) -import LintWriter (LintResult (..), LintWriter, askContext, - filterLintLevel, lintToDep, resultToDeps, - resultToLints, resultToOffers, runLintWriter) -import Properties (checkLayerProperty, checkMap) -import Tiled2 (Layer (layerName, layerProperties), - Tiledmap (tiledmapLayers), loadTiledmap) -import Types (Dep, Level (..), Lint (..), hint, lintLevel) -import Util (PrettyPrint (prettyprint), prettyprint) +import Data.Aeson.Types ((.=)) +import LintWriter (LintResult (..), LintWriter, askContext, + filterLintLevel, lintToDep, resultToDeps, + resultToLints, resultToOffers, runLintWriter) +import Properties (checkLayerProperty, checkMap) +import Tiled2 (Layer (layerName, layerProperties), + LoadResult (..), Tiledmap (tiledmapLayers), + loadTiledmap) +import Types (Dep, Level (..), Lint (..), hint) +import Util (PrettyPrint (prettyprint), prettyprint) @@ -34,16 +37,23 @@ data MapResult = MapResult , mapresultGeneral :: [Lint] , mapresultDepends :: [Dep] , mapresultProvides :: [Text] - } deriving (Generic, ToJSON) - + } deriving (Generic) +instance ToJSON MapResult where + toJSON res = A.object + [ "layer" .= mapresultLayer res + , "general" .= mapresultGeneral res + -- TODO: not sure if these are necessary of even useful + , "depends" .= mapresultDepends res + , "provides" .= mapresultProvides res + ] -- | this module's raison d'ĂȘtre -- Lints the map at `path`, and limits local links to at most `depth` -- layers upwards in the file hierarchy -loadAndLintMap :: FilePath -> Int -> IO MapResult +loadAndLintMap :: FilePath -> Int -> IO (Maybe MapResult) loadAndLintMap path depth = loadTiledmap path >>= pure . \case - Left err -> MapResult + DecodeErr err -> Just $ MapResult { mapresultLayer = Nothing , mapresultDepends = [] , mapresultProvides = [] @@ -52,8 +62,9 @@ loadAndLintMap path depth = loadTiledmap path >>= pure . \case path <> ": Fatal: " <> err ] } - Right waMap -> - runLinter waMap depth + IOErr err -> Nothing + Loaded waMap -> + Just (runLinter waMap depth) -- | lint a loaded map runLinter :: Tiledmap -> Int -> MapResult -- cgit v1.2.3