-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT
{-# LANGUAGE TupleSections #-}

-- | Named configuration parsers at the YAML and JSON boundary.
module Ecluse.Config.Aeson (
    parseAppConfig,
    parseRulesPatch,
    parseRulePatch,
    parseRuleEntry,
) where

import Data.Aeson (Value (..), withObject, (.!=))
import Data.Aeson.Key qualified as Key
import Data.Aeson.KeyMap qualified as KeyMap
import Data.Aeson.Types (Parser, explicitParseFieldMaybe)
import Data.IP (IPRange)
import Data.Map.Strict qualified as Map
import Data.Scientific (Scientific, base10Exponent, toBoundedInteger)
import Data.Text qualified as T
import Data.Time (NominalDiffTime)

import Ecluse.Config.Parser
import Ecluse.Config.Rule
import Ecluse.Config.Types

import Ecluse.Core.BuildIdentity (parseOperatorContact)
import Ecluse.Core.Credential (Secret, mkSecret)
import Ecluse.Core.Ecosystem (Ecosystem (..), ecosystemName, parseEcosystem)
import Ecluse.Core.Osv.Provenance (SourcePathRecording (FingerprintSourcePath, RecordSourcePath))
import Ecluse.Core.Osv.Publication (Percent, mkPercent)
import Ecluse.Core.Package.Integrity (parseMinIntegrity, parseMinTrustedIntegrity)
import Ecluse.Core.Registry.Adapter (firstPartyFor, uploadEndpointFor)
import Ecluse.Core.Registry.Adapter.Capability (UploadEndpointRequirement (ExplicitUploadEndpoint))
import Ecluse.Core.Registry.FirstParty (FirstPartyDeclaration (FirstPartyDeclaration), FirstPartyNames (..))
import Ecluse.Core.Registry.Maintenance.Budget (parseQuotaDimension, parseRequestKind)
import Ecluse.Core.Security (parseBlockedRange)
import Ecluse.Core.Security.Egress (RegistryUrl)
import Ecluse.Core.Server.Encoding (MetadataCompression (GzipWhenAccepted, IdentityOnly))
import Ecluse.Core.Text (readDecimalText)
import Ecluse.Runtime.Log (parseLogFormat, parseLogLevel)
import Ecluse.Runtime.Telemetry (parseTelemetrySwitch)

-- A mount's refusals name the bare key, because "Ecluse.Config" reports the mount around them.
-- The ecosystem comes from the mounts key, so a per-ecosystem value reads in its own shape.
mountDecoder :: Ecosystem -> GroupDecoder MountConfig
mountDecoder :: Ecosystem -> GroupDecoder MountConfig
mountDecoder Ecosystem
eco =
    Maybe Bool
-> Maybe PrivateEndpoint
-> RegistryUrl
-> Maybe MirrorEndpoint
-> Maybe PublicationEndpoint
-> Maybe FirstParty
-> MountIntegrity
-> Maybe Int
-> MountConfig
MountConfig
        (Maybe Bool
 -> Maybe PrivateEndpoint
 -> RegistryUrl
 -> Maybe MirrorEndpoint
 -> Maybe PublicationEndpoint
 -> Maybe FirstParty
 -> MountIntegrity
 -> Maybe Int
 -> MountConfig)
-> GroupDecoder (Maybe Bool)
-> GroupDecoder
     (Maybe PrivateEndpoint
      -> RegistryUrl
      -> Maybe MirrorEndpoint
      -> Maybe PublicationEndpoint
      -> Maybe FirstParty
      -> MountIntegrity
      -> Maybe Int
      -> MountConfig)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> GroupDecoder (Maybe Bool)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"enabled"
        GroupDecoder
  (Maybe PrivateEndpoint
   -> RegistryUrl
   -> Maybe MirrorEndpoint
   -> Maybe PublicationEndpoint
   -> Maybe FirstParty
   -> MountIntegrity
   -> Maybe Int
   -> MountConfig)
-> GroupDecoder (Maybe PrivateEndpoint)
-> GroupDecoder
     (RegistryUrl
      -> Maybe MirrorEndpoint
      -> Maybe PublicationEndpoint
      -> Maybe FirstParty
      -> MountIntegrity
      -> Maybe Int
      -> MountConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser PrivateEndpoint)
-> GroupDecoder (Maybe PrivateEndpoint)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"privateUpstream" String -> Value -> Parser PrivateEndpoint
parseReadTarget
        GroupDecoder
  (RegistryUrl
   -> Maybe MirrorEndpoint
   -> Maybe PublicationEndpoint
   -> Maybe FirstParty
   -> MountIntegrity
   -> Maybe Int
   -> MountConfig)
-> GroupDecoder RegistryUrl
-> GroupDecoder
     (Maybe MirrorEndpoint
      -> Maybe PublicationEndpoint
      -> Maybe FirstParty
      -> MountIntegrity
      -> Maybe Int
      -> MountConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser RegistryUrl)
-> GroupDecoder RegistryUrl
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"publicUpstream" String -> Value -> Parser RegistryUrl
parsePublicUpstream
        GroupDecoder
  (Maybe MirrorEndpoint
   -> Maybe PublicationEndpoint
   -> Maybe FirstParty
   -> MountIntegrity
   -> Maybe Int
   -> MountConfig)
-> GroupDecoder (Maybe MirrorEndpoint)
-> GroupDecoder
     (Maybe PublicationEndpoint
      -> Maybe FirstParty -> MountIntegrity -> Maybe Int -> MountConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser MirrorEndpoint)
-> GroupDecoder (Maybe MirrorEndpoint)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"mirrorTarget" (GroupDecoder (Maybe RegistryUrl)
-> String -> Value -> Parser MirrorEndpoint
parseMirrorTarget GroupDecoder (Maybe RegistryUrl)
upload)
        GroupDecoder
  (Maybe PublicationEndpoint
   -> Maybe FirstParty -> MountIntegrity -> Maybe Int -> MountConfig)
-> GroupDecoder (Maybe PublicationEndpoint)
-> GroupDecoder
     (Maybe FirstParty -> MountIntegrity -> Maybe Int -> MountConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser PublicationEndpoint)
-> GroupDecoder (Maybe PublicationEndpoint)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"publicationTarget" (GroupDecoder (Maybe RegistryUrl)
-> String -> Value -> Parser PublicationEndpoint
parsePublicationTarget GroupDecoder (Maybe RegistryUrl)
upload)
        GroupDecoder
  (Maybe FirstParty -> MountIntegrity -> Maybe Int -> MountConfig)
-> GroupDecoder (Maybe FirstParty)
-> GroupDecoder (MountIntegrity -> Maybe Int -> MountConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser FirstParty)
-> GroupDecoder (Maybe FirstParty)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"firstParty" (Ecosystem -> String -> Value -> Parser FirstParty
parseFirstParty Ecosystem
eco)
        GroupDecoder (MountIntegrity -> Maybe Int -> MountConfig)
-> GroupDecoder MountIntegrity
-> GroupDecoder (Maybe Int -> MountConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser MountIntegrity)
-> GroupDecoder MountIntegrity
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"integrity" (String
-> GroupDecoder MountIntegrity
-> KeyMap Value
-> Parser MountIntegrity
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"integrity" GroupDecoder MountIntegrity
mountIntegrityDecoder)
        GroupDecoder (Maybe Int -> MountConfig)
-> GroupDecoder (Maybe Int) -> GroupDecoder MountConfig
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"maxRequestBytes" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder MountConfig
-> GroupDecoder () -> GroupDecoder MountConfig
forall a b. GroupDecoder a -> GroupDecoder b -> GroupDecoder a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Key -> GroupDecoder ()
unreadKey Key
"rules"
  where
    upload :: GroupDecoder (Maybe RegistryUrl)
upload = Ecosystem -> GroupDecoder (Maybe RegistryUrl)
uploadUrlKey Ecosystem
eco

-- A protocol with a separate upload endpoint requires uploadUrl on both write targets. On any
-- other protocol the key stays optional here, and "Ecluse.Config.Mirror" refuses it.
uploadUrlKey :: Ecosystem -> GroupDecoder (Maybe RegistryUrl)
uploadUrlKey :: Ecosystem -> GroupDecoder (Maybe RegistryUrl)
uploadUrlKey Ecosystem
eco
    | Ecosystem -> Maybe UploadEndpointRequirement
uploadEndpointFor Ecosystem
eco Maybe UploadEndpointRequirement
-> Maybe UploadEndpointRequirement -> Bool
forall a. Eq a => a -> a -> Bool
== UploadEndpointRequirement -> Maybe UploadEndpointRequirement
forall a. a -> Maybe a
Just UploadEndpointRequirement
ExplicitUploadEndpoint = RegistryUrl -> Maybe RegistryUrl
forall a. a -> Maybe a
Just (RegistryUrl -> Maybe RegistryUrl)
-> GroupDecoder RegistryUrl -> GroupDecoder (Maybe RegistryUrl)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> (String -> Value -> Parser RegistryUrl)
-> GroupDecoder RegistryUrl
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"uploadUrl" String -> Value -> Parser RegistryUrl
parseRegistryUrl
    | Bool
otherwise = Key
-> (String -> Value -> Parser RegistryUrl)
-> GroupDecoder (Maybe RegistryUrl)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"uploadUrl" String -> Value -> Parser RegistryUrl
parseRegistryUrl

-- Each endpoint admits exactly the tags and keys its cell of the store admission matrix names,
-- so a shape outside the cell has nothing to parse into and refuses with the key path.
parsePublicUpstream :: String -> Value -> Parser RegistryUrl
parsePublicUpstream :: String -> Value -> Parser RegistryUrl
parsePublicUpstream = [TagCase RegistryUrl] -> String -> Value -> Parser RegistryUrl
forall a. [TagCase a] -> String -> Value -> Parser a
taggedTarget [Key -> GroupDecoder RegistryUrl -> TagCase RegistryUrl
forall a. Key -> GroupDecoder a -> TagCase a
TagCase (StoreTag -> Key
tagKey StoreTag
TagRegistry) GroupDecoder RegistryUrl
targetUrl]

parseReadTarget :: String -> Value -> Parser PrivateEndpoint
parseReadTarget :: String -> Value -> Parser PrivateEndpoint
parseReadTarget = [TagCase PrivateEndpoint]
-> String -> Value -> Parser PrivateEndpoint
forall a. [TagCase a] -> String -> Value -> Parser a
taggedTarget [StoreTag -> TagCase PrivateEndpoint
readCase StoreTag
TagRegistry, StoreTag -> TagCase PrivateEndpoint
readCase StoreTag
TagCodeArtifact, TagCase PrivateEndpoint
verdaccio]
  where
    readCase :: StoreTag -> TagCase PrivateEndpoint
readCase StoreTag
tag = Key -> GroupDecoder PrivateEndpoint -> TagCase PrivateEndpoint
forall a. Key -> GroupDecoder a -> TagCase a
TagCase (StoreTag -> Key
tagKey StoreTag
tag) (Target -> Maybe Secret -> DeletionConsent -> PrivateEndpoint
PrivateEndpoint (Target -> Maybe Secret -> DeletionConsent -> PrivateEndpoint)
-> (RegistryUrl -> Target)
-> RegistryUrl
-> Maybe Secret
-> DeletionConsent
-> PrivateEndpoint
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StoreTag -> RegistryUrl -> Target
Target StoreTag
tag (RegistryUrl -> Maybe Secret -> DeletionConsent -> PrivateEndpoint)
-> GroupDecoder RegistryUrl
-> GroupDecoder
     (Maybe Secret -> DeletionConsent -> PrivateEndpoint)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GroupDecoder RegistryUrl
targetUrl GroupDecoder (Maybe Secret -> DeletionConsent -> PrivateEndpoint)
-> GroupDecoder (Maybe Secret)
-> GroupDecoder (DeletionConsent -> PrivateEndpoint)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe Secret -> GroupDecoder (Maybe Secret)
forall a. a -> GroupDecoder a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Secret
forall a. Maybe a
Nothing GroupDecoder (DeletionConsent -> PrivateEndpoint)
-> GroupDecoder DeletionConsent -> GroupDecoder PrivateEndpoint
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> DeletionConsent -> GroupDecoder DeletionConsent
forall a. a -> GroupDecoder a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DeletionConsent
DeletionWithheld)
    verdaccio :: TagCase PrivateEndpoint
verdaccio = Key -> GroupDecoder PrivateEndpoint -> TagCase PrivateEndpoint
forall a. Key -> GroupDecoder a -> TagCase a
TagCase (StoreTag -> Key
tagKey StoreTag
TagVerdaccio) (Target -> Maybe Secret -> DeletionConsent -> PrivateEndpoint
PrivateEndpoint (Target -> Maybe Secret -> DeletionConsent -> PrivateEndpoint)
-> (RegistryUrl -> Target)
-> RegistryUrl
-> Maybe Secret
-> DeletionConsent
-> PrivateEndpoint
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StoreTag -> RegistryUrl -> Target
Target StoreTag
TagVerdaccio (RegistryUrl -> Maybe Secret -> DeletionConsent -> PrivateEndpoint)
-> GroupDecoder RegistryUrl
-> GroupDecoder
     (Maybe Secret -> DeletionConsent -> PrivateEndpoint)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GroupDecoder RegistryUrl
targetUrl GroupDecoder (Maybe Secret -> DeletionConsent -> PrivateEndpoint)
-> GroupDecoder (Maybe Secret)
-> GroupDecoder (DeletionConsent -> PrivateEndpoint)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser Secret)
-> GroupDecoder (Maybe Secret)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"token" String -> Value -> Parser Secret
parseSecret GroupDecoder (DeletionConsent -> PrivateEndpoint)
-> GroupDecoder DeletionConsent -> GroupDecoder PrivateEndpoint
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GroupDecoder DeletionConsent
deletionConsent)

