{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE LambdaCase #-}
module Hpack.Yaml (
-- | /__NOTE:__/ This module is exposed to allow integration of Hpack into
-- other tools.  It is not meant for general use by end users.  The following
-- caveats apply:
--
-- * The API is undocumented, consult the source instead.
--
-- * The exposed types and functions primarily serve Hpack's own needs, not
-- that of a public API.  Breaking changes can happen as Hpack evolves.
--
-- As an Hpack user you either want to use the @hpack@ executable or a build
-- tool that supports Hpack (e.g. @stack@ or @cabal2nix@).

  decodeYaml
, module Data.Aeson.Config.FromValue
) where

import           Imports

import           Data.Yaml hiding (decodeFile, decodeFileWithWarnings)
import           Data.Yaml.Include
import           Data.Yaml.Internal (Warning(..))
import           Data.Aeson.Config.FromValue
import           Data.Aeson.Config.Parser (fromAesonPath, formatPath)

formatWarning :: FilePath -> Warning -> String
formatWarning :: FilePath -> Warning -> FilePath
formatWarning FilePath
file = \ case
  DuplicateKey JSONPath
path -> FilePath
file FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
": Duplicate field " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ JSONPath -> FilePath
formatPath (JSONPath -> JSONPath
fromAesonPath JSONPath
path)

decodeYaml :: FilePath -> IO (Either String ([String], Value))
decodeYaml :: FilePath -> IO (Either FilePath ([FilePath], Value))
decodeYaml FilePath
file = do
  Either ParseException ([Warning], Value)
result <- FilePath -> IO (Either ParseException ([Warning], Value))
forall a.
FromJSON a =>
FilePath -> IO (Either ParseException ([Warning], a))
decodeFileWithWarnings FilePath
file
  Either FilePath ([FilePath], Value)
-> IO (Either FilePath ([FilePath], Value))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either FilePath ([FilePath], Value)
 -> IO (Either FilePath ([FilePath], Value)))
-> Either FilePath ([FilePath], Value)
-> IO (Either FilePath ([FilePath], Value))
forall a b. (a -> b) -> a -> b
$ (ParseException -> Either FilePath ([FilePath], Value))
-> (([Warning], Value) -> Either FilePath ([FilePath], Value))
-> Either ParseException ([Warning], Value)
-> Either FilePath ([FilePath], Value)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (FilePath -> Either FilePath ([FilePath], Value)
forall a b. a -> Either a b
Left (FilePath -> Either FilePath ([FilePath], Value))
-> (ParseException -> FilePath)
-> ParseException
-> Either FilePath ([FilePath], Value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseException -> FilePath
errToString) (([FilePath], Value) -> Either FilePath ([FilePath], Value)
forall a b. b -> Either a b
Right (([FilePath], Value) -> Either FilePath ([FilePath], Value))
-> (([Warning], Value) -> ([FilePath], Value))
-> ([Warning], Value)
-> Either FilePath ([FilePath], Value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Warning] -> [FilePath])
-> ([Warning], Value) -> ([FilePath], Value)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ((Warning -> FilePath) -> [Warning] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map ((Warning -> FilePath) -> [Warning] -> [FilePath])
-> (Warning -> FilePath) -> [Warning] -> [FilePath]
forall a b. (a -> b) -> a -> b
$ FilePath -> Warning -> FilePath
formatWarning FilePath
file)) Either ParseException ([Warning], Value)
result
  where
    errToString :: ParseException -> FilePath
errToString ParseException
err = FilePath
file FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ case ParseException
err of
      AesonException FilePath
e -> FilePath
": " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
e
      InvalidYaml (Just (YamlException FilePath
s)) -> FilePath
": " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
s
      InvalidYaml (Just (YamlParseException{FilePath
YamlMark
yamlProblem :: YamlException -> FilePath
yamlContext :: YamlException -> FilePath
yamlProblemMark :: YamlException -> YamlMark
yamlProblemMark :: YamlMark
yamlContext :: FilePath
yamlProblem :: FilePath
..})) -> FilePath
":" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Int -> FilePath
forall a. Show a => a -> FilePath
show Int
yamlLine FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
":" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Int -> FilePath
forall a. Show a => a -> FilePath
show Int
yamlColumn FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
": " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
yamlProblem FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
" " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
yamlContext
        where YamlMark{Int
yamlIndex :: YamlMark -> Int
yamlLine :: YamlMark -> Int
yamlColumn :: YamlMark -> Int
yamlIndex :: Int
yamlColumn :: Int
yamlLine :: Int
..} = YamlMark
yamlProblemMark
      ParseException
_ -> FilePath
": " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ ParseException -> FilePath
forall a. Show a => a -> FilePath
show ParseException
err