-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

{- | Project decoded PyPI files into releases keyed by canonical PEP 440 versions.
The same filename parser supplies coordinates for upstream projection and inbound routes. An upload's file name passes a stricter form of it.
-}
module Ecluse.Core.Registry.PyPI.Project (
    -- * Projection
    projectSimpleIndex,
    ProjectedFile,
    projectFile,
    projectedEntryKey,

    -- * File coordinates
    FileCoordinate (..),
    fcVersionKey,
    DistributionKind (..),
    fileCoordinate,
    builtFileCoordinate,
    FilenameMemo,
    filenameMemo,
    readCoordinate,
    readLatestCoordinate,

    -- * Name validation
    projectName,
    canonicalName,
    isCanonicalName,
    isNameSeparator,
    pypiNameLeadChars,
) where

import Data.Aeson (toJSON)
import Data.Char (isAlphaNum, isAscii, isDigit)
import Data.Map.Strict qualified as Map
import Data.Text qualified as T
import Data.Time (UTCTime)

import Ecluse.Core.Ecosystem (Ecosystem (PyPI))
import Ecluse.Core.Package (
    Artifact (..),
    Availability (Available, Yanked),
    CodeExecSignal (NoCodeOnInstall, RunsCodeOnInstall),
    DependencySources (DependencySourcesUnknown),
    Hash,
    InvalidEntry,
    InvalidEntryKind (InvalidIndexFile),
    PackageDetails (..),
    PackageInfo (..),
    PackageName,
    canonicalise,
    mkHash,
    mkInvalidEntry,
    mkPackageName,
    parseHashAlg,
    renderPackageName,
 )
import Ecluse.Core.Package.Entry (EntryKey)
import Ecluse.Core.Registry (ParseError (..))
import Ecluse.Core.Registry.PyPI.Wire (
    IndexFile (..),
    YankState (FileWithdrawn),
 )
import Ecluse.Core.Registry.WireSupport (
    nameComponentWith,
    newestUpload,
    withinNameLimit,
 )
import Ecluse.Core.Strict (strictElements)
import Ecluse.Core.Version (Version, canonicalPep440, renderVersion, selectLatest)

-- | A filename's canonical release and distribution kind.
data FileCoordinate = FileCoordinate
    { FileCoordinate -> Version
fcVersion :: Version
    -- ^ The file's version under its canonical PEP 440 spelling.
    , FileCoordinate -> DistributionKind
fcKind :: DistributionKind
    }
    deriving stock (FileCoordinate -> FileCoordinate -> Bool
(FileCoordinate -> FileCoordinate -> Bool)
-> (FileCoordinate -> FileCoordinate -> Bool) -> Eq FileCoordinate
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FileCoordinate -> FileCoordinate -> Bool
== :: FileCoordinate -> FileCoordinate -> Bool
$c/= :: FileCoordinate -> FileCoordinate -> Bool
/= :: FileCoordinate -> FileCoordinate -> Bool
Eq, Int -> FileCoordinate -> ShowS
[FileCoordinate] -> ShowS
FileCoordinate -> [Char]
(Int -> FileCoordinate -> ShowS)
-> (FileCoordinate -> [Char])
-> ([FileCoordinate] -> ShowS)
-> Show FileCoordinate
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FileCoordinate -> ShowS
showsPrec :: Int -> FileCoordinate -> ShowS
$cshow :: FileCoordinate -> [Char]
show :: FileCoordinate -> [Char]
$cshowList :: [FileCoordinate] -> ShowS
showList :: [FileCoordinate] -> ShowS
Show)

-- | The release key: the file's version in canonical PEP 440 form.
fcVersionKey :: FileCoordinate -> Text
fcVersionKey :: FileCoordinate -> Text
fcVersionKey = Version -> Text
renderVersion (Version -> Text)
-> (FileCoordinate -> Version) -> FileCoordinate -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FileCoordinate -> Version
fcVersion

-- | Whether a file is a source distribution, whose install runs its own build, or a wheel.
data DistributionKind = Sdist | Wheel
    deriving stock (DistributionKind -> DistributionKind -> Bool
(DistributionKind -> DistributionKind -> Bool)
-> (DistributionKind -> DistributionKind -> Bool)
-> Eq DistributionKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DistributionKind -> DistributionKind -> Bool
== :: DistributionKind -> DistributionKind -> Bool
$c/= :: DistributionKind -> DistributionKind -> Bool
/= :: DistributionKind -> DistributionKind -> Bool
Eq, Int -> DistributionKind -> ShowS
[DistributionKind] -> ShowS
DistributionKind -> [Char]
(Int -> DistributionKind -> ShowS)
-> (DistributionKind -> [Char])
-> ([DistributionKind] -> ShowS)
-> Show DistributionKind
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DistributionKind -> ShowS
showsPrec :: Int -> DistributionKind -> ShowS
$cshow :: DistributionKind -> [Char]
show :: DistributionKind -> [Char]
$cshowList :: [DistributionKind] -> ShowS
showList :: [DistributionKind] -> ShowS
Show)