parseMirrorTarget :: GroupDecoder (Maybe RegistryUrl) -> String -> Value -> Parser MirrorEndpoint
parseMirrorTarget :: GroupDecoder (Maybe RegistryUrl)
-> String -> Value -> Parser MirrorEndpoint
parseMirrorTarget GroupDecoder (Maybe RegistryUrl)
upload =
    [TagCase MirrorEndpoint]
-> String -> Value -> Parser MirrorEndpoint
forall a. [TagCase a] -> String -> Value -> Parser a
taggedTarget
        [ Key -> GroupDecoder MirrorEndpoint -> TagCase MirrorEndpoint
forall a. Key -> GroupDecoder a -> TagCase a
TagCase (StoreTag -> Key
tagKey StoreTag
TagRegistry) (GroupDecoder MirrorWrite -> GroupDecoder MirrorEndpoint
mirrorEndpoint (Secret -> MirrorWrite
WriteRegistry (Secret -> MirrorWrite)
-> GroupDecoder Secret -> GroupDecoder MirrorWrite
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GroupDecoder Secret
writeToken))
        , Key -> GroupDecoder MirrorEndpoint -> TagCase MirrorEndpoint
forall a. Key -> GroupDecoder a -> TagCase a
TagCase (StoreTag -> Key
tagKey StoreTag
TagCodeArtifact) (GroupDecoder MirrorWrite -> GroupDecoder MirrorEndpoint
mirrorEndpoint (Maybe Natural -> MirrorWrite
WriteCodeArtifact (Maybe Natural -> MirrorWrite)
-> GroupDecoder (Maybe Natural) -> GroupDecoder MirrorWrite
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GroupDecoder (Maybe Natural)
mintLifetime))
        , Key -> GroupDecoder MirrorEndpoint -> TagCase MirrorEndpoint
forall a. Key -> GroupDecoder a -> TagCase a
TagCase (StoreTag -> Key
tagKey StoreTag
TagVerdaccio) (GroupDecoder MirrorWrite -> GroupDecoder MirrorEndpoint
mirrorEndpoint (Secret -> DeletionConsent -> MirrorWrite
WriteVerdaccio (Secret -> DeletionConsent -> MirrorWrite)
-> GroupDecoder Secret
-> GroupDecoder (DeletionConsent -> MirrorWrite)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GroupDecoder Secret
writeToken GroupDecoder (DeletionConsent -> MirrorWrite)
-> GroupDecoder DeletionConsent -> GroupDecoder MirrorWrite
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GroupDecoder DeletionConsent
deletionConsent))
        ]
  where
    mirrorEndpoint :: GroupDecoder MirrorWrite -> GroupDecoder MirrorEndpoint
mirrorEndpoint GroupDecoder MirrorWrite
write = RegistryUrl
-> MirrorWrite -> Maybe RegistryUrl -> Maybe Text -> MirrorEndpoint
MirrorEndpoint (RegistryUrl
 -> MirrorWrite
 -> Maybe RegistryUrl
 -> Maybe Text
 -> MirrorEndpoint)
-> GroupDecoder RegistryUrl
-> GroupDecoder
     (MirrorWrite -> Maybe RegistryUrl -> Maybe Text -> MirrorEndpoint)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GroupDecoder RegistryUrl
targetUrl GroupDecoder
  (MirrorWrite -> Maybe RegistryUrl -> Maybe Text -> MirrorEndpoint)
-> GroupDecoder MirrorWrite
-> GroupDecoder (Maybe RegistryUrl -> Maybe Text -> MirrorEndpoint)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GroupDecoder MirrorWrite
write GroupDecoder (Maybe RegistryUrl -> Maybe Text -> MirrorEndpoint)
-> GroupDecoder (Maybe RegistryUrl)
-> GroupDecoder (Maybe Text -> MirrorEndpoint)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GroupDecoder (Maybe RegistryUrl)
upload GroupDecoder (Maybe Text -> MirrorEndpoint)
-> GroupDecoder (Maybe Text) -> GroupDecoder MirrorEndpoint
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Text -> Parser Text) -> GroupDecoder (Maybe Text)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"username" String -> Text -> Parser Text
parseCredentialUsername
    writeToken :: GroupDecoder Secret
writeToken = Key -> (String -> Value -> Parser Secret) -> GroupDecoder Secret
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"token" String -> Value -> Parser Secret
parseSecret
    mintLifetime :: GroupDecoder (Maybe Natural)
mintLifetime = Key
-> (String -> Value -> Parser Natural)
-> GroupDecoder (Maybe Natural)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"tokenDuration" String -> Value -> Parser Natural
parseCodeArtifactDuration

parsePublicationTarget :: GroupDecoder (Maybe RegistryUrl) -> String -> Value -> Parser PublicationEndpoint
parsePublicationTarget :: GroupDecoder (Maybe RegistryUrl)
-> String -> Value -> Parser PublicationEndpoint
parsePublicationTarget GroupDecoder (Maybe RegistryUrl)
upload = [TagCase PublicationEndpoint]
-> String -> Value -> Parser PublicationEndpoint
forall a. [TagCase a] -> String -> Value -> Parser a
taggedTarget ((StoreTag -> TagCase PublicationEndpoint)
-> [StoreTag] -> [TagCase PublicationEndpoint]
forall a b. (a -> b) -> [a] -> [b]
map StoreTag -> TagCase PublicationEndpoint
publishCase [StoreTag
TagRegistry, StoreTag
TagCodeArtifact, StoreTag
TagVerdaccio])
  where
    publishCase :: StoreTag -> TagCase PublicationEndpoint
publishCase StoreTag
tag =
        Key
-> GroupDecoder PublicationEndpoint -> TagCase PublicationEndpoint
forall a. Key -> GroupDecoder a -> TagCase a
TagCase (StoreTag -> Key
tagKey StoreTag
tag) (Target
-> Maybe RegistryUrl
-> Maybe Secret
-> Maybe Text
-> PublicationEndpoint
PublicationEndpoint (Target
 -> Maybe RegistryUrl
 -> Maybe Secret
 -> Maybe Text
 -> PublicationEndpoint)
-> (RegistryUrl -> Target)
-> RegistryUrl
-> Maybe RegistryUrl
-> Maybe Secret
-> Maybe Text
-> PublicationEndpoint
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StoreTag -> RegistryUrl -> Target
Target StoreTag
tag (RegistryUrl
 -> Maybe RegistryUrl
 -> Maybe Secret
 -> Maybe Text
 -> PublicationEndpoint)
-> GroupDecoder RegistryUrl
-> GroupDecoder
     (Maybe RegistryUrl
      -> Maybe Secret -> Maybe Text -> PublicationEndpoint)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GroupDecoder RegistryUrl
targetUrl GroupDecoder
  (Maybe RegistryUrl
   -> Maybe Secret -> Maybe Text -> PublicationEndpoint)
-> GroupDecoder (Maybe RegistryUrl)
-> GroupDecoder (Maybe Secret -> Maybe Text -> PublicationEndpoint)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GroupDecoder (Maybe RegistryUrl)
upload GroupDecoder (Maybe Secret -> Maybe Text -> PublicationEndpoint)
-> GroupDecoder (Maybe Secret)
-> GroupDecoder (Maybe Text -> PublicationEndpoint)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser Secret)
-> GroupDecoder (Maybe Secret)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"token" String -> Value -> Parser Secret
parseSecret GroupDecoder (Maybe Text -> PublicationEndpoint)
-> GroupDecoder (Maybe Text) -> GroupDecoder PublicationEndpoint
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Text -> Parser Text) -> GroupDecoder (Maybe Text)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"username" String -> Text -> Parser Text
parseCredentialUsername)

parseCredentialUsername :: String -> Text -> Parser Text
parseCredentialUsername :: String -> Text -> Parser Text
parseCredentialUsername String
field Text
username
    | (Char -> Bool) -> Text -> Bool
T.any (Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Char
':', Char
'\r', Char
'\n', Char
'\0']) Text
username = String -> Parser Text
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
field String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" contains a Basic credential delimiter")
    | Bool
otherwise = Text -> Parser Text
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
username

-- The one key every tag admits, refined by the egress boundary's own constructor.
targetUrl :: GroupDecoder RegistryUrl
targetUrl :: GroupDecoder RegistryUrl
targetUrl = Key
-> (String -> Value -> Parser RegistryUrl)
-> GroupDecoder RegistryUrl
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"url" String -> Value -> Parser RegistryUrl
parseRegistryUrl

-- Unwritten, the Dredger holds no consent to delete from the store.
deletionConsent :: GroupDecoder DeletionConsent
deletionConsent :: GroupDecoder DeletionConsent
deletionConsent = Key
-> Bool
-> (String -> Bool -> Parser DeletionConsent)
-> GroupDecoder DeletionConsent
forall b a.
FromJSON b =>
Key -> b -> (String -> b -> Parser a) -> GroupDecoder a
optionalKeyOr Key
"permitDeletion" Bool
False ((Bool -> Parser DeletionConsent)
-> String -> Bool -> Parser DeletionConsent
forall a b. a -> b -> a
const (DeletionConsent -> Parser DeletionConsent
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DeletionConsent -> Parser DeletionConsent)
-> (Bool -> DeletionConsent) -> Bool -> Parser DeletionConsent
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> DeletionConsent
consentOf))
  where
    consentOf :: Bool -> DeletionConsent
consentOf Bool
granted = if Bool
granted then DeletionConsent
DeletionPermitted else DeletionConsent
DeletionWithheld

