    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE RecordWildCards #-}
    module Hpack.Syntax.ParseDependencies where

    import           Data.Text (Text)
    import           Data.Bifunctor
    import           Data.Aeson.Config.FromValue

    data Parse k v = Parse {
      Parse k v -> Text -> Parser (k, v)
    parseString  :: Text -> Parser (k, v)
    , Parse k v -> Object -> Parser v
    parseListItem :: Object -> Parser v
    , Parse k v -> Value -> Parser v
    parseDictItem :: Value -> Parser v
    , Parse k v -> Text -> k
    parseName :: Text -> k
    }

    parseDependencies :: Parse k v -> Value -> Parser [(k, v)]
    parseDependencies :: Parse k v -> Value -> Parser [(k, v)]
    parseDependencies parse :: Parse k v
    parse@Parse{..} v :: Value
    v = case Value
    v of
      String s :: Text
    s -> (k, v) -> [(k, v)]
    forall (m :: * -> *) a. Monad m => a -> m a
    return ((k, v) -> [(k, v)]) -> Parser (k, v) -> Parser [(k, v)]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> Parser (k, v)
    parseString Text
    s
      Array xs :: Array
    xs -> (Value -> Parser (k, v)) -> Array -> Parser [(k, v)]
    forall a. (Value -> Parser a) -> Array -> Parser [a]
    parseArray (Parse k v -> Value -> Parser (k, v)
    forall k v. Parse k v -> Value -> Parser (k, v)
    buildToolFromValue Parse k v
    parse) Array
    xs
      Object o :: Object
    o -> ((Text, v) -> (k, v)) -> [(Text, v)] -> [(k, v)]
    forall a b. (a -> b) -> [a] -> [b]
    map ((Text -> k) -> (Text, v) -> (k, v)
    forall (p :: * -> * -> *) a b c.
    Bifunctor p =>
    (a -> b) -> p a c -> p b c
    first Text -> k
    parseName) ([(Text, v)] -> [(k, v)]) -> Parser [(Text, v)] -> Parser [(k, v)]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (Value -> Parser v) -> Object -> Parser [(Text, v)]
    forall a. (Value -> Parser a) -> Object -> Parser [(Text, a)]
    traverseObject Value -> Parser v
    parseDictItem Object
    o
      _ -> String -> Value -> Parser [(k, v)]
    forall a. String -> Value -> Parser a
    typeMismatch "Array, Object, or String" Value
    v

    buildToolFromValue :: Parse k v -> Value -> Parser (k, v)
    buildToolFromValue :: Parse k v -> Value -> Parser (k, v)
    buildToolFromValue Parse{..} v :: Value
    v = case Value
    v of
      String s :: Text
    s -> Text -> Parser (k, v)
    parseString Text
    s
      Object o :: Object
    o -> Object -> Parser (k, v)
    sourceDependency Object
    o
      _ -> String -> Value -> Parser (k, v)
    forall a. String -> Value -> Parser a
    typeMismatch "Object or String" Value
    v
      where
        sourceDependency :: Object -> Parser (k, v)
    sourceDependency o :: Object
    o = (,) (k -> v -> (k, v)) -> Parser k -> Parser (v -> (k, v))
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (Text -> k
    parseName (Text -> k) -> Parser Text -> Parser k
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Parser Text
    name) Parser (v -> (k, v)) -> Parser v -> Parser (k, v)
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object -> Parser v
    parseListItem Object
    o
          where
            name :: Parser Text
            name :: Parser Text
    name = Object
    o Object -> Text -> Parser Text
    forall a. FromValue a => Object -> Text -> Parser a
    .: "name"