-- | A file's domain facts or identity diagnostic, independent of its decoded wire record.
data ProjectedFile
    = ReleaseFile {-# UNPACK #-} !FileRelease
    | UncoordinatedFile EntryKey InvalidEntry

data FileRelease = FileRelease
    { FileRelease -> Artifact
frArtifact :: Artifact
    , FileRelease -> Version
frVersion :: Version
    , FileRelease -> DistributionKind
frKind :: DistributionKind
    , FileRelease -> Maybe UTCTime
frPublishedAt :: Maybe UTCTime
    , FileRelease -> YankState
frYanked :: YankState
    }

-- | Project before retaining a file, so distribution hash maps and wire-only fields can be released.
projectFile :: IndexFile -> Maybe FileCoordinate -> ProjectedFile
projectFile :: IndexFile -> Maybe FileCoordinate -> ProjectedFile
projectFile IndexFile
file = \case
    Just FileCoordinate
coordinate ->
        FileRelease -> ProjectedFile
ReleaseFile
            FileRelease
                { frArtifact :: Artifact
frArtifact = IndexFile -> Artifact
projectArtifact IndexFile
file
                , frVersion :: Version
frVersion = FileCoordinate -> Version
fcVersion FileCoordinate
coordinate
                , frKind :: DistributionKind
frKind = FileCoordinate -> DistributionKind
fcKind FileCoordinate
coordinate
                , frPublishedAt :: Maybe UTCTime
frPublishedAt = IndexFile -> Maybe UTCTime
ifUploadTime IndexFile
file
                , frYanked :: YankState
frYanked = IndexFile -> YankState
ifYanked IndexFile
file
                }
    Maybe FileCoordinate
Nothing -> EntryKey -> InvalidEntry -> ProjectedFile
UncoordinatedFile (IndexFile -> EntryKey
ifEntryKey IndexFile
file) (IndexFile -> InvalidEntry
uncoordinatedDrop IndexFile
file)

-- | The original array coordinate remains available for replay, including rejected filenames.
projectedEntryKey :: ProjectedFile -> EntryKey
projectedEntryKey :: ProjectedFile -> EntryKey
projectedEntryKey = \case
    ReleaseFile FileRelease
file -> Artifact -> EntryKey
artEntryKey (FileRelease -> Artifact
frArtifact FileRelease
file)
    UncoordinatedFile EntryKey
key InvalidEntry
_ -> EntryKey
key

-- | Group files supplied in reverse source order, keeping artifact and diagnostic order. Decode errors come first.
projectSimpleIndex :: PackageName -> [InvalidEntry] -> [ProjectedFile] -> PackageInfo
projectSimpleIndex :: PackageName -> [InvalidEntry] -> [ProjectedFile] -> PackageInfo
projectSimpleIndex PackageName
name [InvalidEntry]
invalid [ProjectedFile]
files =
    PackageInfo
        { infoName :: PackageName
infoName = PackageName
name
        , infoVersions :: Map Text PackageDetails
infoVersions = Map Text PackageDetails
versions
        , infoDistTags :: Map Text Version
infoDistTags = Map Text PackageDetails -> Map Text Version
latestTag Map Text PackageDetails
versions
        , infoInvalidEntries :: [InvalidEntry]
infoInvalidEntries = [InvalidEntry] -> [InvalidEntry]
forall (t :: * -> *) a. Foldable t => t a -> t a
strictElements ([InvalidEntry]
invalid [InvalidEntry] -> [InvalidEntry] -> [InvalidEntry]
forall a. Semigroup a => a -> a -> a
<> [InvalidEntry]
fileDrops)
        }
  where
    (Map Text PackageDetails
versions, [InvalidEntry]
fileDrops) = PackageName
-> [ProjectedFile] -> (Map Text PackageDetails, [InvalidEntry])
projectVersions PackageName
name [ProjectedFile]
files

projectVersions :: PackageName -> [ProjectedFile] -> (Map Text PackageDetails, [InvalidEntry])
projectVersions :: PackageName
-> [ProjectedFile] -> (Map Text PackageDetails, [InvalidEntry])
projectVersions PackageName
name [ProjectedFile]
files = (OpenRun -> Map Text PackageDetails -> Map Text PackageDetails
closeRun OpenRun
run Map Text PackageDetails
byVersion, [InvalidEntry]
drops)
  where
    VersionFold Map Text PackageDetails
byVersion OpenRun
run [InvalidEntry]
drops = (VersionFold -> ProjectedFile -> VersionFold)
-> VersionFold -> [ProjectedFile] -> VersionFold
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (PackageName -> VersionFold -> ProjectedFile -> VersionFold
placeFile PackageName
name) (Map Text PackageDetails -> OpenRun -> [InvalidEntry] -> VersionFold
VersionFold Map Text PackageDetails
forall k a. Map k a
Map.empty OpenRun
NoRun []) [ProjectedFile]
files

-- The open run collects adjacent files of one release, so the map takes one write per run, not per file.
data VersionFold = VersionFold (Map Text PackageDetails) OpenRun [InvalidEntry]

data OpenRun = NoRun | OpenRun Text PackageDetails

placeFile :: PackageName -> VersionFold -> ProjectedFile -> VersionFold
placeFile :: PackageName -> VersionFold -> ProjectedFile -> VersionFold
placeFile PackageName
_ (VersionFold Map Text PackageDetails
byVersion OpenRun
run [InvalidEntry]
drops) (UncoordinatedFile EntryKey
_ InvalidEntry
failure) = Map Text PackageDetails -> OpenRun -> [InvalidEntry] -> VersionFold
VersionFold Map Text PackageDetails
byVersion OpenRun
run (InvalidEntry
failure InvalidEntry -> [InvalidEntry] -> [InvalidEntry]
forall a. a -> [a] -> [a]
: [InvalidEntry]
drops)
placeFile PackageName
name (VersionFold Map Text PackageDetails
byVersion OpenRun
run [InvalidEntry]
drops) (ReleaseFile FileRelease
file) = case OpenRun
run of
    OpenRun Text
open PackageDetails
held | Text
open Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
key -> Map Text PackageDetails -> Maybe PackageDetails -> VersionFold
extendRun Map Text PackageDetails
byVersion (PackageDetails -> Maybe PackageDetails
forall a. a -> Maybe a
Just PackageDetails
held)
    OpenRun
_ -> Map Text PackageDetails -> Maybe PackageDetails -> VersionFold
extendRun Map Text PackageDetails
settled (Text -> Map Text PackageDetails -> Maybe PackageDetails
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
key Map Text PackageDetails
settled)
  where
    key :: Text
key = Version -> Text
renderVersion (FileRelease -> Version
frVersion FileRelease
file)
    settled :: Map Text PackageDetails
settled = OpenRun -> Map Text PackageDetails -> Map Text PackageDetails
closeRun OpenRun
run Map Text PackageDetails
byVersion
    extendRun :: Map Text PackageDetails -> Maybe PackageDetails -> VersionFold
extendRun Map Text PackageDetails
into Maybe PackageDetails
held = Map Text PackageDetails -> OpenRun -> [InvalidEntry] -> VersionFold
VersionFold Map Text PackageDetails
into (Text -> PackageDetails -> OpenRun
OpenRun Text
key (PackageName
-> FileRelease -> Maybe PackageDetails -> PackageDetails
projectDetails PackageName
name FileRelease
file Maybe PackageDetails
held)) [InvalidEntry]
drops

closeRun :: OpenRun -> Map Text PackageDetails -> Map Text PackageDetails
closeRun :: OpenRun -> Map Text PackageDetails -> Map Text PackageDetails
closeRun = \case
    OpenRun
NoRun -> Map Text PackageDetails -> Map Text PackageDetails
forall a. a -> a
id
    OpenRun Text
key PackageDetails
details -> Text
-> PackageDetails
-> Map Text PackageDetails
-> Map Text PackageDetails
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Text
key PackageDetails
details

-- 'mkInvalidEntry' reduces the location to its authority before logging.
uncoordinatedDrop :: IndexFile -> InvalidEntry
uncoordinatedDrop :: IndexFile -> InvalidEntry
uncoordinatedDrop IndexFile
file =
    InvalidEntryKind -> Text -> Value -> Text -> InvalidEntry
mkInvalidEntry
        InvalidEntryKind
InvalidIndexFile
        (IndexFile -> Text
ifFilename IndexFile
file)
        (Text -> Value
forall a. ToJSON a => a -> Value
toJSON (IndexFile -> Text
ifUrl IndexFile
file))
        Text
"file name names no PEP 440 release of this project"

latestTag :: Map Text PackageDetails -> Map Text Version
latestTag :: Map Text PackageDetails -> Map Text Version
latestTag Map Text PackageDetails
versions =
    Map Text Version
-> (Version -> Map Text Version)
-> Maybe Version
-> Map Text Version
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Map Text Version
forall k a. Map k a
Map.empty (Text -> Version -> Map Text Version
forall k a. k -> a -> Map k a
Map.singleton Text
"latest") (Maybe Version -> [Version] -> Maybe Version
selectLatest Maybe Version
forall a. Maybe a
Nothing ((PackageDetails -> Version) -> [PackageDetails] -> [Version]
forall a b. (a -> b) -> [a] -> [b]
map PackageDetails -> Version
pkgVersion (Map Text PackageDetails -> [PackageDetails]
forall k a. Map k a -> [a]
Map.elems Map Text PackageDetails
versions)))

-- A retained release must not keep its decoded files alive.
projectDetails :: PackageName -> FileRelease -> Maybe PackageDetails -> PackageDetails
projectDetails :: PackageName
-> FileRelease -> Maybe PackageDetails -> PackageDetails
projectDetails PackageName
name FileRelease
file Maybe PackageDetails
held =
    Artifact
artifact Artifact -> PackageDetails -> PackageDetails
forall a b. a -> b -> b
`seq` case Maybe PackageDetails
held of
        Maybe PackageDetails
Nothing ->
            PackageDetails
                { pkgName :: PackageName
pkgName = PackageName
name
                , pkgVersion :: Version
pkgVersion = FileRelease -> Version
frVersion FileRelease
file
                , pkgPublishedAt :: Maybe UTCTime
pkgPublishedAt = FileRelease -> Maybe UTCTime
frPublishedAt FileRelease
file
                , pkgInstallCode :: CodeExecSignal
pkgInstallCode = DistributionKind -> CodeExecSignal -> CodeExecSignal
releaseInstallCode (FileRelease -> DistributionKind
frKind FileRelease
file) CodeExecSignal
NoCodeOnInstall
                , pkgDependencySources :: DependencySources
pkgDependencySources = DependencySources
DependencySourcesUnknown
                , pkgAvailability :: Availability
pkgAvailability = YankState -> Availability -> Availability
releaseAvailability (FileRelease -> YankState
frYanked FileRelease
file) Availability
Yanked
                , pkgArtifacts :: NonEmpty Artifact
pkgArtifacts = Artifact
artifact Artifact -> [Artifact] -> NonEmpty Artifact
forall a. a -> [a] -> NonEmpty a
:| []
                }
        Just PackageDetails
details ->
            let artifacts :: [Artifact]
artifacts = NonEmpty Artifact -> [Artifact]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (PackageDetails -> NonEmpty Artifact
pkgArtifacts PackageDetails
details)
             in [Artifact]
artifacts [Artifact] -> PackageDetails -> PackageDetails
forall a b. a -> b -> b
`seq`
                    PackageDetails
details
                        { pkgVersion = frVersion file
                        , pkgPublishedAt = newestUpload (frPublishedAt file) (pkgPublishedAt details)
                        , pkgInstallCode = releaseInstallCode (frKind file) (pkgInstallCode details)
                        , pkgAvailability = releaseAvailability (frYanked file) (pkgAvailability details)
                        , pkgArtifacts = artifact :| artifacts
                        }
  where
    artifact :: Artifact
artifact = FileRelease -> Artifact
frArtifact FileRelease
file

releaseInstallCode :: DistributionKind -> CodeExecSignal -> CodeExecSignal
releaseInstallCode :: DistributionKind -> CodeExecSignal -> CodeExecSignal
releaseInstallCode DistributionKind
kind CodeExecSignal
previous
    | DistributionKind
kind DistributionKind -> DistributionKind -> Bool
forall a. Eq a => a -> a -> Bool
== DistributionKind
Sdist =
        Text -> CodeExecSignal
RunsCodeOnInstall Text
"offers a source distribution, which runs its own build"
    | Bool
otherwise = CodeExecSignal
previous

-- A release is withdrawn only when PEP 592 withdraws every file of it.
releaseAvailability :: YankState -> Availability -> Availability
releaseAvailability :: YankState -> Availability -> Availability
releaseAvailability YankState
yanked Availability
previous
    | YankState
yanked YankState -> YankState -> Bool
forall a. Eq a => a -> a -> Bool
== YankState
FileWithdrawn = Availability
previous
    | Bool
otherwise = Availability
Available

-- The location stays verbatim. 'Ecluse.Core.Package.Filter' folds its scheme and authority
-- against the egress and host policies afterward.
projectArtifact :: IndexFile -> Artifact
projectArtifact :: IndexFile -> Artifact
projectArtifact IndexFile
file =
    Artifact
        { artEntryKey :: EntryKey
artEntryKey = IndexFile -> EntryKey
ifEntryKey IndexFile
file
        , artFilename :: Text
artFilename = IndexFile -> Text
ifFilename IndexFile
file
        , artUrl :: Text
artUrl = IndexFile -> Text
ifUrl IndexFile
file
        , artHashes :: [Hash]
artHashes = [Hash] -> [Hash]
forall (t :: * -> *) a. Foldable t => t a -> t a
strictElements (((Text, Text) -> Maybe Hash) -> [(Text, Text)] -> [Hash]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Text, Text) -> Maybe Hash
indexHash (Map Text Text -> [(Text, Text)]
forall k a. Map k a -> [(k, a)]
Map.toAscList (IndexFile -> Map Text Text
ifHashes IndexFile
file)))
        , artCoreMetadata :: Maybe CoreMetadata
artCoreMetadata = Maybe CoreMetadata -> Maybe CoreMetadata
forall (t :: * -> *) a. Foldable t => t a -> t a
strictElements (IndexFile -> Maybe CoreMetadata
ifCoreMetadata IndexFile
file)
        , artSize :: Maybe Int
artSize = IndexFile -> Maybe Int
ifSize IndexFile
file
        }