tagKey :: StoreTag -> Key.Key
tagKey :: StoreTag -> Key
tagKey = Text -> Key
Key.fromText (Text -> Key) -> (StoreTag -> Text) -> StoreTag -> Key
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StoreTag -> Text
storeTagName

-- An absent mount integrity object inherits the global trusted floor.
mountIntegrityDecoder :: GroupDecoder MountIntegrity
mountIntegrityDecoder :: GroupDecoder MountIntegrity
mountIntegrityDecoder =
    Maybe MinTrustedIntegrity -> MountIntegrity
MountIntegrity
        (Maybe MinTrustedIntegrity -> MountIntegrity)
-> GroupDecoder (Maybe MinTrustedIntegrity)
-> GroupDecoder MountIntegrity
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> (String -> Value -> Parser MinTrustedIntegrity)
-> GroupDecoder (Maybe MinTrustedIntegrity)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"minTrusted" ((Text -> Either Text MinTrustedIntegrity)
-> String -> Value -> Parser MinTrustedIntegrity
forall a. (Text -> Either Text a) -> String -> Value -> Parser a
parseEnum Text -> Either Text MinTrustedIntegrity
parseMinTrustedIntegrity)
        GroupDecoder MountIntegrity
-> GroupDecoder (Maybe ()) -> GroupDecoder MountIntegrity
forall a b. GroupDecoder a -> GroupDecoder b -> GroupDecoder a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Key -> (String -> Value -> Parser ()) -> GroupDecoder (Maybe ())
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"divergencePolicy" String -> Value -> Parser ()
parseLegacyDivergencePolicy

-- | Parse settings while leaving rule resolution to "Ecluse.Config".
parseAppConfig :: Value -> Parser AppConfig
parseAppConfig :: Value -> Parser AppConfig
parseAppConfig = String
-> (KeyMap Value -> Parser AppConfig) -> Value -> Parser AppConfig
forall a. String -> (KeyMap Value -> Parser a) -> Value -> Parser a
withObject String
"AppConfig" (String
-> GroupDecoder AppConfig -> KeyMap Value -> Parser AppConfig
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"document" GroupDecoder AppConfig
documentDecoder)

-- Rule overlays resolve separately against their own layer.
documentDecoder :: GroupDecoder AppConfig
documentDecoder :: GroupDecoder AppConfig
documentDecoder =
    ServerSettings
-> QueueSettings
-> LimitsSettings
-> CacheSettings
-> IntegritySettings
-> EgressSettings
-> AdvisoriesSettings
-> Maybe AdvisoryStoreUrl
-> RuntimeSettings
-> ObservabilitySettings
-> DredgerSettings
-> Map Ecosystem MountConfig
-> AppConfig
AppConfig
        (ServerSettings
 -> QueueSettings
 -> LimitsSettings
 -> CacheSettings
 -> IntegritySettings
 -> EgressSettings
 -> AdvisoriesSettings
 -> Maybe AdvisoryStoreUrl
 -> RuntimeSettings
 -> ObservabilitySettings
 -> DredgerSettings
 -> Map Ecosystem MountConfig
 -> AppConfig)
-> GroupDecoder ServerSettings
-> GroupDecoder
     (QueueSettings
      -> LimitsSettings
      -> CacheSettings
      -> IntegritySettings
      -> EgressSettings
      -> AdvisoriesSettings
      -> Maybe AdvisoryStoreUrl
      -> RuntimeSettings
      -> ObservabilitySettings
      -> DredgerSettings
      -> Map Ecosystem MountConfig
      -> AppConfig)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> (KeyMap Value -> Parser ServerSettings)
-> GroupDecoder ServerSettings
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"server" (String
-> GroupDecoder ServerSettings
-> KeyMap Value
-> Parser ServerSettings
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"server" GroupDecoder ServerSettings
serverDecoder)
        GroupDecoder
  (QueueSettings
   -> LimitsSettings
   -> CacheSettings
   -> IntegritySettings
   -> EgressSettings
   -> AdvisoriesSettings
   -> Maybe AdvisoryStoreUrl
   -> RuntimeSettings
   -> ObservabilitySettings
   -> DredgerSettings
   -> Map Ecosystem MountConfig
   -> AppConfig)
-> GroupDecoder QueueSettings
-> GroupDecoder
     (LimitsSettings
      -> CacheSettings
      -> IntegritySettings
      -> EgressSettings
      -> AdvisoriesSettings
      -> Maybe AdvisoryStoreUrl
      -> RuntimeSettings
      -> ObservabilitySettings
      -> DredgerSettings
      -> Map Ecosystem MountConfig
      -> AppConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser QueueSettings)
-> GroupDecoder QueueSettings
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"queue" (String
-> GroupDecoder QueueSettings
-> KeyMap Value
-> Parser QueueSettings
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"queue" GroupDecoder QueueSettings
queueDecoder)
        GroupDecoder
  (LimitsSettings
   -> CacheSettings
   -> IntegritySettings
   -> EgressSettings
   -> AdvisoriesSettings
   -> Maybe AdvisoryStoreUrl
   -> RuntimeSettings
   -> ObservabilitySettings
   -> DredgerSettings
   -> Map Ecosystem MountConfig
   -> AppConfig)
-> GroupDecoder LimitsSettings
-> GroupDecoder
     (CacheSettings
      -> IntegritySettings
      -> EgressSettings
      -> AdvisoriesSettings
      -> Maybe AdvisoryStoreUrl
      -> RuntimeSettings
      -> ObservabilitySettings
      -> DredgerSettings
      -> Map Ecosystem MountConfig
      -> AppConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser LimitsSettings)
-> GroupDecoder LimitsSettings
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"limits" (String
-> GroupDecoder LimitsSettings
-> KeyMap Value
-> Parser LimitsSettings
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"limits" GroupDecoder LimitsSettings
limitsDecoder)
        GroupDecoder
  (CacheSettings
   -> IntegritySettings
   -> EgressSettings
   -> AdvisoriesSettings
   -> Maybe AdvisoryStoreUrl
   -> RuntimeSettings
   -> ObservabilitySettings
   -> DredgerSettings
   -> Map Ecosystem MountConfig
   -> AppConfig)
-> GroupDecoder CacheSettings
-> GroupDecoder
     (IntegritySettings
      -> EgressSettings
      -> AdvisoriesSettings
      -> Maybe AdvisoryStoreUrl
      -> RuntimeSettings
      -> ObservabilitySettings
      -> DredgerSettings
      -> Map Ecosystem MountConfig
      -> AppConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser CacheSettings)
-> GroupDecoder CacheSettings
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"cache" (String
-> GroupDecoder CacheSettings
-> KeyMap Value
-> Parser CacheSettings
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"cache" GroupDecoder CacheSettings
cacheDecoder)
        GroupDecoder
  (IntegritySettings
   -> EgressSettings
   -> AdvisoriesSettings
   -> Maybe AdvisoryStoreUrl
   -> RuntimeSettings
   -> ObservabilitySettings
   -> DredgerSettings
   -> Map Ecosystem MountConfig
   -> AppConfig)
-> GroupDecoder IntegritySettings
-> GroupDecoder
     (EgressSettings
      -> AdvisoriesSettings
      -> Maybe AdvisoryStoreUrl
      -> RuntimeSettings
      -> ObservabilitySettings
      -> DredgerSettings
      -> Map Ecosystem MountConfig
      -> AppConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser IntegritySettings)
-> GroupDecoder IntegritySettings
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"integrity" (String
-> GroupDecoder IntegritySettings
-> KeyMap Value
-> Parser IntegritySettings
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"integrity" GroupDecoder IntegritySettings
integrityDecoder)
        GroupDecoder
  (EgressSettings
   -> AdvisoriesSettings
   -> Maybe AdvisoryStoreUrl
   -> RuntimeSettings
   -> ObservabilitySettings
   -> DredgerSettings
   -> Map Ecosystem MountConfig
   -> AppConfig)
-> GroupDecoder EgressSettings
-> GroupDecoder
     (AdvisoriesSettings
      -> Maybe AdvisoryStoreUrl
      -> RuntimeSettings
      -> ObservabilitySettings
      -> DredgerSettings
      -> Map Ecosystem MountConfig
      -> AppConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser EgressSettings)
-> GroupDecoder EgressSettings
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"egress" (String
-> GroupDecoder EgressSettings
-> KeyMap Value
-> Parser EgressSettings
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"egress" GroupDecoder EgressSettings
egressDecoder)
        GroupDecoder
  (AdvisoriesSettings
   -> Maybe AdvisoryStoreUrl
   -> RuntimeSettings
   -> ObservabilitySettings
   -> DredgerSettings
   -> Map Ecosystem MountConfig
   -> AppConfig)
-> GroupDecoder AdvisoriesSettings
-> GroupDecoder
     (Maybe AdvisoryStoreUrl
      -> RuntimeSettings
      -> ObservabilitySettings
      -> DredgerSettings
      -> Map Ecosystem MountConfig
      -> AppConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser AdvisoriesSettings)
-> GroupDecoder AdvisoriesSettings
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"advisories" (String
-> GroupDecoder AdvisoriesSettings
-> KeyMap Value
-> Parser AdvisoriesSettings
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"advisories" GroupDecoder AdvisoriesSettings
advisoriesDecoder)
        GroupDecoder
  (Maybe AdvisoryStoreUrl
   -> RuntimeSettings
   -> ObservabilitySettings
   -> DredgerSettings
   -> Map Ecosystem MountConfig
   -> AppConfig)
-> GroupDecoder (Maybe AdvisoryStoreUrl)
-> GroupDecoder
     (RuntimeSettings
      -> ObservabilitySettings
      -> DredgerSettings
      -> Map Ecosystem MountConfig
      -> AppConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser (Maybe AdvisoryStoreUrl))
-> GroupDecoder (Maybe AdvisoryStoreUrl)
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"mirrorState" (String
-> GroupDecoder (Maybe AdvisoryStoreUrl)
-> KeyMap Value
-> Parser (Maybe AdvisoryStoreUrl)
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"mirrorState" (Key
-> (String -> Value -> Parser AdvisoryStoreUrl)
-> GroupDecoder (Maybe AdvisoryStoreUrl)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"url" String -> Value -> Parser AdvisoryStoreUrl
parseAdvisoryStoreUrl))
        GroupDecoder
  (RuntimeSettings
   -> ObservabilitySettings
   -> DredgerSettings
   -> Map Ecosystem MountConfig
   -> AppConfig)
-> GroupDecoder RuntimeSettings
-> GroupDecoder
     (ObservabilitySettings
      -> DredgerSettings -> Map Ecosystem MountConfig -> AppConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser RuntimeSettings)
-> GroupDecoder RuntimeSettings
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"runtime" (String
-> GroupDecoder RuntimeSettings
-> KeyMap Value
-> Parser RuntimeSettings
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"runtime" GroupDecoder RuntimeSettings
runtimeDecoder)
        GroupDecoder
  (ObservabilitySettings
   -> DredgerSettings -> Map Ecosystem MountConfig -> AppConfig)
-> GroupDecoder ObservabilitySettings
-> GroupDecoder
     (DredgerSettings -> Map Ecosystem MountConfig -> AppConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser ObservabilitySettings)
-> GroupDecoder ObservabilitySettings
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"observability" (String
-> GroupDecoder ObservabilitySettings
-> KeyMap Value
-> Parser ObservabilitySettings
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"observability" GroupDecoder ObservabilitySettings
observabilityDecoder)
        GroupDecoder
  (DredgerSettings -> Map Ecosystem MountConfig -> AppConfig)
