diff --git a/package.yaml b/package.yaml index b2c184d..8679cc0 100644 --- a/package.yaml +++ b/package.yaml @@ -9,12 +9,13 @@ description: Please see the README on GitHub at = 4.7 && < 5 - OddWord >= 1.0 && < 1.1 - aeson + - aeson-pretty - bytestring - containers - text diff --git a/src/Mima/Vm/Storage.hs b/src/Mima/Vm/Storage.hs new file mode 100644 index 0000000..7dc98ec --- /dev/null +++ b/src/Mima/Vm/Storage.hs @@ -0,0 +1,39 @@ +module Mima.Vm.Storage + ( + -- * Methods for loading/storing Metadata + loadMetadata + , saveMetadata + -- * Test methods + , roundTripFile + ) where + +import Data.Aeson +import Data.Aeson.Encode.Pretty +import qualified Data.ByteString.Lazy as BSL +import qualified Data.Text as T +import Mima.Run +import Mima.Vm.Metadata + +-- | Loads 'Metadata' from a given file path +loadMetadata :: FilePath -> Run Metadata +loadMetadata path = do + file <- readFileBS path + let decoded = eitherDecode (BSL.fromStrict file) :: Either String Metadata + case decoded of + Left msg -> throw (T.pack msg) + Right metadata -> pure metadata + +-- | Stores prettified 'Metadata' in a given file. +saveMetadata :: FilePath -> Metadata -> Run () +saveMetadata path metadata = writeFileBS path (BSL.toStrict (encodePretty metadata)) + +-- | A garbage test method that resds the input file, parses it and writes the +-- prettified result back in the output file. +-- +-- Can be used with the example file: +-- +-- > roundTripFile "test/files/SimpleMetadataFile.json" "/tmp/test.json" +roundTripFile :: FilePath -- ^ The input file + -> FilePath -- ^ The output file + -> Run () +roundTripFile input output = loadMetadata input >>= saveMetadata output diff --git a/test/files/SimpleMetadataFile.json b/test/files/SimpleMetadataFile.json new file mode 100644 index 0000000..20246c0 --- /dev/null +++ b/test/files/SimpleMetadataFile.json @@ -0,0 +1,13 @@ +{ + "global": { + "some-data": "some value" + }, + "local": [ + { + "at": 20, + "info": { + "src-line": 200 + } + } + ] +}