indexHash :: (Text, Text) -> Maybe Hash
indexHash :: (Text, Text) -> Maybe Hash
indexHash (Text
algorithm, Text
digest) = do
    algo <- Either Text HashAlg -> Maybe HashAlg
forall l r. Either l r -> Maybe r
rightToMaybe (Text -> Either Text HashAlg
parseHashAlg Text
algorithm)
    rightToMaybe (mkHash algo digest)

-- | Read a filename's coordinate, rejecting another project, an unknown archive, or invalid PEP 440.
fileCoordinate :: PackageName -> Text -> Maybe FileCoordinate
fileCoordinate :: PackageName -> Text -> Maybe FileCoordinate
fileCoordinate PackageName
name = (Text, DistributionKind) -> Maybe FileCoordinate
partsCoordinate ((Text, DistributionKind) -> Maybe FileCoordinate)
-> (Text -> Maybe (Text, DistributionKind))
-> Text
-> Maybe FileCoordinate
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< FileProject -> Text -> Maybe (Text, DistributionKind)
filenameParts (PackageName -> FileProject
fileProject PackageName
name)

-- | Read as 'fileCoordinate' does, refusing a name no current build tool writes: a non-PEP 625 sdist, or a version not led by a digit or holding @-@.
builtFileCoordinate :: PackageName -> Text -> Maybe FileCoordinate
builtFileCoordinate :: PackageName -> Text -> Maybe FileCoordinate
builtFileCoordinate PackageName
name Text
file = do
    parts@(version, _) <- FileProject -> Text -> Maybe (Text, DistributionKind)