-> GroupDecoder DredgerSettings
-> GroupDecoder (Map Ecosystem MountConfig -> AppConfig)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser DredgerSettings)
-> GroupDecoder DredgerSettings
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"dredger" (String
-> GroupDecoder DredgerSettings
-> KeyMap Value
-> Parser DredgerSettings
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"dredger" GroupDecoder DredgerSettings
dredgerDecoder)
        GroupDecoder (Map Ecosystem MountConfig -> AppConfig)
-> GroupDecoder (Map Ecosystem MountConfig)
-> GroupDecoder AppConfig
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> KeyMap Value
-> (String -> KeyMap Value -> Parser (Map Ecosystem MountConfig))
-> GroupDecoder (Map Ecosystem MountConfig)
forall b a.
FromJSON b =>
Key -> b -> (String -> b -> Parser a) -> GroupDecoder a
optionalKeyOr Key
"mounts" KeyMap Value
forall a. Monoid a => a
mempty ((KeyMap Value -> Parser (Map Ecosystem MountConfig))
-> String -> KeyMap Value -> Parser (Map Ecosystem MountConfig)
forall a b. a -> b -> a
const KeyMap Value -> Parser (Map Ecosystem MountConfig)
parseMounts)
        GroupDecoder AppConfig -> GroupDecoder () -> GroupDecoder AppConfig
forall a b. GroupDecoder a -> GroupDecoder b -> GroupDecoder a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Key -> GroupDecoder ()
unreadKey Key
"rules"

serverDecoder :: GroupDecoder ServerSettings
serverDecoder :: GroupDecoder ServerSettings
serverDecoder =
    Int
-> Maybe Url
-> Maybe Secret
-> Maybe Text
-> Int
-> MetadataCompression
-> ServerSettings
ServerSettings
        (Int
 -> Maybe Url
 -> Maybe Secret
 -> Maybe Text
 -> Int
 -> MetadataCompression
 -> ServerSettings)
-> GroupDecoder Int
-> GroupDecoder
     (Maybe Url
      -> Maybe Secret
      -> Maybe Text
      -> Int
      -> MetadataCompression
      -> ServerSettings)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> (String -> Int -> Parser Int) -> GroupDecoder Int
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"port" String -> Int -> Parser Int
parsePort
        GroupDecoder
  (Maybe Url
   -> Maybe Secret
   -> Maybe Text
   -> Int
   -> MetadataCompression
   -> ServerSettings)
-> GroupDecoder (Maybe Url)
-> GroupDecoder
     (Maybe Secret
      -> Maybe Text -> Int -> MetadataCompression -> ServerSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Value -> Parser Url) -> GroupDecoder (Maybe Url)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"publicUrl" String -> Value -> Parser Url
parseHttpUrl
        GroupDecoder
  (Maybe Secret
   -> Maybe Text -> Int -> MetadataCompression -> ServerSettings)
-> GroupDecoder (Maybe Secret)
-> GroupDecoder
     (Maybe Text -> Int -> MetadataCompression -> ServerSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser Secret)
-> GroupDecoder (Maybe Secret)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"authToken" String -> Value -> Parser Secret
parseSecret
        GroupDecoder
  (Maybe Text -> Int -> MetadataCompression -> ServerSettings)
-> GroupDecoder (Maybe Text)
-> GroupDecoder (Int -> MetadataCompression -> ServerSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Text)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"helpMessage"
        GroupDecoder (Int -> MetadataCompression -> ServerSettings)
-> GroupDecoder Int
-> GroupDecoder (MetadataCompression -> ServerSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder Int
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"shutdownDrainTimeout" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder (MetadataCompression -> ServerSettings)
-> GroupDecoder MetadataCompression -> GroupDecoder ServerSettings
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Bool -> Parser MetadataCompression)
-> GroupDecoder MetadataCompression
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"metadataGzip" (\String
_ Bool
enabled -> MetadataCompression -> Parser MetadataCompression
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (if Bool
enabled then MetadataCompression
GzipWhenAccepted else MetadataCompression
IdentityOnly))

queueDecoder :: GroupDecoder QueueSettings
queueDecoder :: GroupDecoder QueueSettings
queueDecoder =
    Maybe QueueUrl -> Maybe Int -> Int -> QueueSettings
QueueSettings
        (Maybe QueueUrl -> Maybe Int -> Int -> QueueSettings)
-> GroupDecoder (Maybe QueueUrl)
-> GroupDecoder (Maybe Int -> Int -> QueueSettings)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> (String -> Value -> Parser QueueUrl)
-> GroupDecoder (Maybe QueueUrl)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"url" String -> Value -> Parser QueueUrl
parseQueueUrl
        GroupDecoder (Maybe Int -> Int -> QueueSettings)
