module Ecluse.Core.Registry.Metadata (
MetadataClient (..),
Manifest (..),
ContentDigest,
digestBytes,
VersionDoc (..),
VersionRead (..),
MetadataError (..),
metadataResponse,
VersionEvaluation (..),
fetchVersionDetails,
versionEvaluation,
versionTransience,
) where
import Ecluse.Core.Package (PackageDetails, PackageInfo, PackageName)
import Ecluse.Core.Registry (BodyOutcome (SuccessBody, UnreadStatus), FetchFault (FetchBoundExceeded), isAuthorisationFailure)
import Ecluse.Core.Registry.CachedDocument (CachedDoc)
import Ecluse.Core.Rules.Types (Transience (WillResolve, WontResolve))
import Ecluse.Core.Security (LimitError (..))
import Ecluse.Core.Snapshot (ContentDigest, digestBytes)
import Ecluse.Core.Version (Version)
data Manifest = Manifest
{ Manifest -> PackageInfo
manifestInfo :: PackageInfo
, Manifest -> CachedDoc
manifestRaw :: CachedDoc
, Manifest -> Int
manifestBodyBytes :: Int
, Manifest -> ContentDigest
manifestDigest :: ContentDigest
}
data MetadataClient = MetadataClient
{ MetadataClient -> PackageName -> IO (Either MetadataError Manifest)
fetchFullManifest :: PackageName -> IO (Either MetadataError Manifest)
, MetadataClient
-> PackageName -> Version -> IO (Either MetadataError VersionRead)
fetchVersionMetadata :: PackageName -> Version -> IO (Either MetadataError VersionRead)
}
data VersionDoc = VersionDoc
{ VersionDoc -> PackageDetails
vdDetails :: PackageDetails
, VersionDoc -> Maybe CachedDoc
vdRaw :: Maybe CachedDoc
}
deriving stock (VersionDoc -> VersionDoc -> Bool
(VersionDoc -> VersionDoc -> Bool)
-> (VersionDoc -> VersionDoc -> Bool) -> Eq VersionDoc
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VersionDoc -> VersionDoc -> Bool
== :: VersionDoc -> VersionDoc -> Bool
$c/= :: VersionDoc -> VersionDoc -> Bool
/= :: VersionDoc -> VersionDoc -> Bool
Eq, Int -> VersionDoc -> ShowS
[VersionDoc] -> ShowS
VersionDoc -> String
(Int -> VersionDoc -> ShowS)
-> (VersionDoc -> String)
-> ([VersionDoc] -> ShowS)
-> Show VersionDoc
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VersionDoc -> ShowS
showsPrec :: Int -> VersionDoc -> ShowS
$cshow :: VersionDoc -> String
show :: VersionDoc -> String
$cshowList :: [VersionDoc] -> ShowS
showList :: [VersionDoc] -> ShowS
Show)
data VersionRead = VersionRead
{ VersionRead -> Maybe VersionDoc
vrVersion :: Maybe VersionDoc
, VersionRead -> Int
vrBodyBytes :: Int
, VersionRead -> Maybe Version
vrUpstreamLatest :: Maybe Version
}
deriving stock (VersionRead -> VersionRead -> Bool
(VersionRead -> VersionRead -> Bool)
-> (VersionRead -> VersionRead -> Bool) -> Eq VersionRead
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VersionRead -> VersionRead -> Bool
== :: VersionRead -> VersionRead -> Bool
$c/= :: VersionRead -> VersionRead -> Bool
/= :: VersionRead -> VersionRead -> Bool
Eq, Int -> VersionRead -> ShowS
[VersionRead] -> ShowS
VersionRead -> String
(Int -> VersionRead -> ShowS)
-> (VersionRead -> String)
-> ([VersionRead] -> ShowS)
-> Show VersionRead
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VersionRead -> ShowS
showsPrec :: Int -> VersionRead -> ShowS
$cshow :: VersionRead -> String
show :: VersionRead -> String
$cshowList :: [VersionRead] -> ShowS
showList :: [VersionRead] -> ShowS
Show)
data MetadataError
=
MetadataAuthorisationFailure Int
|
MetadataAbsent
|
MetadataHttpFailure Int
|
MetadataFetch FetchFault
|
MetadataBoundExceeded LimitError
|
MetadataUndecodable
|
MetadataNameMismatch Text
deriving stock (MetadataError -> MetadataError -> Bool
(MetadataError -> MetadataError -> Bool)
-> (MetadataError -> MetadataError -> Bool) -> Eq MetadataError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MetadataError -> MetadataError -> Bool
== :: MetadataError -> MetadataError -> Bool
$c/= :: MetadataError -> MetadataError -> Bool
/= :: MetadataError -> MetadataError -> Bool
Eq, Int -> MetadataError -> ShowS
[MetadataError] -> ShowS
MetadataError -> String
(Int -> MetadataError -> ShowS)
-> (MetadataError -> String)
-> ([MetadataError] -> ShowS)
-> Show MetadataError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MetadataError -> ShowS
showsPrec :: Int -> MetadataError -> ShowS
$cshow :: MetadataError -> String
show :: MetadataError -> String
$cshowList :: [MetadataError] -> ShowS
showList :: [MetadataError] -> ShowS
Show)
data VersionEvaluation
=
VersionPresent VersionDoc (Maybe Version)
|
VersionMissing
|
VersionMetadataUnavailable
deriving stock (VersionEvaluation -> VersionEvaluation -> Bool
(VersionEvaluation -> VersionEvaluation -> Bool)
-> (VersionEvaluation -> VersionEvaluation -> Bool)
-> Eq VersionEvaluation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VersionEvaluation -> VersionEvaluation -> Bool
== :: VersionEvaluation -> VersionEvaluation -> Bool
$c/= :: VersionEvaluation -> VersionEvaluation -> Bool
/= :: VersionEvaluation -> VersionEvaluation -> Bool
Eq, Int -> VersionEvaluation -> ShowS
[VersionEvaluation] -> ShowS
VersionEvaluation -> String
(Int -> VersionEvaluation -> ShowS)
-> (VersionEvaluation -> String)
-> ([VersionEvaluation] -> ShowS)
-> Show VersionEvaluation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VersionEvaluation -> ShowS
showsPrec :: Int -> VersionEvaluation -> ShowS
$cshow :: VersionEvaluation -> String
show :: VersionEvaluation -> String
$cshowList :: [VersionEvaluation] -> ShowS
showList :: [VersionEvaluation] -> ShowS
Show)
fetchVersionDetails :: MetadataClient -> PackageName -> Version -> IO VersionEvaluation
fetchVersionDetails :: MetadataClient -> PackageName -> Version -> IO VersionEvaluation
fetchVersionDetails MetadataClient
client PackageName
name Version
version =
Either MetadataError VersionRead -> VersionEvaluation
versionEvaluation (Either MetadataError VersionRead -> VersionEvaluation)
-> IO (Either MetadataError VersionRead) -> IO VersionEvaluation
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MetadataClient
-> PackageName -> Version -> IO (Either MetadataError VersionRead)
fetchVersionMetadata MetadataClient
client PackageName
name Version
version
versionEvaluation :: Either MetadataError VersionRead -> VersionEvaluation
versionEvaluation :: Either MetadataError VersionRead -> VersionEvaluation
versionEvaluation = \case
Left MetadataError
_ -> VersionEvaluation
VersionMetadataUnavailable
Right VersionRead
versionRead -> case VersionRead -> Maybe VersionDoc
vrVersion VersionRead
versionRead of
Maybe VersionDoc
Nothing -> VersionEvaluation
VersionMissing
Just VersionDoc
present -> VersionDoc -> Maybe Version -> VersionEvaluation
VersionPresent VersionDoc
present (VersionRead -> Maybe Version
vrUpstreamLatest VersionRead
versionRead)
versionTransience :: VersionEvaluation -> Maybe Transience
versionTransience :: VersionEvaluation -> Maybe Transience
versionTransience = \case
VersionEvaluation
VersionMetadataUnavailable -> Transience -> Maybe Transience
forall a. a -> Maybe a
Just (Maybe RetryAfter -> Transience
WillResolve Maybe RetryAfter
forall a. Maybe a
Nothing)
VersionEvaluation
VersionMissing -> Transience -> Maybe Transience
forall a. a -> Maybe a
Just Transience
WontResolve
VersionPresent{} -> Maybe Transience
forall a. Maybe a
Nothing
metadataResponse :: Either FetchFault (BodyOutcome a) -> Either MetadataError a
metadataResponse :: forall a.
Either FetchFault (BodyOutcome a) -> Either MetadataError a
metadataResponse = \case
Left FetchFault
fault -> MetadataError -> Either MetadataError a
forall a b. a -> Either a b
Left (FetchFault -> MetadataError
metadataFetchError FetchFault
fault)
Right (SuccessBody Int
_ a
parsed) -> a -> Either MetadataError a
forall a b. b -> Either a b
Right a
parsed
Right (UnreadStatus Int
404) -> MetadataError -> Either MetadataError a
forall a b. a -> Either a b
Left MetadataError
MetadataAbsent
Right (UnreadStatus Int
code)
| Int -> Bool
isAuthorisationFailure Int
code -> MetadataError -> Either MetadataError a
forall a b. a -> Either a b
Left (Int -> MetadataError
MetadataAuthorisationFailure Int
code)
| Bool
otherwise -> MetadataError -> Either MetadataError a
forall a b. a -> Either a b
Left (Int -> MetadataError
MetadataHttpFailure Int
code)
metadataFetchError :: FetchFault -> MetadataError
metadataFetchError :: FetchFault -> MetadataError
metadataFetchError FetchFault
fault = case FetchFault
fault of
FetchBoundExceeded LimitError
limit -> case LimitError
limit of
BodyTooLarge BodyLimit
_ -> FetchFault -> MetadataError
MetadataFetch FetchFault
fault
TooManyVersions Int
_ Int
_ -> LimitError -> MetadataError
MetadataBoundExceeded LimitError
limit
TooManyArtifacts Int
_ Int
_ -> LimitError -> MetadataError
MetadataBoundExceeded LimitError
limit
TooDeeplyNested Int
_ -> LimitError -> MetadataError
MetadataBoundExceeded LimitError
limit
FetchFault
_ -> FetchFault -> MetadataError
MetadataFetch FetchFault
fault