wheelParts FileProject
project Text
file Maybe (Text, DistributionKind)
-> Maybe (Text, DistributionKind) -> Maybe (Text, DistributionKind)
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> FileProject -> Text -> Maybe (Text, DistributionKind)
pep625SdistParts FileProject
project Text
file
    guard (maybe False (isDigit . fst) (T.uncons version) && not (T.any (== '-') version))
    partsCoordinate parts
  where
    project :: FileProject
project = PackageName -> FileProject
fileProject PackageName
name

partsCoordinate :: (Text, DistributionKind) -> Maybe FileCoordinate
partsCoordinate :: (Text, DistributionKind) -> Maybe FileCoordinate
partsCoordinate (Text
version, DistributionKind
kind) = (Version -> DistributionKind -> FileCoordinate
`FileCoordinate` DistributionKind
kind) (Version -> FileCoordinate)
-> Maybe Version -> Maybe FileCoordinate
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe Version
canonicalPep440 Text
version

-- A project's PEP 503 key, prepared once for reading many of its filenames.
data FileProject = FileProject
    { FileProject -> Text
fpCanonical :: Text
    , FileProject -> [Text]
fpChunks :: [Text]
    }

fileProject :: PackageName -> FileProject
fileProject :: PackageName -> FileProject
fileProject PackageName
name = Text -> [Text] -> FileProject
FileProject Text
canonical (HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"-" Text
canonical)
  where
    canonical :: Text