-> GroupDecoder (Maybe Int) -> GroupDecoder (Int -> QueueSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"maxMemoryDepth" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder (Int -> QueueSettings)
-> GroupDecoder Int -> GroupDecoder QueueSettings
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder Int
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"maxReceiveCount" String -> Int -> Parser Int
parsePositiveInt

limitsDecoder :: GroupDecoder LimitsSettings
limitsDecoder :: GroupDecoder LimitsSettings
limitsDecoder =
    Maybe Int
-> Int
-> Int
-> Int
-> Maybe Int
-> Maybe Int
-> Int
-> Int
-> LimitsSettings
LimitsSettings
        (Maybe Int
 -> Int
 -> Int
 -> Int
 -> Maybe Int
 -> Maybe Int
 -> Int
 -> Int
 -> LimitsSettings)
-> GroupDecoder (Maybe Int)
-> GroupDecoder
     (Int
      -> Int
      -> Int
      -> Maybe Int
      -> Maybe Int
      -> Int
      -> Int
      -> LimitsSettings)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"maxResponseBytes" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder
  (Int
   -> Int
   -> Int
   -> Maybe Int
   -> Maybe Int
   -> Int
   -> Int
   -> LimitsSettings)
-> GroupDecoder Int
-> GroupDecoder
     (Int
      -> Int -> Maybe Int -> Maybe Int -> Int -> Int -> LimitsSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder Int
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"maxVersionCount" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder
  (Int
   -> Int -> Maybe Int -> Maybe Int -> Int -> Int -> LimitsSettings)
-> GroupDecoder Int
-> GroupDecoder
     (Int -> Maybe Int -> Maybe Int -> Int -> Int -> LimitsSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder Int
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"maxNestingDepth" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder
  (Int -> Maybe Int -> Maybe Int -> Int -> Int -> LimitsSettings)
-> GroupDecoder Int
-> GroupDecoder
     (Maybe Int -> Maybe Int -> Int -> Int -> LimitsSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder Int
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"maxAdvisoryDatabaseBytes" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder
  (Maybe Int -> Maybe Int -> Int -> Int -> LimitsSettings)
-> GroupDecoder (Maybe Int)
-> GroupDecoder (Maybe Int -> Int -> Int -> LimitsSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"maxRequestBytes" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder (Maybe Int -> Int -> Int -> LimitsSettings)
-> GroupDecoder (Maybe Int)
-> GroupDecoder (Int -> Int -> LimitsSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"maxArtifactBytes" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder (Int -> Int -> LimitsSettings)
-> GroupDecoder Int -> GroupDecoder (Int -> LimitsSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder Int
forall a. FromJSON a => Key -> GroupDecoder a
plainKey Key
"progressWindow"
        GroupDecoder (Int -> LimitsSettings)
-> GroupDecoder Int -> GroupDecoder LimitsSettings
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder Int
forall a. FromJSON a => Key -> GroupDecoder a
plainKey Key
"minProgressBytes"

cacheDecoder :: GroupDecoder CacheSettings
cacheDecoder :: GroupDecoder CacheSettings
cacheDecoder =
    NominalDiffTime -> Maybe Int -> Maybe Int -> CacheSettings
CacheSettings
        (NominalDiffTime -> Maybe Int -> Maybe Int -> CacheSettings)
-> GroupDecoder NominalDiffTime
-> GroupDecoder (Maybe Int -> Maybe Int -> CacheSettings)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> (String -> Value -> Parser NominalDiffTime)
-> GroupDecoder NominalDiffTime
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"ttl" String -> Value -> Parser NominalDiffTime
parseSeconds
        GroupDecoder (Maybe Int -> Maybe Int -> CacheSettings)
-> GroupDecoder (Maybe Int)
-> GroupDecoder (Maybe Int -> CacheSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"maxEntries" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder (Maybe Int -> CacheSettings)
-> GroupDecoder (Maybe Int) -> GroupDecoder CacheSettings
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"maxBytes" String -> Int -> Parser Int
parsePositiveInt

integrityDecoder :: GroupDecoder IntegritySettings
integrityDecoder :: GroupDecoder IntegritySettings
integrityDecoder =
    MinIntegrity -> MinTrustedIntegrity -> IntegritySettings
IntegritySettings
        (MinIntegrity -> MinTrustedIntegrity -> IntegritySettings)
-> GroupDecoder MinIntegrity
-> GroupDecoder (MinTrustedIntegrity -> IntegritySettings)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> (String -> Value -> Parser MinIntegrity)
-> GroupDecoder MinIntegrity
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"minPublic" ((Text -> Either Text MinIntegrity)
-> String -> Value -> Parser MinIntegrity
forall a. (Text -> Either Text a) -> String -> Value -> Parser a
parseEnum Text -> Either Text MinIntegrity
parseMinIntegrity)
        GroupDecoder (MinTrustedIntegrity -> IntegritySettings)
-> GroupDecoder MinTrustedIntegrity
-> GroupDecoder IntegritySettings
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser MinTrustedIntegrity)
-> GroupDecoder MinTrustedIntegrity
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"minTrusted" ((Text -> Either Text MinTrustedIntegrity)
-> String -> Value -> Parser MinTrustedIntegrity
forall a. (Text -> Either Text a) -> String -> Value -> Parser a
parseEnum Text -> Either Text MinTrustedIntegrity
parseMinTrustedIntegrity)
        GroupDecoder IntegritySettings
-> GroupDecoder (Maybe ()) -> GroupDecoder IntegritySettings
forall a b. GroupDecoder a -> GroupDecoder b -> GroupDecoder a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Key -> (String -> Value -> Parser ()) -> GroupDecoder (Maybe ())
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"divergencePolicy" String -> Value -> Parser ()
parseLegacyDivergencePolicy

-- A removed refusal must fail at boot instead of silently becoming private preference.
parseLegacyDivergencePolicy :: String -> Value -> Parser ()
parseLegacyDivergencePolicy :: String -> Value -> Parser ()
parseLegacyDivergencePolicy String
field = String -> (Text -> Parser ()) -> Value -> Parser ()
forall a. String -> (Text -> Parser a) -> Value -> Parser a
expectString String
field ((Text -> Parser ()) -> Value -> Parser ())
-> (Text -> Parser ()) -> Value -> Parser ()
forall a b. (a -> b) -> a -> b
$ \Text
value ->
    case Text -> Text
T.toLower (Text -> Text
T.strip Text
value) of
        Text
"warn" -> () -> Parser ()
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
        Text
removed
            | Text
removed Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text
"fail-closed", Text
"fail_closed", Text
"failclosed"] ->
                String -> Parser ()
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
field String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": fail-closed was removed. Remove this setting after accepting private preference and divergence alarms.")
        Text
_ -> String -> Parser ()
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
field String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": expected deprecated warn, or remove this setting")

egressDecoder :: GroupDecoder EgressSettings
egressDecoder :: GroupDecoder EgressSettings
egressDecoder =
    [IPRange] -> Maybe OperatorContact -> EgressSettings
EgressSettings
        ([IPRange] -> Maybe OperatorContact -> EgressSettings)
-> GroupDecoder [IPRange]
-> GroupDecoder (Maybe OperatorContact -> EgressSettings)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> Value
-> (String -> Value -> Parser [IPRange])
-> GroupDecoder [IPRange]
forall b a.
FromJSON b =>
Key -> b -> (String -> b -> Parser a) -> GroupDecoder a
optionalKeyOr Key
"additionalBlockedRanges" (Text -> Value
String Text
"") String -> Value -> Parser [IPRange]
parseBlockedRanges
        GroupDecoder (Maybe OperatorContact -> EgressSettings)
-> GroupDecoder (Maybe OperatorContact)
-> GroupDecoder EgressSettings
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser OperatorContact)
-> GroupDecoder (Maybe OperatorContact)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"operatorContact" ((Text -> Either Text OperatorContact)
-> String -> Value -> Parser OperatorContact
forall a. (Text -> Either Text a) -> String -> Value -> Parser a
parseEnum Text -> Either Text OperatorContact
parseOperatorContact)

advisoriesDecoder :: GroupDecoder AdvisoriesSettings
advisoriesDecoder :: GroupDecoder AdvisoriesSettings
advisoriesDecoder =
    Maybe AdvisoryStoreUrl
-> NominalDiffTime
-> NominalDiffTime
-> String
-> Url
-> Url
-> SourcePathRecording
-> Map Ecosystem NominalDiffTime
-> NominalDiffTime
-> Maybe NominalDiffTime
-> Percent
-> Percent
-> AdvisoriesSettings
AdvisoriesSettings
        (Maybe AdvisoryStoreUrl
 -> NominalDiffTime
 -> NominalDiffTime
 -> String
 -> Url
 -> Url
 -> SourcePathRecording
 -> Map Ecosystem NominalDiffTime
 -> NominalDiffTime
 -> Maybe NominalDiffTime
 -> Percent
 -> Percent
 -> AdvisoriesSettings)
-> GroupDecoder (Maybe AdvisoryStoreUrl)
-> GroupDecoder
     (NominalDiffTime
      -> NominalDiffTime
      -> String
      -> Url
      -> Url
      -> SourcePathRecording
      -> Map Ecosystem NominalDiffTime
      -> NominalDiffTime
      -> Maybe NominalDiffTime
      -> Percent
      -> Percent
      -> AdvisoriesSettings)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> (String -> Value -> Parser AdvisoryStoreUrl)
-> GroupDecoder (Maybe AdvisoryStoreUrl)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"url" String -> Value -> Parser AdvisoryStoreUrl
parseAdvisoryStoreUrl
        GroupDecoder
  (NominalDiffTime
   -> NominalDiffTime
   -> String
   -> Url
   -> Url
   -> SourcePathRecording
   -> Map Ecosystem NominalDiffTime
   -> NominalDiffTime
   -> Maybe NominalDiffTime
   -> Percent
   -> Percent
   -> AdvisoriesSettings)
-> GroupDecoder NominalDiffTime
-> GroupDecoder
     (NominalDiffTime
      -> String
      -> Url
      -> Url
      -> SourcePathRecording
      -> Map Ecosystem NominalDiffTime
      -> NominalDiffTime
      -> Maybe NominalDiffTime
      -> Percent
      -> Percent
      -> AdvisoriesSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser NominalDiffTime)
-> GroupDecoder NominalDiffTime
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"pollInterval" String -> Value -> Parser NominalDiffTime
parseDelaySeconds
        GroupDecoder
  (NominalDiffTime
   -> String
   -> Url
   -> Url
   -> SourcePathRecording
   -> Map Ecosystem NominalDiffTime
   -> NominalDiffTime
   -> Maybe NominalDiffTime
   -> Percent
   -> Percent
   -> AdvisoriesSettings)
-> GroupDecoder NominalDiffTime
-> GroupDecoder
     (String
      -> Url
      -> Url
      -> SourcePathRecording
      -> Map Ecosystem NominalDiffTime
      -> NominalDiffTime
      -> Maybe NominalDiffTime
      -> Percent
      -> Percent
      -> AdvisoriesSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser NominalDiffTime)
-> GroupDecoder NominalDiffTime
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"compileInterval" String -> Value -> Parser NominalDiffTime
parseDelaySeconds
        GroupDecoder
  (String
   -> Url
   -> Url
   -> SourcePathRecording
   -> Map Ecosystem NominalDiffTime
   -> NominalDiffTime
   -> Maybe NominalDiffTime
   -> Percent
   -> Percent
   -> AdvisoriesSettings)
-> GroupDecoder String
-> GroupDecoder
     (Url
      -> Url
      -> SourcePathRecording
      -> Map Ecosystem NominalDiffTime
      -> NominalDiffTime
      -> Maybe NominalDiffTime
      -> Percent
      -> Percent
      -> AdvisoriesSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder String
forall a. FromJSON a => Key -> GroupDecoder a
plainKey Key
"dataDir"
        GroupDecoder
  (Url
   -> Url
   -> SourcePathRecording
   -> Map Ecosystem NominalDiffTime
   -> NominalDiffTime
   -> Maybe NominalDiffTime
   -> Percent
   -> Percent
   -> AdvisoriesSettings)
-> GroupDecoder Url
-> GroupDecoder
     (Url
      -> SourcePathRecording
      -> Map Ecosystem NominalDiffTime
      -> NominalDiffTime
      -> Maybe NominalDiffTime
      -> Percent
      -> Percent
      -> AdvisoriesSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Value -> Parser Url) -> GroupDecoder Url
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"osvExportBaseUrl" String -> Value -> Parser Url
parseHttpUrl
        GroupDecoder
  (Url
   -> SourcePathRecording
   -> Map Ecosystem NominalDiffTime
   -> NominalDiffTime
   -> Maybe NominalDiffTime
   -> Percent
   -> Percent
   -> AdvisoriesSettings)
-> GroupDecoder Url
-> GroupDecoder
     (SourcePathRecording
      -> Map Ecosystem NominalDiffTime
      -> NominalDiffTime
      -> Maybe NominalDiffTime
      -> Percent
      -> Percent
      -> AdvisoriesSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Value -> Parser Url) -> GroupDecoder Url
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"epssFeedUrl" String -> Value -> Parser Url
parseHttpUrl
        GroupDecoder
  (SourcePathRecording
   -> Map Ecosystem NominalDiffTime
   -> NominalDiffTime
   -> Maybe NominalDiffTime
   -> Percent
   -> Percent
   -> AdvisoriesSettings)
-> GroupDecoder SourcePathRecording
-> GroupDecoder
     (Map Ecosystem NominalDiffTime
      -> NominalDiffTime
      -> Maybe NominalDiffTime
      -> Percent
      -> Percent
      -> AdvisoriesSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Bool -> Parser SourcePathRecording)
-> GroupDecoder SourcePathRecording
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"recordSourcePath" (\String
_ Bool
recordPath -> SourcePathRecording -> Parser SourcePathRecording
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (if Bool
recordPath then SourcePathRecording
RecordSourcePath else SourcePathRecording
FingerprintSourcePath))
        GroupDecoder
  (Map Ecosystem NominalDiffTime
   -> NominalDiffTime
   -> Maybe NominalDiffTime
   -> Percent
   -> Percent
   -> AdvisoriesSettings)
-> GroupDecoder (Map Ecosystem NominalDiffTime)
-> GroupDecoder
     (NominalDiffTime
      -> Maybe NominalDiffTime
      -> Percent
      -> Percent
      -> AdvisoriesSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser (Map Ecosystem NominalDiffTime))
-> GroupDecoder (Map Ecosystem NominalDiffTime)
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"quietTime" KeyMap Value -> Parser (Map Ecosystem NominalDiffTime)
parseQuietTimes
        GroupDecoder
  (NominalDiffTime
   -> Maybe NominalDiffTime
   -> Percent
   -> Percent
   -> AdvisoriesSettings)
-> GroupDecoder NominalDiffTime
-> GroupDecoder
     (Maybe NominalDiffTime -> Percent -> Percent -> AdvisoriesSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser NominalDiffTime)
-> GroupDecoder NominalDiffTime
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"epssQuietTime" String -> Value -> Parser NominalDiffTime
parseDelaySeconds
        GroupDecoder
  (Maybe NominalDiffTime -> Percent -> Percent -> AdvisoriesSettings)
-> GroupDecoder (Maybe NominalDiffTime)
-> GroupDecoder (Percent -> Percent -> AdvisoriesSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser NominalDiffTime)
-> GroupDecoder (Maybe NominalDiffTime)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"maxAgeSeconds" String -> Value -> Parser NominalDiffTime
parseDelaySeconds
        GroupDecoder (Percent -> Percent -> AdvisoriesSettings)
-> GroupDecoder Percent
-> GroupDecoder (Percent -> AdvisoriesSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Value -> Parser Percent) -> GroupDecoder Percent
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"maxShrinkPercent" String -> Value -> Parser Percent
parsePercent
        GroupDecoder (Percent -> AdvisoriesSettings)
-> GroupDecoder Percent -> GroupDecoder AdvisoriesSettings
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Value -> Parser Percent) -> GroupDecoder Percent
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"epssLossCapPercent" String -> Value -> Parser Percent
parsePercent

runtimeDecoder :: GroupDecoder RuntimeSettings
runtimeDecoder :: GroupDecoder RuntimeSettings
runtimeDecoder =
    Maybe Int
-> Maybe Int
-> Maybe Int
-> Maybe Int
-> Maybe Int
-> Maybe Int
-> RuntimeSettings
RuntimeSettings
        (Maybe Int
 -> Maybe Int
 -> Maybe Int
 -> Maybe Int
 -> Maybe Int
 -> Maybe Int
 -> RuntimeSettings)
-> GroupDecoder (Maybe Int)
-> GroupDecoder
     (Maybe Int
      -> Maybe Int
      -> Maybe Int
      -> Maybe Int
      -> Maybe Int
      -> RuntimeSettings)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"cores" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder
  (Maybe Int
   -> Maybe Int
   -> Maybe Int
   -> Maybe Int
   -> Maybe Int
   -> RuntimeSettings)
-> GroupDecoder (Maybe Int)
-> GroupDecoder
     (Maybe Int
      -> Maybe Int -> Maybe Int -> Maybe Int -> RuntimeSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"coresCeiling" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder
  (Maybe Int
   -> Maybe Int -> Maybe Int -> Maybe Int -> RuntimeSettings)
-> GroupDecoder (Maybe Int)
-> GroupDecoder
     (Maybe Int -> Maybe Int -> Maybe Int -> RuntimeSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"maxHeapBytes" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder
  (Maybe Int -> Maybe Int -> Maybe Int -> RuntimeSettings)
-> GroupDecoder (Maybe Int)
-> GroupDecoder (Maybe Int -> Maybe Int -> RuntimeSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"serveMaxInFlight" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder (Maybe Int -> Maybe Int -> RuntimeSettings)
-> GroupDecoder (Maybe Int)
-> GroupDecoder (Maybe Int -> RuntimeSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"publicConnectionsPerHost" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder (Maybe Int -> RuntimeSettings)
-> GroupDecoder (Maybe Int) -> GroupDecoder RuntimeSettings
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"privateConnectionsPerHost" String -> Int -> Parser Int
parsePositiveInt

observabilityDecoder :: GroupDecoder ObservabilitySettings
observabilityDecoder :: GroupDecoder ObservabilitySettings
observabilityDecoder =
    LogFormat -> LogLevel -> TelemetrySwitch -> ObservabilitySettings
ObservabilitySettings
        (LogFormat -> LogLevel -> TelemetrySwitch -> ObservabilitySettings)
-> GroupDecoder LogFormat
-> GroupDecoder
     (LogLevel -> TelemetrySwitch -> ObservabilitySettings)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> (String -> Value -> Parser LogFormat) -> GroupDecoder LogFormat
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"logFormat" ((Text -> Either Text LogFormat)
-> String -> Value -> Parser LogFormat
forall a. (Text -> Either Text a) -> String -> Value -> Parser a
parseEnum Text -> Either Text LogFormat
parseLogFormat)
        GroupDecoder (LogLevel -> TelemetrySwitch -> ObservabilitySettings)
-> GroupDecoder LogLevel
-> GroupDecoder (TelemetrySwitch -> ObservabilitySettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser LogLevel) -> GroupDecoder LogLevel
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"logLevel" ((Text -> Either Text LogLevel)
-> String -> Value -> Parser LogLevel
forall a. (Text -> Either Text a) -> String -> Value -> Parser a
parseEnum Text -> Either Text LogLevel
parseLogLevel)
        GroupDecoder (TelemetrySwitch -> ObservabilitySettings)
-> GroupDecoder TelemetrySwitch
-> GroupDecoder ObservabilitySettings
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser TelemetrySwitch)
-> GroupDecoder TelemetrySwitch
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"telemetry" ((Text -> Either Text TelemetrySwitch)
-> String -> Value -> Parser TelemetrySwitch
forall a. (Text -> Either Text a) -> String -> Value -> Parser a
parseEnum Text -> Either Text TelemetrySwitch
parseTelemetrySwitch)

dredgerDecoder :: GroupDecoder DredgerSettings
dredgerDecoder :: GroupDecoder DredgerSettings
dredgerDecoder =
    Int
-> NominalDiffTime
-> NominalDiffTime
-> Maybe NominalDiffTime
-> Maybe Rational
-> Map Text QuotaOverride
-> Maybe Int
-> Bool
-> DredgerSettings
DredgerSettings
        (Int
 -> NominalDiffTime
 -> NominalDiffTime
 -> Maybe NominalDiffTime
 -> Maybe Rational
 -> Map Text QuotaOverride
 -> Maybe Int
 -> Bool
 -> DredgerSettings)
-> GroupDecoder Int
-> GroupDecoder
     (NominalDiffTime
      -> NominalDiffTime
      -> Maybe NominalDiffTime
      -> Maybe Rational
      -> Map Text QuotaOverride
      -> Maybe Int
      -> Bool
      -> DredgerSettings)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> (String -> Int -> Parser Int) -> GroupDecoder Int
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"chunkSize" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder
  (NominalDiffTime
   -> NominalDiffTime
   -> Maybe NominalDiffTime
   -> Maybe Rational
   -> Map Text QuotaOverride
   -> Maybe Int
   -> Bool
   -> DredgerSettings)
-> GroupDecoder NominalDiffTime
-> GroupDecoder
     (NominalDiffTime
      -> Maybe NominalDiffTime
      -> Maybe Rational
      -> Map Text QuotaOverride
      -> Maybe Int
      -> Bool
      -> DredgerSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser NominalDiffTime)
-> GroupDecoder NominalDiffTime
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"chunkPause" String -> Value -> Parser NominalDiffTime
parseDelaySeconds
        GroupDecoder
  (NominalDiffTime
   -> Maybe NominalDiffTime
   -> Maybe Rational
   -> Map Text QuotaOverride
   -> Maybe Int
   -> Bool
   -> DredgerSettings)
-> GroupDecoder NominalDiffTime
-> GroupDecoder
     (Maybe NominalDiffTime
      -> Maybe Rational
      -> Map Text QuotaOverride
      -> Maybe Int
      -> Bool
      -> DredgerSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser NominalDiffTime)
-> GroupDecoder NominalDiffTime
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder a
requiredKey Key
"cyclePause" String -> Value -> Parser NominalDiffTime
parseDelaySeconds
        GroupDecoder
  (Maybe NominalDiffTime
   -> Maybe Rational
   -> Map Text QuotaOverride
   -> Maybe Int
   -> Bool
   -> DredgerSettings)
-> GroupDecoder (Maybe NominalDiffTime)
-> GroupDecoder
     (Maybe Rational
      -> Map Text QuotaOverride -> Maybe Int -> Bool -> DredgerSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser NominalDiffTime)
-> GroupDecoder (Maybe NominalDiffTime)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"targetCycleWindow" String -> Value -> Parser NominalDiffTime
parseDelaySeconds
        GroupDecoder
  (Maybe Rational
   -> Map Text QuotaOverride -> Maybe Int -> Bool -> DredgerSettings)
-> GroupDecoder (Maybe Rational)
-> GroupDecoder
     (Map Text QuotaOverride -> Maybe Int -> Bool -> DredgerSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (String -> Value -> Parser Rational)
-> GroupDecoder (Maybe Rational)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"requestBudgetFraction" String -> Value -> Parser Rational
parseUnitFraction
        GroupDecoder
  (Map Text QuotaOverride -> Maybe Int -> Bool -> DredgerSettings)
-> GroupDecoder (Map Text QuotaOverride)
-> GroupDecoder (Maybe Int -> Bool -> DredgerSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser (Map Text QuotaOverride))
-> GroupDecoder (Map Text QuotaOverride)
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"quotaOverrides" KeyMap Value -> Parser (Map Text QuotaOverride)
parseQuotaOverrides
        GroupDecoder (Maybe Int -> Bool -> DredgerSettings)
-> GroupDecoder (Maybe Int)
-> GroupDecoder (Bool -> DredgerSettings)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> (String -> Int -> Parser Int) -> GroupDecoder (Maybe Int)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"deletionCap" String -> Int -> Parser Int
parsePositiveInt
        GroupDecoder (Bool -> DredgerSettings)
-> GroupDecoder Bool -> GroupDecoder DredgerSettings
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder Bool
forall a. FromJSON a => Key -> GroupDecoder a
plainKey Key
"fullWalk"

-- The key is the store URL the entry describes, kept as written so the boot can match it against
-- the endpoints the mounts declare.
parseQuotaOverrides :: KeyMap.KeyMap Value -> Parser (Map.Map Text QuotaOverride)
parseQuotaOverrides :: KeyMap Value -> Parser (Map Text QuotaOverride)
parseQuotaOverrides KeyMap Value
km = [(Text, QuotaOverride)] -> Map Text QuotaOverride
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(Text, QuotaOverride)] -> Map Text QuotaOverride)
-> Parser [(Text, QuotaOverride)]
-> Parser (Map Text QuotaOverride)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Key, Value) -> Parser (Text, QuotaOverride))
-> [(Key, Value)] -> Parser [(Text, QuotaOverride)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Key, Value) -> Parser (Text, QuotaOverride)
parseQuotaOverrideEntry (KeyMap Value -> [(Key, Value)]
forall v. KeyMap v -> [(Key, v)]
KeyMap.toList KeyMap Value
km)

parseQuotaOverrideEntry :: (Key.Key, Value) -> Parser (Text, QuotaOverride)
parseQuotaOverrideEntry :: (Key, Value) -> Parser (Text, QuotaOverride)
parseQuotaOverrideEntry (Key
k, Value
v) =
    (Key -> Text
Key.toText Key
k,) (QuotaOverride -> (Text, QuotaOverride))
-> Parser QuotaOverride -> Parser (Text, QuotaOverride)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String
-> (KeyMap Value -> Parser QuotaOverride)
-> Value
-> Parser QuotaOverride
forall a. String -> (KeyMap Value -> Parser a) -> Value -> Parser a
withObject String
"QuotaOverride" (String
-> GroupDecoder QuotaOverride
-> KeyMap Value
-> Parser QuotaOverride
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeBareGroup String
entryPath (String -> GroupDecoder QuotaOverride
quotaOverrideDecoder String
entryPath)) Value
v
  where
    entryPath :: String
entryPath = String
"dredger.quotaOverrides." String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Key -> String
Key.toString Key
k

quotaOverrideDecoder :: String -> GroupDecoder QuotaOverride
quotaOverrideDecoder :: String -> GroupDecoder QuotaOverride
quotaOverrideDecoder String
entryPath =
    Maybe Text
-> Map QuotaDimension Rational
-> Map RequestKind Rational
-> QuotaOverride
QuotaOverride
        (Maybe Text
 -> Map QuotaDimension Rational
 -> Map RequestKind Rational
 -> QuotaOverride)
-> GroupDecoder (Maybe Text)
-> GroupDecoder
     (Map QuotaDimension Rational
      -> Map RequestKind Rational -> QuotaOverride)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> (String -> Value -> Parser Text) -> GroupDecoder (Maybe Text)
forall b a.
FromJSON b =>
Key -> (String -> b -> Parser a) -> GroupDecoder (Maybe a)
optionalKey Key
"scope" (String -> (Text -> Parser Text) -> Value -> Parser Text
forall a. String -> (Text -> Parser a) -> Value -> Parser a
`expectString` Text -> Parser Text
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure)
        GroupDecoder
  (Map QuotaDimension Rational
   -> Map RequestKind Rational -> QuotaOverride)
-> GroupDecoder (Map QuotaDimension Rational)
-> GroupDecoder (Map RequestKind Rational -> QuotaOverride)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser (Map QuotaDimension Rational))
-> GroupDecoder (Map QuotaDimension Rational)
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"quotas" (String
-> (Text -> Maybe QuotaDimension)
-> String
-> KeyMap Value
-> Parser (Map QuotaDimension Rational)
forall key.
Ord key =>
String
-> (Text -> Maybe key)
-> String
-> KeyMap Value
-> Parser (Map key Rational)
parseRates String
entryPath Text -> Maybe QuotaDimension
parseQuotaDimension String
"quota dimension")
        GroupDecoder (Map RequestKind Rational -> QuotaOverride)
-> GroupDecoder (Map RequestKind Rational)
-> GroupDecoder QuotaOverride
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key
-> (KeyMap Value -> Parser (Map RequestKind Rational))
-> GroupDecoder (Map RequestKind Rational)
forall a. Key -> (KeyMap Value -> Parser a) -> GroupDecoder a
nestedKey Key
"requestWeights" (String
-> (Text -> Maybe RequestKind)
-> String
-> KeyMap Value
-> Parser (Map RequestKind Rational)
forall key.
Ord key =>
String
-> (Text -> Maybe key)
-> String
-> KeyMap Value
-> Parser (Map key Rational)
parseRates String
entryPath Text -> Maybe RequestKind
parseRequestKind String
"request kind")

{- Every entry under one map of positive rates, keyed by a name this build meters under. An
unknown name fails the load, because a rate nothing reads would silently pace nothing. -}
parseRates :: (Ord key) => String -> (Text -> Maybe key) -> String -> KeyMap.KeyMap Value -> Parser (Map.Map key Rational)
parseRates :: forall key.
Ord key =>
String
-> (Text -> Maybe key)
-> String
-> KeyMap Value
-> Parser (Map key Rational)
parseRates String
entryPath Text -> Maybe key
readKey String
subject KeyMap Value
km = [(key, Rational)] -> Map key Rational
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(key, Rational)] -> Map key Rational)
-> Parser [(key, Rational)] -> Parser (Map key Rational)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Key, Value) -> Parser (key, Rational))
-> [(Key, Value)] -> Parser [(key, Rational)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Key, Value) -> Parser (key, Rational)
rate (KeyMap Value -> [(Key, Value)]
forall v. KeyMap v -> [(Key, v)]
KeyMap.toList KeyMap Value
km)
  where
    rate :: (Key, Value) -> Parser (key, Rational)
