Add simple load and store methods for Metadata

This commit is contained in:
I-Al-Istannen 2020-03-27 16:13:49 +01:00
parent c3584df5dc
commit ae7f0bdac4
3 changed files with 54 additions and 1 deletions

View file

@ -9,12 +9,13 @@ description: Please see the README on GitHub at <https://github.com/Garmelon/mim
github: Garmelon/mima-tools github: Garmelon/mima-tools
extra-source-files: extra-source-files:
- README.md - README.md
dependencies: dependencies:
- base >= 4.7 && < 5 - base >= 4.7 && < 5
- OddWord >= 1.0 && < 1.1 - OddWord >= 1.0 && < 1.1
- aeson - aeson
- aeson-pretty
- bytestring - bytestring
- containers - containers
- text - text

39
src/Mima/Vm/Storage.hs Normal file
View file

@ -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

View file

@ -0,0 +1,13 @@
{
"global": {
"some-data": "some value"
},
"local": [
{
"at": 20,
"info": {
"src-line": 200
}
}
]
}