canonical = PackageName -> Text
canonicalName PackageName
name

-- | One read's PEP 440 versions by version text, so the files of one release parse their version once.
data FilenameMemo = FilenameMemo
    { FilenameMemo -> FileProject
memoProject :: FileProject
    , FilenameMemo -> Map Text (Maybe Version)
memoVersions :: Map Text (Maybe Version)
    }

-- | Start a memo for one index read. It ends with the read, so no request inherits its versions.
filenameMemo :: PackageName -> FilenameMemo
filenameMemo :: PackageName -> FilenameMemo
filenameMemo PackageName
name = FileProject -> Map Text (Maybe Version) -> FilenameMemo
FilenameMemo (PackageName -> FileProject
fileProject PackageName
name) Map Text (Maybe Version)
forall k a. Map k a
Map.empty

-- | Read a coordinate as 'fileCoordinate' does, parsing each distinct version text once.
readCoordinate :: FilenameMemo -> Text -> (Maybe FileCoordinate, FilenameMemo)
readCoordinate :: FilenameMemo -> Text -> (Maybe FileCoordinate, FilenameMemo)
readCoordinate = (Text
 -> Maybe Version
 -> Map Text (Maybe Version)
 -> Map Text (Maybe Version))
-> FilenameMemo -> Text -> (Maybe FileCoordinate, FilenameMemo)
readRemembering Text
-> Maybe Version
-> Map Text (Maybe Version)
-> Map Text (Maybe Version)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert

-- | Read as 'readCoordinate' does, retaining only the latest version text for this read.
readLatestCoordinate :: FilenameMemo -> Text -> (Maybe FileCoordinate, FilenameMemo)
readLatestCoordinate :: FilenameMemo -> Text -> (Maybe FileCoordinate, FilenameMemo)
readLatestCoordinate = (Text
 -> Maybe Version
 -> Map Text (Maybe Version)
 -> Map Text (Maybe Version))
-> FilenameMemo -> Text -> (Maybe FileCoordinate, FilenameMemo)
readRemembering (\Text
version Maybe Version
parsed Map Text (Maybe Version)
_ -> Text -> Maybe Version -> Map Text (Maybe Version)
forall k a. k -> a -> Map k a
Map.singleton Text
version Maybe Version
parsed)

readRemembering :: (Text -> Maybe Version -> Map Text (Maybe Version) -> Map Text (Maybe Version)) -> FilenameMemo -> Text -> (Maybe FileCoordinate, FilenameMemo)
readRemembering :: (Text
 -> Maybe Version
 -> Map Text (Maybe Version)
 -> Map Text (Maybe Version))
-> FilenameMemo -> Text -> (Maybe FileCoordinate, FilenameMemo)
readRemembering Text
-> Maybe Version
-> Map Text (Maybe Version)
-> Map Text (Maybe Version)
remember FilenameMemo
memo Text
file = (Maybe FileCoordinate, FilenameMemo)
-> ((Text, DistributionKind)
    -> (Maybe FileCoordinate, FilenameMemo))
-> Maybe (Text, DistributionKind)
-> (Maybe FileCoordinate, FilenameMemo)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Maybe FileCoordinate
forall a. Maybe a
Nothing, FilenameMemo
memo) (Text, DistributionKind) -> (Maybe FileCoordinate, FilenameMemo)
known (FileProject -> Text -> Maybe (Text, DistributionKind)
filenameParts (FilenameMemo -> FileProject
memoProject FilenameMemo
memo) Text
file)
  where
    known :: (Text, DistributionKind) -> (Maybe FileCoordinate, FilenameMemo)
known (Text
version, DistributionKind
kind) = case Text -> Map Text (Maybe Version) -> Maybe (Maybe Version)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
version (FilenameMemo -> Map Text (Maybe Version)
memoVersions FilenameMemo
memo) of
        Just Maybe Version
held -> (DistributionKind -> Maybe Version -> Maybe FileCoordinate
forall {f :: * -> *}.
Functor f =>
DistributionKind -> f Version -> f FileCoordinate
coordinate DistributionKind
kind Maybe Version
held, FilenameMemo
memo)
        Maybe (Maybe Version)
Nothing ->
            let parsed :: Maybe Version
parsed = Text -> Maybe Version
canonicalPep440 Text
version
             in (DistributionKind -> Maybe Version -> Maybe FileCoordinate
forall {f :: * -> *}.
Functor f =>
DistributionKind -> f Version -> f FileCoordinate
coordinate DistributionKind
kind Maybe Version
parsed, FilenameMemo
memo{memoVersions = remember version parsed (memoVersions memo)})
    coordinate :: DistributionKind -> f Version -> f FileCoordinate
coordinate DistributionKind
kind = (Version -> FileCoordinate) -> f Version -> f FileCoordinate
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Version -> DistributionKind -> FileCoordinate
`FileCoordinate` DistributionKind
kind)