rate (Key
k, Value
v) = case Text -> Maybe key
readKey (Key -> Text
Key.toText Key
k) of
        Maybe key
Nothing -> String -> Parser (key, Rational)
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
entryPath String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Key -> String
Key.toString Key
k String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" is not a " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
subject String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" this build meters")
        Just key
metered -> (key
metered,) (Rational -> (key, Rational))
-> Parser Rational -> Parser (key, Rational)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Value -> Parser Rational
parsePositiveRate (String
entryPath String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"." String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Key -> String
Key.toString Key
k) Value
v

parseQuietTimes :: KeyMap.KeyMap Value -> Parser (Map.Map Ecosystem NominalDiffTime)
parseQuietTimes :: KeyMap Value -> Parser (Map Ecosystem NominalDiffTime)
parseQuietTimes KeyMap Value
km = [(Ecosystem, NominalDiffTime)] -> Map Ecosystem NominalDiffTime
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(Ecosystem, NominalDiffTime)] -> Map Ecosystem NominalDiffTime)
-> Parser [(Ecosystem, NominalDiffTime)]
-> Parser (Map Ecosystem NominalDiffTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Key, Value) -> Parser (Ecosystem, NominalDiffTime))
-> [(Key, Value)] -> Parser [(Ecosystem, NominalDiffTime)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Key, Value) -> Parser (Ecosystem, NominalDiffTime)
parseQuietTimeEntry (KeyMap Value -> [(Key, Value)]
forall v. KeyMap v -> [(Key, v)]
KeyMap.toList KeyMap Value
km)

