{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE NoFieldSelectors  #-}
{-# LANGUAGE OverloadedStrings #-}

module Stack.Types.DownloadInfo
  ( DownloadInfo (..)
  , parseDownloadInfoFromObject
  ) where

import           Data.Aeson.Types ( FromJSON (..), Object )
import           Data.Aeson.WarningParser
                   ( WarningParser, WithJSONWarnings (..), (..:), (..:?)
                   , withObjectWarnings
                   )
import           Stack.Prelude

-- | Build of the compiler distribution (e.g. standard, gmp4, tinfo6)

-- | Information for a file to download.

data DownloadInfo = DownloadInfo
  { DownloadInfo -> Text
url :: Text
    -- ^ URL or absolute file path

  , DownloadInfo -> Maybe Int
contentLength :: Maybe Int
  , DownloadInfo -> Maybe ByteString
sha1 :: Maybe ByteString
  , DownloadInfo -> Maybe ByteString
sha256 :: Maybe ByteString
  }
  deriving Int -> DownloadInfo -> ShowS
[DownloadInfo] -> ShowS
DownloadInfo -> String
(Int -> DownloadInfo -> ShowS)
-> (DownloadInfo -> String)
-> ([DownloadInfo] -> ShowS)
-> Show DownloadInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DownloadInfo -> ShowS
showsPrec :: Int -> DownloadInfo -> ShowS
$cshow :: DownloadInfo -> String
show :: DownloadInfo -> String
$cshowList :: [DownloadInfo] -> ShowS
showList :: [DownloadInfo] -> ShowS
Show

instance FromJSON (WithJSONWarnings DownloadInfo) where
  parseJSON :: Value -> Parser (WithJSONWarnings DownloadInfo)
parseJSON = String
-> (Object -> WarningParser DownloadInfo)
-> Value
-> Parser (WithJSONWarnings DownloadInfo)
forall a.
String
-> (Object -> WarningParser a)
-> Value
-> Parser (WithJSONWarnings a)
withObjectWarnings String
"DownloadInfo" Object -> WarningParser DownloadInfo
parseDownloadInfoFromObject

-- | Parse JSON in existing object for 'DownloadInfo'

parseDownloadInfoFromObject :: Object -> WarningParser DownloadInfo
parseDownloadInfoFromObject :: Object -> WarningParser DownloadInfo
parseDownloadInfoFromObject Object
o = do
  Text
url <- Object
o Object -> Text -> WarningParser Text
forall a. FromJSON a => Object -> Text -> WarningParser a
..: Text
"url"
  Maybe Int
contentLength <- Object
o Object -> Text -> WarningParser (Maybe Int)
forall a. FromJSON a => Object -> Text -> WarningParser (Maybe a)
..:? Text
"content-length"
  Maybe Text
sha1TextMay <- Object
o Object -> Text -> WarningParser (Maybe Text)
forall a. FromJSON a => Object -> Text -> WarningParser (Maybe a)
..:? Text
"sha1"
  Maybe Text
sha256TextMay <- Object
o Object -> Text -> WarningParser (Maybe Text)
forall a. FromJSON a => Object -> Text -> WarningParser (Maybe a)
..:? Text
"sha256"
  let sha1 :: Maybe ByteString
sha1 = (Text -> ByteString) -> Maybe Text -> Maybe ByteString
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> ByteString
encodeUtf8 Maybe Text
sha1TextMay
      sha256 :: Maybe ByteString
sha256 = (Text -> ByteString) -> Maybe Text -> Maybe ByteString
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> ByteString
encodeUtf8 Maybe Text
sha256TextMay
  DownloadInfo -> WarningParser DownloadInfo
forall a. a -> WriterT WarningParserMonoid Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    DownloadInfo
    { Text
$sel:url:DownloadInfo :: Text
url :: Text
url
    , Maybe Int
$sel:contentLength:DownloadInfo :: Maybe Int
contentLength :: Maybe Int
contentLength
    , Maybe ByteString
$sel:sha1:DownloadInfo :: Maybe ByteString
sha1 :: Maybe ByteString
sha1
    , Maybe ByteString
$sel:sha256:DownloadInfo :: Maybe ByteString
sha256 :: Maybe ByteString
sha256
    }