filenameParts :: FileProject -> Text -> Maybe (Text, DistributionKind)
filenameParts :: FileProject -> Text -> Maybe (Text, DistributionKind)
filenameParts FileProject
project Text
file = FileProject -> Text -> Maybe (Text, DistributionKind)
wheelParts FileProject
project Text
file Maybe (Text, DistributionKind)
-> Maybe (Text, DistributionKind) -> Maybe (Text, DistributionKind)
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> FileProject -> Text -> Maybe (Text, DistributionKind)
sdistParts FileProject
project Text
file

-- Five or six components qualify, even when build or tag components are empty.
wheelParts :: FileProject -> Text -> Maybe (Text, DistributionKind)
wheelParts :: FileProject -> Text -> Maybe (Text, DistributionKind)
wheelParts FileProject
project Text
file = do
    stem <- Text -> Text -> Maybe Text
T.stripSuffix Text
".whl" Text
file
    let (name, afterName) = T.break (== '-') stem
        (version, tags) = T.break (== '-') (T.drop 1 afterName)
    guard (wheelTailFits tags)
    guard (canonicalise PyPI name == fpCanonical project)
    pure (version, Wheel)

data WheelDelimiterCount
    = ZeroDelimiters
    | OneDelimiter
    | TwoDelimiters
    | ThreeDelimiters
    | FourDelimiters
    | ExcessDelimiters

wheelTailFits :: Text -> Bool
wheelTailFits :: Text -> Bool
wheelTailFits Text
tags = case (WheelDelimiterCount -> Char -> WheelDelimiterCount)
-> WheelDelimiterCount -> Text -> WheelDelimiterCount
forall a. (a -> Char -> a) -> a -> Text -> a
T.foldl' WheelDelimiterCount -> Char -> WheelDelimiterCount
advanceCount WheelDelimiterCount
ZeroDelimiters Text
tags of
    WheelDelimiterCount
ThreeDelimiters -> Bool
True
    WheelDelimiterCount
FourDelimiters -> Bool
True
    WheelDelimiterCount
_ -> Bool
False
  where
    advanceCount :: WheelDelimiterCount -> Char -> WheelDelimiterCount
advanceCount WheelDelimiterCount
delimiterCount Char
character
        | Char
character Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'-' = WheelDelimiterCount
delimiterCount
        | Bool
otherwise = case WheelDelimiterCount
delimiterCount of
            WheelDelimiterCount
ZeroDelimiters -> WheelDelimiterCount
OneDelimiter
            WheelDelimiterCount
OneDelimiter -> WheelDelimiterCount
TwoDelimiters
            WheelDelimiterCount
TwoDelimiters -> WheelDelimiterCount
ThreeDelimiters
            WheelDelimiterCount
ThreeDelimiters -> WheelDelimiterCount
FourDelimiters
            WheelDelimiterCount
FourDelimiters -> WheelDelimiterCount
ExcessDelimiters
            WheelDelimiterCount
ExcessDelimiters -> WheelDelimiterCount
ExcessDelimiters
{-# INLINE wheelTailFits #-}

-- Legacy project names and versions can share separators. Keep the longest matching project prefix.
sdistParts :: FileProject -> Text -> Maybe (Text, DistributionKind)
sdistParts :: FileProject -> Text -> Maybe (Text, DistributionKind)
sdistParts FileProject
project Text
file = do
    stem <- [Maybe Text] -> Maybe Text
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum ((Text -> Maybe Text) -> [Text] -> [Maybe Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Text -> Maybe Text
`T.stripSuffix` Text
file) [Text]
sdistSuffixes)
    version <- afterProjectName project stem
    pure (version, Sdist)

sdistSuffixes :: [Text]
sdistSuffixes :: [Text]
sdistSuffixes = [Text
".tar.gz", Text
".tgz", Text
".zip", Text
".tar.bz2", Text
".tar.xz"]

-- PEP 625 spells the project as its canonical name with `_` for `-`, so the project part holds no `-`.
pep625SdistParts :: FileProject -> Text -> Maybe (Text, DistributionKind)
pep625SdistParts :: FileProject -> Text -> Maybe (Text, DistributionKind)
pep625SdistParts FileProject
project Text
file = do
    stem <- Text -> Text -> Maybe Text
T.stripSuffix Text
".tar.gz" Text
file
    version <- T.stripPrefix (T.replace "-" "_" (fpCanonical project) <> "-") stem
    pure (version, Sdist)

-- Compare disjoint chunks so unauthenticated filenames cannot trigger repeated prefix work.
afterProjectName :: FileProject -> Text -> Maybe Text
afterProjectName :: FileProject -> Text -> Maybe Text
afterProjectName FileProject
project Text
stem
    | Text -> Bool
T.null (FileProject -> Text
fpCanonical FileProject
project) = do
        (separator, _) <- Text -> Maybe (Char, Text)
T.uncons Text
stem
        guard (isNameSeparator separator)
        pure (T.dropWhile isNameSeparator stem)
    | Bool
otherwise = [Text] -> Text -> Maybe Text
matchProjectChunks (FileProject -> [Text]
fpChunks FileProject
project) ((Char -> Bool) -> Text -> Text
T.dropWhile Char -> Bool
isNameSeparator Text
stem)

matchProjectChunks :: [Text] -> Text -> Maybe Text
matchProjectChunks :: [Text] -> Text -> Maybe Text
matchProjectChunks [] Text
rest = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
rest
matchProjectChunks (Text
expected : [Text]
remaining) Text
rest = do
    let (Text
chunk, Text
separated) = (Char -> Bool) -> Text -> (Text, Text)
T.break Char -> Bool
isNameSeparator Text
rest
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Ecosystem -> Text -> Text
canonicalise Ecosystem
PyPI Text
chunk Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
expected)
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not (Text -> Bool
T.null Text
separated))
    [Text] -> Text -> Maybe Text