parseQuietTimeEntry :: (Key.Key, Value) -> Parser (Ecosystem, NominalDiffTime)
parseQuietTimeEntry :: (Key, Value) -> Parser (Ecosystem, NominalDiffTime)
parseQuietTimeEntry (Key
k, Value
v) = do
    eco <- String -> Key -> Parser Ecosystem
ecosystemKey String
" in advisories.quietTime" Key
k
    (eco,) <$> parseDelaySeconds ("advisories.quietTime." <> T.unpack (Key.toText k)) v

-- Every mount in the merged object, the shipped per-ecosystem templates included.
-- "Ecluse.Config" decides which of them are active and must be complete.
parseMounts :: KeyMap.KeyMap Value -> Parser (Map.Map Ecosystem MountConfig)
parseMounts :: KeyMap Value -> Parser (Map Ecosystem MountConfig)
parseMounts KeyMap Value
km = [(Ecosystem, MountConfig)] -> Map Ecosystem MountConfig
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(Ecosystem, MountConfig)] -> Map Ecosystem MountConfig)
-> Parser [(Ecosystem, MountConfig)]
-> Parser (Map Ecosystem MountConfig)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Key, Value) -> Parser (Ecosystem, MountConfig))
-> [(Key, Value)] -> Parser [(Ecosystem, MountConfig)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Key, Value) -> Parser (Ecosystem, MountConfig)
parseMountEntry (KeyMap Value -> [(Key, Value)]
forall v. KeyMap v -> [(Key, v)]
KeyMap.toList KeyMap Value
km)

parseMountEntry :: (Key.Key, Value) -> Parser (Ecosystem, MountConfig)
parseMountEntry :: (Key, Value) -> Parser (Ecosystem, MountConfig)
parseMountEntry (Key
k, Value
v) = do
    eco <- String -> Key -> Parser Ecosystem
ecosystemKey String
"" Key
k
    mcfg <- withObject "MountConfig" (decodeBareGroup "mount" (mountDecoder eco)) v
    pure (eco, mcfg)

-- A map key spelled as a mounts key is, so an unknown ecosystem fails the load rather than
-- configuring nothing. The location is appended to the refusal that names the key.
ecosystemKey :: String -> Key.Key -> Parser Ecosystem
ecosystemKey :: String -> Key -> Parser Ecosystem
ecosystemKey String
location Key
k = Parser Ecosystem
-> (Ecosystem -> Parser Ecosystem)
-> Maybe Ecosystem
-> Parser Ecosystem
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Parser Ecosystem
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
refusal) Ecosystem -> Parser Ecosystem
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Maybe Ecosystem
parseEcosystem (Key -> Text
Key.toText Key
k))
  where
    refusal :: String
refusal = String
"Invalid ecosystem" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
location String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack (Key -> Text
Key.toText Key
k)

parseSecret :: String -> Value -> Parser Secret
parseSecret :: String -> Value -> Parser Secret
parseSecret String
field = String -> (Text -> Parser Secret) -> Value -> Parser Secret
forall a. String -> (Text -> Parser a) -> Value -> Parser a
expectString String
field (Secret -> Parser Secret
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Secret -> Parser Secret)
-> (Text -> Secret) -> Text -> Parser Secret
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Secret
mkSecret)

-- An entry no scope, name or prefix can equal (an empty one, a wrong separator) fails the load,
-- so a typo cannot seed a privilege that covers nothing. The mount's adapter reads each entry.
parseFirstParty :: Ecosystem -> String -> Value -> Parser FirstParty
parseFirstParty :: Ecosystem -> String -> Value -> Parser FirstParty
parseFirstParty Ecosystem
eco String
field Value
v = case Ecosystem -> Maybe FirstPartyDeclaration
firstPartyFor Ecosystem
eco of
    Maybe FirstPartyDeclaration
Nothing -> String -> Parser FirstParty
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
field String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": this build reads no first-party entries for " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. ToString a => a -> String
toString (Ecosystem -> Text
ecosystemName Ecosystem
eco))
    Just (FirstPartyDeclaration FirstPartyNames entry
names) ->
        FirstPartyNames entry -> NonEmpty entry -> FirstParty
forall entry.
(Eq entry, Show entry, Typeable entry) =>
FirstPartyNames entry -> NonEmpty entry -> FirstParty
FirstParty FirstPartyNames entry
names
            (NonEmpty entry -> FirstParty)
-> Parser (NonEmpty entry) -> Parser FirstParty
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String
-> (Text -> Parser entry)
-> String
-> Value
-> Parser (NonEmpty entry)
forall a.
String
-> (Text -> Parser a) -> String -> Value -> Parser (NonEmpty a)
nonEmptyCommaSeparated
                String
field
                (String -> (Text -> Either ParseError entry) -> Text -> Parser entry
forall e a. String -> (Text -> Either e a) -> Text -> Parser a
projectedEntry (String
"invalid " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. ToString a => a -> String
toString (FirstPartyNames entry -> Text
forall entry. FirstPartyNames entry -> Text
namesEntryNoun FirstPartyNames entry
names) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" in " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
field) (FirstPartyNames entry -> Text -> Either ParseError entry
forall entry.
FirstPartyNames entry -> Text -> Either ParseError entry
namesEntry FirstPartyNames entry
names))
                (String
field String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" must name at least one " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. ToString a => a -> String
toString (FirstPartyNames entry -> Text
forall entry. FirstPartyNames entry -> Text
namesListNoun FirstPartyNames entry
names))
                Value
v

parseBlockedRanges :: String -> Value -> Parser [IPRange]
parseBlockedRanges :: String -> Value -> Parser [IPRange]
parseBlockedRanges String
field = String -> (Text -> Parser IPRange) -> Value -> Parser [IPRange]
forall a. String -> (Text -> Parser a) -> Value -> Parser [a]
commaSeparated String
field Text -> Parser IPRange
parseBlockedRangeEntry

parseBlockedRangeEntry :: Text -> Parser IPRange
parseBlockedRangeEntry :: Text -> Parser IPRange
parseBlockedRangeEntry Text
entry =
    case Text -> Maybe IPRange
parseBlockedRange Text
entry of
        Just IPRange
range -> IPRange -> Parser IPRange
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure IPRange
range
        Maybe IPRange
Nothing -> String -> Parser IPRange
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"invalid CIDR range in additionalBlockedRanges: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
entry)

-- A rate of zero admits no request at all, so it is refused rather than read as a store that may
-- never be swept.
parsePositiveRate :: String -> Value -> Parser Rational
parsePositiveRate :: String -> Value -> Parser Rational
parsePositiveRate String
field Value
value = case Value
value of
    Number Scientific
n | Just Rational
rate <- Scientific -> Maybe Rational
boundedRational Scientific
n, Rational
rate Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
> Rational
0 -> Rational -> Parser Rational
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Rational
rate
    Value
_ -> String -> Parser Rational
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
field String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" must be a positive number written within nine decimal places")

-- Neither end of the range is a share, so both are refused rather than read as a sweep that
-- stops or one that takes the whole store.
parseUnitFraction :: String -> Value -> Parser Rational
parseUnitFraction :: String -> Value -> Parser Rational
parseUnitFraction String
field Value
value = case Value
value of
    Number Scientific
n | Just Rational
share <- Scientific -> Maybe Rational
boundedRational Scientific
n, Rational
share Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
> Rational
0, Rational
share Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
< Rational
1 -> Rational -> Parser Rational
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Rational
share
    Value
_ -> String -> Parser Rational
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
field String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" must be a number above 0 and below 1")

-- Both ends mean something: 0 allows no shrink at all, and 100 lets any size through.
parsePercent :: String -> Value -> Parser Percent
parsePercent :: String -> Value -> Parser Percent
parsePercent String
field Value
value = case Value
value of
    Number Scientific
n | Just Percent
percent <- Rational -> Maybe Percent
mkPercent (Rational -> Maybe Percent) -> Maybe Rational -> Maybe Percent
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Scientific -> Maybe Rational
boundedRational Scientific
n -> Percent -> Parser Percent
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Percent
percent
    Value
_ -> String -> Parser Percent
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
field String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" must be a number from 0 to 100")

-- The exponent is bounded before the rational is realised, so a 1e999999999 is refused rather
-- than expanded.
boundedRational :: Scientific -> Maybe Rational
boundedRational :: Scientific -> Maybe Rational
boundedRational Scientific
n
    | Scientific -> Int
base10Exponent Scientific
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
rationalExponentBound Bool -> Bool -> Bool
|| Scientific -> Int
base10Exponent Scientific
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Int
forall a. Num a => a -> a
negate Int
rationalExponentBound = Maybe Rational
forall a. Maybe a
Nothing
    | Bool
otherwise = Rational -> Maybe Rational
forall a. a -> Maybe a
Just (Scientific -> Rational
forall a. Real a => a -> Rational
toRational Scientific
n)

rationalExponentBound :: Int
rationalExponentBound :: Int
rationalExponentBound = Int
9

parseSeconds :: String -> Value -> Parser NominalDiffTime
parseSeconds :: String -> Value -> Parser NominalDiffTime
parseSeconds String
field = \case
    String Text
t -> case Text -> Maybe Integer
forall a. Integral a => Text -> Maybe a
readDecimalText Text
t :: Maybe Integer of
        Just Integer
n -> String -> Integer -> Parser NominalDiffTime
boundedSeconds String
field Integer
n
        Maybe Integer
Nothing -> String -> String -> Parser NominalDiffTime
forall a. String -> String -> Parser a
secondsFailure String
field (Text -> String
forall b a. (Show a, IsString b) => a -> b
show Text
t)
    -- 'toBoundedInteger' refuses a fractional or out-of-'Int64' value, and its exponent guard
    -- rejects a pathological 1e999999999999 without ever realising the integer.
    Number Scientific
n -> case Scientific -> Maybe Int64
forall i. (Integral i, Bounded i) => Scientific -> Maybe i
toBoundedInteger Scientific
n :: Maybe Int64 of
        Just Int64
val -> String -> Integer -> Parser NominalDiffTime
boundedSeconds String
field (Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger Int64
val)
        Maybe Int64
Nothing -> String -> String -> Parser NominalDiffTime
forall a. String -> String -> Parser a
secondsFailure String
field (Scientific -> String
forall b a. (Show a, IsString b) => a -> b
show Scientific
n)
    Value
other -> String -> Parser NominalDiffTime
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
field String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" must be a non-negative integer count of seconds, but encountered " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value -> String
valueKind Value
other)