matchProjectChunks [Text]
remaining ((Char -> Bool) -> Text -> Text
T.dropWhile Char -> Bool
isNameSeparator Text
separated)

-- | The characters PEP 503 treats as one separator when it normalises a name.
isNameSeparator :: Char -> Bool
isNameSeparator :: Char -> Bool
isNameSeparator Char
c = Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'_' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.'

-- | The PEP 503 key used for filename comparison and upstream Simple-index URLs.
canonicalName :: PackageName -> Text
canonicalName :: PackageName -> Text
canonicalName = Ecosystem -> Text -> Text
canonicalise Ecosystem
PyPI (Text -> Text) -> (PackageName -> Text) -> PackageName -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageName -> Text
renderPackageName

-- | Parse one PyPI name component under the shared floor and PEP 508 grammar.
projectName :: Text -> Either ParseError PackageName
projectName :: Text -> Either ParseError PackageName
projectName Text
raw = do
    Text -> Int -> Text -> Either ParseError ()
withinNameLimit Text
"PyPI project name" Int
pypiNameLimit Text
raw
    Ecosystem -> Maybe Scope -> Text -> PackageName
mkPackageName Ecosystem
PyPI Maybe Scope
forall a. Maybe a
Nothing (Text -> PackageName)
-> Either ParseError Text -> Either ParseError PackageName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either ParseError Text
nameComponent Text
raw

-- | Whether the route can claim this name without a canonical-spelling redirect.
isCanonicalName :: Text -> Bool
isCanonicalName :: Text -> Bool
isCanonicalName Text
raw = Ecosystem -> Text -> Text
canonicalise Ecosystem
PyPI Text
raw Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
raw

nameComponent :: Text -> Either ParseError Text
nameComponent :: Text -> Either ParseError Text
nameComponent = Text -> (Text -> Bool) -> Text -> Either ParseError Text
nameComponentWith Text
"PyPI project name" Text -> Bool
usableComponent

-- | Initial characters for partitioning canonical PyPI names during a store walk.
pypiNameLeadChars :: [Char]
pypiNameLeadChars :: [Char]
pypiNameLeadChars = [Char
'a' .. Char
'z'] [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char
'0' .. Char
'9']

usableComponent :: Text -> Bool
usableComponent :: Text -> Bool
usableComponent Text
component =
    (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
nameChar Text
component
        Bool -> Bool -> Bool
&& Bool -> ((Char, Text) -> Bool) -> Maybe (Char, Text) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Char -> Bool
nameEdge (Char -> Bool) -> ((Char, Text) -> Char) -> (Char, Text) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char, Text) -> Char
forall a b. (a, b) -> a
fst) (Text -> Maybe (Char, Text)
T.uncons Text
component)
        Bool -> Bool -> Bool
&& Bool -> ((Text, Char) -> Bool) -> Maybe (Text, Char) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Char -> Bool
nameEdge (Char -> Bool) -> ((Text, Char) -> Char) -> (Text, Char) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Char) -> Char
forall a b. (a, b) -> b
snd) (Text -> Maybe (Text, Char)
T.unsnoc Text
component)

nameChar :: Char -> Bool
nameChar :: Char -> Bool
nameChar Char
ch = Char -> Bool
nameEdge Char
ch Bool -> Bool -> Bool
|| Char -> Bool
isNameSeparator Char
ch

nameEdge :: Char -> Bool
nameEdge :: Char -> Bool
nameEdge Char
ch = Char -> Bool
isAscii Char
ch Bool -> Bool -> Bool
&& Char -> Bool
isAlphaNum Char
ch

pypiNameLimit :: Int
pypiNameLimit :: Int
pypiNameLimit = Int
100