boundedSeconds :: String -> Integer -> Parser NominalDiffTime
boundedSeconds :: String -> Integer -> Parser NominalDiffTime
boundedSeconds String
field Integer
n
    | Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0 Bool -> Bool -> Bool
&& Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
maxBound :: Int64) = NominalDiffTime -> Parser NominalDiffTime
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer -> NominalDiffTime
forall a. Num a => Integer -> a
fromInteger Integer
n)
    | Bool
otherwise = String -> String -> Parser NominalDiffTime
forall a. String -> String -> Parser a
secondsFailure String
field (Integer -> String
forall b a. (Show a, IsString b) => a -> b
show Integer
n)

secondsFailure :: String -> String -> Parser a
secondsFailure :: forall a. String -> String -> Parser a
secondsFailure String
field String
got =
    String -> Parser a
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
field String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" must be a non-negative integer count of seconds, got " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
got)

parsePositiveInt :: String -> Int -> Parser Int
parsePositiveInt :: String -> Int -> Parser Int
parsePositiveInt String
field Int
value
    | Int
value Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = Int -> Parser Int
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
value
    | Bool
otherwise = String -> Parser Int
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
field String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" must be a positive integer")

-- A recurring delay, positive and bounded. Zero would spin the poll, and the bound keeps the
-- microsecond conversion inside 'Int' rather than wrapping to a negative delay.
parseDelaySeconds :: String -> Value -> Parser NominalDiffTime
parseDelaySeconds :: String -> Value -> Parser NominalDiffTime
parseDelaySeconds String
field Value
v = do
    secs <- String -> Value -> Parser NominalDiffTime
parseSeconds String
field Value
v
    let n = NominalDiffTime -> Integer
forall b. Integral b => NominalDiffTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate NominalDiffTime
secs :: Integer
        maxDelay = Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int
forall a. Bounded a => a
maxBound :: Int) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
1_000_000
    if n >= 1 && n <= maxDelay
        then pure secs
        else fail (field <> " must be a positive integer count of seconds, at most " <> show maxDelay)

-- | Read a document's rule overlay, treating absent and null rules as an empty patch.
parseRulesPatch :: Value -> Parser RulePatch
parseRulesPatch :: Value -> Parser RulePatch
parseRulesPatch = String
-> (KeyMap Value -> Parser RulePatch) -> Value -> Parser RulePatch
forall a. String -> (KeyMap Value -> Parser a) -> Value -> Parser a
withObject String
"Config" (\KeyMap Value
obj -> (Value -> Parser RulePatch)
-> KeyMap Value -> Key -> Parser (Maybe RulePatch)
forall a.
(Value -> Parser a) -> KeyMap Value -> Key -> Parser (Maybe a)
explicitParseFieldMaybe Value -> Parser RulePatch
parseRulePatch KeyMap Value
obj Key
"rules" Parser (Maybe RulePatch) -> RulePatch -> Parser RulePatch
forall a. Parser (Maybe a) -> a -> Parser a
.!= Map Text RuleEntry -> RulePatch
RulePatch Map Text RuleEntry
forall k a. Map k a
Map.empty)

-- | Parse named rule entries without resolving them against a baseline.
parseRulePatch :: Value -> Parser RulePatch
parseRulePatch :: Value -> Parser RulePatch
parseRulePatch = String
-> (KeyMap Value -> Parser RulePatch) -> Value -> Parser RulePatch
forall a. String -> (KeyMap Value -> Parser a) -> Value -> Parser a
withObject String
"rules" ((KeyMap Value -> Parser RulePatch) -> Value -> Parser RulePatch)
-> (KeyMap Value -> Parser RulePatch) -> Value -> Parser RulePatch
forall a b. (a -> b) -> a -> b
$ \KeyMap Value
o ->
    Map Text RuleEntry -> RulePatch
RulePatch (Map Text RuleEntry -> RulePatch)
-> ([(Text, RuleEntry)] -> Map Text RuleEntry)
-> [(Text, RuleEntry)]
-> RulePatch
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Text, RuleEntry)] -> Map Text RuleEntry
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(Text, RuleEntry)] -> RulePatch)
-> Parser [(Text, RuleEntry)] -> Parser RulePatch
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Key, Value) -> Parser (Text, RuleEntry))
-> [(Key, Value)] -> Parser [(Text, RuleEntry)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Key, Value) -> Parser (Text, RuleEntry)
decodeEntry (KeyMap Value -> [(Key, Value)]
forall v. KeyMap v -> [(Key, v)]
KeyMap.toList KeyMap Value
o)
  where
    decodeEntry :: (Key, Value) -> Parser (Text, RuleEntry)
decodeEntry (Key
k, Value
v) = (Key -> Text
Key.toText Key
k,) (RuleEntry -> (Text, RuleEntry))
-> Parser RuleEntry -> Parser (Text, RuleEntry)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser RuleEntry
parseRuleEntry Value
v

-- | Refuse secret keys before decoding a rule's fields.
parseRuleEntry :: Value -> Parser RuleEntry
parseRuleEntry :: Value -> Parser RuleEntry
parseRuleEntry = String
-> (KeyMap Value -> Parser RuleEntry) -> Value -> Parser RuleEntry
forall a. String -> (KeyMap Value -> Parser a) -> Value -> Parser a
withObject String
"rule" ((KeyMap Value -> Parser RuleEntry) -> Value -> Parser RuleEntry)
-> (KeyMap Value -> Parser RuleEntry) -> Value -> Parser RuleEntry
forall a b. (a -> b) -> a -> b
$ \KeyMap Value
o -> do
    KeyMap Value -> Parser ()
rejectSecretKeys KeyMap Value
o
    String
-> GroupDecoder RuleEntry -> KeyMap Value -> Parser RuleEntry
forall a. String -> GroupDecoder a -> KeyMap Value -> Parser a
decodeGroup String
"rule" GroupDecoder RuleEntry
ruleEntryDecoder KeyMap Value
o

ruleEntryDecoder :: GroupDecoder RuleEntry
ruleEntryDecoder :: GroupDecoder RuleEntry
ruleEntryDecoder =
    Maybe Text
-> Maybe Int
-> Maybe Bool
-> Maybe Integer
-> Maybe Text
-> Maybe Text
-> Maybe Double
-> Maybe Double
-> Maybe Text
-> Maybe Text
-> Maybe Double
-> Maybe Double
-> Maybe Double
-> Maybe Double
-> Maybe [Text]
-> RuleEntry
RuleEntry
        (Maybe Text
 -> Maybe Int
 -> Maybe Bool
 -> Maybe Integer
 -> Maybe Text
 -> Maybe Text
 -> Maybe Double
 -> Maybe Double
 -> Maybe Text
 -> Maybe Text
 -> Maybe Double
 -> Maybe Double
 -> Maybe Double
 -> Maybe Double
 -> Maybe [Text]
 -> RuleEntry)
-> GroupDecoder (Maybe Text)
-> GroupDecoder
     (Maybe Int
      -> Maybe Bool
      -> Maybe Integer
      -> Maybe Text
      -> Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Text
      -> Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe [Text]
      -> RuleEntry)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> GroupDecoder (Maybe Text)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"type"
        GroupDecoder
  (Maybe Int
   -> Maybe Bool
   -> Maybe Integer
   -> Maybe Text
   -> Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Text
   -> Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe [Text]
   -> RuleEntry)
-> GroupDecoder (Maybe Int)
-> GroupDecoder
     (Maybe Bool
      -> Maybe Integer
      -> Maybe Text
      -> Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Text
      -> Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe [Text]
      -> RuleEntry)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Int)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"precedence"
        GroupDecoder
  (Maybe Bool
   -> Maybe Integer
   -> Maybe Text
   -> Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Text
   -> Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe [Text]
   -> RuleEntry)
-> GroupDecoder (Maybe Bool)
-> GroupDecoder
     (Maybe Integer
      -> Maybe Text
      -> Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Text
      -> Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe [Text]
      -> RuleEntry)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Bool)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"enabled"
        GroupDecoder
  (Maybe Integer
   -> Maybe Text
   -> Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Text
   -> Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe [Text]
   -> RuleEntry)
-> GroupDecoder (Maybe Integer)
-> GroupDecoder
     (Maybe Text
      -> Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Text
      -> Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe [Text]
      -> RuleEntry)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Integer)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"ageSeconds"
        GroupDecoder
  (Maybe Text
   -> Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Text
   -> Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe [Text]
   -> RuleEntry)
-> GroupDecoder (Maybe Text)
-> GroupDecoder
     (Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Text
      -> Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe [Text]
      -> RuleEntry)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Text)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"scope"
        GroupDecoder
  (Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Text
   -> Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe [Text]
   -> RuleEntry)
-> GroupDecoder (Maybe Text)
-> GroupDecoder
     (Maybe Double
      -> Maybe Double
      -> Maybe Text
      -> Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe [Text]
      -> RuleEntry)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Text)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"identity"
        GroupDecoder
  (Maybe Double
   -> Maybe Double
   -> Maybe Text
   -> Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe [Text]
   -> RuleEntry)
-> GroupDecoder (Maybe Double)
-> GroupDecoder
     (Maybe Double
      -> Maybe Text
      -> Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe [Text]
      -> RuleEntry)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Double)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"minCvss"
        GroupDecoder
  (Maybe Double
   -> Maybe Text
   -> Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe [Text]
   -> RuleEntry)
-> GroupDecoder (Maybe Double)
-> GroupDecoder
     (Maybe Text
      -> Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe [Text]
      -> RuleEntry)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Double)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"minEpss"
        GroupDecoder
  (Maybe Text
   -> Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe [Text]
   -> RuleEntry)
-> GroupDecoder (Maybe Text)
-> GroupDecoder
     (Maybe Text
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe [Text]
      -> RuleEntry)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Text)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"onUnavailable"
        GroupDecoder
  (Maybe Text
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe [Text]
   -> RuleEntry)
-> GroupDecoder (Maybe Text)
-> GroupDecoder
     (Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe Double
      -> Maybe [Text]
      -> RuleEntry)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Text)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"remainingAdvisories"
        GroupDecoder
  (Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe Double
   -> Maybe [Text]
   -> RuleEntry)
-> GroupDecoder (Maybe Double)
-> GroupDecoder
     (Maybe Double
      -> Maybe Double -> Maybe Double -> Maybe [Text] -> RuleEntry)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Double)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"maxRemainingCvss"
        GroupDecoder
  (Maybe Double
   -> Maybe Double -> Maybe Double -> Maybe [Text] -> RuleEntry)
-> GroupDecoder (Maybe Double)
-> GroupDecoder
     (Maybe Double -> Maybe Double -> Maybe [Text] -> RuleEntry)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Double)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"maxRemainingEpss"
        GroupDecoder
  (Maybe Double -> Maybe Double -> Maybe [Text] -> RuleEntry)
-> GroupDecoder (Maybe Double)
-> GroupDecoder (Maybe Double -> Maybe [Text] -> RuleEntry)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Double)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"minCvssGap"
        GroupDecoder (Maybe Double -> Maybe [Text] -> RuleEntry)
-> GroupDecoder (Maybe Double)
-> GroupDecoder (Maybe [Text] -> RuleEntry)
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe Double)
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalPlainKey Key
"minEpssGap"
        -- An empty value would read as both phases, and the Dredger deletes on that reading.
        GroupDecoder (Maybe [Text] -> RuleEntry)
-> GroupDecoder (Maybe [Text]) -> GroupDecoder RuleEntry
forall a b.
GroupDecoder (a -> b) -> GroupDecoder a -> GroupDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> GroupDecoder (Maybe [Text])
forall a. FromJSON a => Key -> GroupDecoder (Maybe a)
optionalNonNullKey Key
"appliesTo"