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

-- | Origin-scoped sparse discovery and checksum-bound native download templates.
module Ecluse.Core.Registry.Cargo.Discovery (
    CargoDiscovery,
    cargoDiscovery,
    parseDiscoveryWithDepth,
    discoveryDl,
    discoveryApi,
    discoveryIndex,
    renderCargoDiscovery,
    CargoArtifactCoordinate,
    cargoArtifactCoordinate,
    coordinateName,
    coordinateVersion,
    coordinateFilename,
    coordinateChecksum,
    bindCargoDownload,
) where

import Data.Aeson (Value (..), encode, object, (.=))
import Data.Aeson.Key qualified as Key
import Data.Aeson.KeyMap qualified as KeyMap
import Data.ByteString qualified as BS
import Data.Char (isSpace)
import Data.Text qualified as T

import Ecluse.Core.Ecosystem (Ecosystem (Cargo))
import Ecluse.Core.Package (Hash, HashAlg (SHA256), hashAlg, hashValue, mkHash)
import Ecluse.Core.Package.Filter.Internal (ArtifactOrigin, artifactOrigin, checkArtifactAddress)
import Ecluse.Core.Registry (FetchFault (FetchUrlUnformable))
import Ecluse.Core.Registry.Cargo.Project (cargoArchiveFilename, projectName)
import Ecluse.Core.Registry.Cargo.Request (cargoArtifactHosts, cargoConfigRequest, cargoPrefix)
import Ecluse.Core.Registry.Cargo.Wire (CargoRecordError (..), decodeCargoRecord)
import Ecluse.Core.Registry.Exchange (boundedIncrementalRead, formThen, withSuccessBodyUsing)
import Ecluse.Core.Registry.Metadata (MetadataError (MetadataBoundExceeded, MetadataUndecodable), metadataResponse)
import Ecluse.Core.Registry.Origin (OriginClient (..), originBaseUrl)
import Ecluse.Core.Registry.Public (ReadControl (..))
import Ecluse.Core.Registry.Request (CredentialMapping, presentedRequest)
import Ecluse.Core.Security (AllowedHostPorts, BodyLimit (MetadataBodyLimit), LimitError (BodyTooLarge), Limits (maxMetadataBytes, maxNestingDepth, progressFloor), ecosystemArtifactAuthorities)
import Ecluse.Core.Server.Path (Filename, encodeComponent, mkFilename)
import Ecluse.Core.Telemetry.Upstream (PublicReadKind (Listing))
import Ecluse.Core.Text (httpPrefix, httpsPrefix, isPrefixOfLowered)
import Ecluse.Core.Version (Version, mkVersion, renderVersion)
import Ecluse.Core.Version.Cargo (parseCargo)

-- | A checked response from one authorised index origin. Its URLs grant no new authority.
data CargoDiscovery = CargoDiscovery !Text !(Maybe Text) !Text !ArtifactOrigin

-- | The checked native template, with markers still unexpanded.
discoveryDl :: CargoDiscovery -> Text
discoveryDl :: CargoDiscovery -> Text
discoveryDl (CargoDiscovery Text
template Maybe Text
_ Text
_ ArtifactOrigin
_) = Text
template

-- | The advertised API remains a location hint, never an upload credential grant.
discoveryApi :: CargoDiscovery -> Maybe Text
discoveryApi :: CargoDiscovery -> Maybe Text
discoveryApi (CargoDiscovery Text
_ Maybe Text
api Text
_ ArtifactOrigin
_) = Maybe Text
api

-- | The complete selected index root, including any configured base path.
discoveryIndex :: CargoDiscovery -> Text
discoveryIndex :: CargoDiscovery -> Text
discoveryIndex (CargoDiscovery Text
_ Maybe Text
_ Text
index ArtifactOrigin
_) = Text
index

discoveryOrigin :: CargoDiscovery -> ArtifactOrigin
discoveryOrigin :: CargoDiscovery -> ArtifactOrigin
discoveryOrigin (CargoDiscovery Text
_ Maybe Text
_ Text
_ ArtifactOrigin
origin) = ArtifactOrigin
origin

-- | Read bounded discovery under @mapping@, through the same transport and public control as index metadata.
cargoDiscovery :: CredentialMapping -> OriginClient -> IO (Either MetadataError CargoDiscovery)
cargoDiscovery :: CredentialMapping
-> OriginClient -> IO (Either MetadataError CargoDiscovery)
cargoDiscovery CredentialMapping
mapping OriginClient
origin = do
    response <- (UrlFormationError -> FetchFault)
-> (Request
    -> IO (Either FetchFault (BodyOutcome (Int, ByteString))))
-> Either UrlFormationError Request
-> IO (Either FetchFault (BodyOutcome (Int, ByteString)))
forall fault a.
(UrlFormationError -> fault)
-> (Request -> IO (Either fault a))
-> Either UrlFormationError Request
-> IO (Either fault a)
formThen UrlFormationError -> FetchFault
FetchUrlUnformable Request -> IO (Either FetchFault (BodyOutcome (Int, ByteString)))
exchange (CredentialMapping
-> Maybe ClientCredential
-> (Maybe ClientCredential -> Either UrlFormationError Request)
-> Either UrlFormationError Request
forall e.
CredentialMapping
-> Maybe ClientCredential
-> (Maybe ClientCredential -> Either e Request)
-> Either e Request
presentedRequest CredentialMapping
mapping (OriginClient -> Maybe ClientCredential
ocToken OriginClient
origin) (BaseRequest
-> Maybe ClientCredential -> Either UrlFormationError Request
cargoConfigRequest (OriginClient -> BaseRequest
ocBaseRequest OriginClient
origin)))
    pure $ do
        (_, bytes) <- metadataResponse response
        parseDiscoveryWithDepth (min 32 (maxNestingDepth limits)) (ecosystemArtifactAuthorities cargoArtifactHosts) (originBaseUrl origin) bytes
  where
    limits :: Limits
limits = OriginClient -> Limits
ocLimits OriginClient
origin
    bound :: BodyLimit
bound = Int -> BodyLimit
MetadataBodyLimit (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
65536 (Limits -> Int
maxMetadataBytes Limits
limits))
    control :: ReadControl
control = ReadControl
-> (PublicControl -> ReadControl)
-> Maybe PublicControl
-> ReadControl
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ReadControl
Uncontrolled (\PublicControl
handle -> PublicControl -> PublicReadKind -> ReadControl
Controlled PublicControl
handle (PublicReadKind -> Maybe PublicReadKind -> PublicReadKind
forall a. a -> Maybe a -> a
fromMaybe PublicReadKind
Listing (OriginClient -> Maybe PublicReadKind
ocReadKind OriginClient
origin))) (OriginClient -> Maybe PublicControl
ocPublicControl OriginClient
origin)
    exchange :: Request -> IO (Either FetchFault (BodyOutcome (Int, ByteString)))
exchange = ReadControl
-> ExchangeClient
-> ProgressFloor
-> (IO ByteString -> IO (Either LimitError (Int, ByteString)))
-> Request
-> IO (Either FetchFault (BodyOutcome (Int, ByteString)))
forall a.
ReadControl
-> ExchangeClient
-> ProgressFloor
-> (IO ByteString -> IO (Either LimitError a))
-> Request
-> IO (Either FetchFault (BodyOutcome a))
withSuccessBodyUsing ReadControl
control (OriginClient -> ExchangeClient
ocClient OriginClient
origin) (Limits -> ProgressFloor
progressFloor Limits
limits) ((Int -> IO ())
-> BodyLimit
-> IO ByteString
-> IO (Either LimitError (Int, ByteString))
boundedIncrementalRead (OriginClient -> Int -> IO ()
ocChargeFullRead OriginClient
origin) BodyLimit
bound)

-- | Validate bounded discovery against predeclared authority and the selected origin.
parseDiscoveryWithDepth :: Int -> AllowedHostPorts -> Text -> ByteString -> Either MetadataError CargoDiscovery
parseDiscoveryWithDepth :: Int
-> AllowedHostPorts
-> Text
-> ByteString
-> Either MetadataError CargoDiscovery
parseDiscoveryWithDepth Int
depth AllowedHostPorts
hosts Text
origin ByteString
bytes = do
    Bool -> Either MetadataError () -> Either MetadataError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
65536) (MetadataError -> Either MetadataError ()
forall a b. a -> Either a b
Left (LimitError -> MetadataError
MetadataBoundExceeded (BodyLimit -> LimitError
BodyTooLarge (Int -> BodyLimit
MetadataBodyLimit Int
65536))))
    value <- (CargoRecordError -> MetadataError)
-> Either CargoRecordError Value -> Either MetadataError Value
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first CargoRecordError -> MetadataError
recordError (Int -> ByteString -> Either CargoRecordError Value
decodeCargoRecord Int
depth ByteString
bytes)
    first (const MetadataUndecodable) (finishDiscovery value)
  where
    recordError :: CargoRecordError -> MetadataError
recordError = \case
        CargoRecordBound LimitError
fault -> LimitError -> MetadataError
MetadataBoundExceeded LimitError
fault
        CargoRecordMalformed Text
_ -> MetadataError
MetadataUndecodable
    finishDiscovery :: Value -> Either Text CargoDiscovery
finishDiscovery Value
value = do
        fields <- case Value
value of
            Object Object
members -> Object -> Either Text Object
forall a b. b -> Either a b
Right Object
members
            Value
_ -> Text -> Either Text Object
forall a b. a -> Either a b
Left Text
"discovery is not an object"
        dl <- requiredText "dl" fields
        unless (validTemplate dl) (Left "invalid Cargo download template")
        let authority = AllowedHostPorts -> Text -> ArtifactOrigin
artifactOrigin AllowedHostPorts
hosts Text
origin
        _ <- checkedAddress authority (templateExample dl)
        api <- case KeyMap.lookup "api" fields of
            Maybe Value
Nothing -> Maybe Text -> Either Text (Maybe Text)
forall a b. b -> Either a b
Right Maybe Text
forall a. Maybe a
Nothing
            Just Value
Null -> Maybe Text -> Either Text (Maybe Text)
forall a b. b -> Either a b
Right Maybe Text
forall a. Maybe a
Nothing
            Just (String Text
url) -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
url Maybe Text -> Either Text () -> Either Text (Maybe Text)
forall a b. a -> Either Text b -> Either Text a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> Either Text ()
checkedAbsolute Text
url
            Maybe Value
_ -> Text -> Either Text (Maybe Text)
forall a b. a -> Either a b
Left Text
"invalid Cargo API URL"
        case KeyMap.lookup "auth-required" fields of
            Maybe Value
Nothing -> () -> Either Text ()
forall a b. b -> Either a b
Right ()
            Just (Bool Bool
_) -> () -> Either Text ()
forall a b. b -> Either a b
Right ()
            Maybe Value
_ -> Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"invalid Cargo authentication declaration"
        pure (CargoDiscovery dl api origin authority)

-- | Local discovery uses only the supplied local routes and authentication requirement.
renderCargoDiscovery :: Text -> Maybe Text -> Bool -> ByteString
renderCargoDiscovery :: Text -> Maybe Text -> Bool -> ByteString
renderCargoDiscovery Text
dl Maybe Text
api Bool
auth = ByteString -> ByteString
forall l s. LazyStrict l s => l -> s
toStrict (Value -> ByteString
forall a. ToJSON a => a -> ByteString
encode ([Pair] -> Value
object ([Key
"dl" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
dl, Key
"auth-required" Key -> Bool -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Bool
auth] [Pair] -> [Pair] -> [Pair]
forall a. Semigroup a => a -> a -> a
<> [Pair] -> (Text -> [Pair]) -> Maybe Text -> [Pair]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\Text
url -> [Key
"api" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
url]) Maybe Text
api)))

-- | Exact native claims for one archive, independent of its download URL's final component.
data CargoArtifactCoordinate = CargoArtifactCoordinate !Text !Version !Filename !Hash
    deriving stock (CargoArtifactCoordinate -> CargoArtifactCoordinate -> Bool
(CargoArtifactCoordinate -> CargoArtifactCoordinate -> Bool)
-> (CargoArtifactCoordinate -> CargoArtifactCoordinate -> Bool)
-> Eq CargoArtifactCoordinate
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CargoArtifactCoordinate -> CargoArtifactCoordinate -> Bool
== :: CargoArtifactCoordinate -> CargoArtifactCoordinate -> Bool
$c/= :: CargoArtifactCoordinate -> CargoArtifactCoordinate -> Bool
/= :: CargoArtifactCoordinate -> CargoArtifactCoordinate -> Bool
Eq, Int -> CargoArtifactCoordinate -> ShowS
[CargoArtifactCoordinate] -> ShowS
CargoArtifactCoordinate -> String
(Int -> CargoArtifactCoordinate -> ShowS)
-> (CargoArtifactCoordinate -> String)
-> ([CargoArtifactCoordinate] -> ShowS)
-> Show CargoArtifactCoordinate
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CargoArtifactCoordinate -> ShowS
showsPrec :: Int -> CargoArtifactCoordinate -> ShowS
$cshow :: CargoArtifactCoordinate -> String
show :: CargoArtifactCoordinate -> String
$cshowList :: [CargoArtifactCoordinate] -> ShowS
showList :: [CargoArtifactCoordinate] -> ShowS
Show)

-- | Exact published name, including native case and punctuation.
coordinateName :: CargoArtifactCoordinate -> Text
coordinateName :: CargoArtifactCoordinate -> Text
coordinateName (CargoArtifactCoordinate Text
name Version
_ Filename
_ Hash
_) = Text
name

-- | Native raw version, including build metadata.
coordinateVersion :: CargoArtifactCoordinate -> Version
coordinateVersion :: CargoArtifactCoordinate -> Version
coordinateVersion (CargoArtifactCoordinate Text
_ Version
version Filename
_ Hash
_) = Version
version

-- | The checked logical archive name does not depend on the URL's terminal component.
coordinateFilename :: CargoArtifactCoordinate -> Filename
coordinateFilename :: CargoArtifactCoordinate -> Filename
coordinateFilename (CargoArtifactCoordinate Text
_ Version
_ Filename
filename Hash
_) = Filename
filename

-- | The validated SHA-256 from the same native row as this coordinate.
coordinateChecksum :: CargoArtifactCoordinate -> Hash
coordinateChecksum :: CargoArtifactCoordinate -> Hash
coordinateChecksum (CargoArtifactCoordinate Text
_ Version
_ Filename
_ Hash
checksum) = Hash
checksum

-- | Bind published spelling, raw version and SHA-256 before any template interpolation.
cargoArtifactCoordinate :: Text -> Version -> Hash -> Either Text CargoArtifactCoordinate
cargoArtifactCoordinate :: Text -> Version -> Hash -> Either Text CargoArtifactCoordinate
cargoArtifactCoordinate Text
name Version
version Hash
checksum = do
    _ <- (ParseError -> Text)
-> Either ParseError PackageName -> Either Text PackageName
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Text -> ParseError -> Text
forall a b. a -> b -> a
const Text
"invalid Cargo package name") (Text -> Either ParseError PackageName
projectName Text
name)
    _ <- maybeToRight "invalid Cargo version" (parseCargo (renderVersion version))
    unless (hashAlg checksum == SHA256) (Left "Cargo requires SHA-256")
    _ <- mkHash SHA256 (hashValue checksum)
    filename <- maybeToRight "invalid Cargo archive filename" (mkFilename (cargoArchiveFilename name version))
    pure (CargoArtifactCoordinate name (mkVersion Cargo (renderVersion version)) filename checksum)

-- | Expand only native markers and recheck the address against the captured origin.
bindCargoDownload :: CargoDiscovery -> CargoArtifactCoordinate -> Either Text Text
bindCargoDownload :: CargoDiscovery -> CargoArtifactCoordinate -> Either Text Text
bindCargoDownload CargoDiscovery
discovery CargoArtifactCoordinate
coordinate = ArtifactOrigin -> Text -> Either Text Text
checkedAddress (CargoDiscovery -> ArtifactOrigin
discoveryOrigin CargoDiscovery
discovery) Text
expanded
  where
    template :: Text
template = CargoDiscovery -> Text
discoveryDl CargoDiscovery
discovery
    name :: Text
name = CargoArtifactCoordinate -> Text
coordinateName CargoArtifactCoordinate
coordinate
    prefix :: Text
prefix = Text -> Text
cargoPrefix Text
name
    replacements :: [(Text, Text)]
replacements =
        [ (Text
"{crate}", Text -> Text
encodeComponent Text
name)
        , (Text
"{version}", Text -> Text
encodeComponent (Version -> Text
renderVersion (CargoArtifactCoordinate -> Version
coordinateVersion CargoArtifactCoordinate
coordinate)))
        , (Text
"{prefix}", Text
prefix)
        , (Text
"{lowerprefix}", Text -> Text
T.toLower Text
prefix)
        , (Text
"{sha256-checksum}", Hash -> Text
hashValue (CargoArtifactCoordinate -> Hash
coordinateChecksum CargoArtifactCoordinate
coordinate))
        ]
    expanded :: Text
expanded
        | Text
"{" Text -> Text -> Bool
`T.isInfixOf` Text
template = (Text -> (Text, Text) -> Text) -> Text -> [(Text, Text)] -> Text
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Text
text (Text
marker, Text
value) -> HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
marker Text
value Text
text) Text
template [(Text, Text)]
replacements
        | Bool
otherwise = (Char -> Bool) -> Text -> Text
T.dropWhileEnd (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/') Text
template Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
encodeComponent Text
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
encodeComponent (Version -> Text
renderVersion (CargoArtifactCoordinate -> Version
coordinateVersion CargoArtifactCoordinate
coordinate)) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/download"

requiredText :: Key.Key -> KeyMap.KeyMap Value -> Either Text Text
requiredText :: Key -> Object -> Either Text Text
requiredText Key
key Object
fields = case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
key Object
fields of
    Just (String Text
text) -> Text -> Either Text Text
forall a b. b -> Either a b
Right Text
text
    Maybe Value
_ -> Text -> Either Text Text
forall a b. a -> Either a b
Left Text
"missing Cargo download template"

validTemplate :: Text -> Bool
validTemplate :: Text -> Bool
validTemplate Text
template =
    Bool -> Bool
not (Text -> Bool
T.null Text
template)
        Bool -> Bool -> Bool
&& Bool -> Bool
not ((Char -> Bool) -> Text -> Bool
T.any Char -> Bool
isSpace Text
template)
        Bool -> Bool -> Bool
&& Bool -> Bool
not ((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
'}']) Text
withoutMarkers)
        Bool -> Bool -> Bool
&& (Text
"{" Text -> Text -> Bool
`T.isInfixOf` Text
template Bool -> Bool -> Bool
|| Bool -> Bool
not ((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
'#']) Text
template))
  where
    withoutMarkers :: Text
withoutMarkers = (Text -> Text -> Text) -> Text -> [Text] -> Text
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Text
text Text
marker -> HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
marker Text
"" Text
text) Text
template [Text]
markers

markers :: [Text]
markers :: [Text]
markers = [Text
"{crate}", Text
"{version}", Text
"{prefix}", Text
"{lowerprefix}", Text
"{sha256-checksum}"]

templateExample :: Text -> Text
templateExample :: Text -> Text
templateExample Text
template = (Text -> Text -> Text) -> Text -> [Text] -> Text
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Text
text Text
marker -> HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
marker Text
"cargo" Text
text) Text
template [Text]
markers

checkedAbsolute :: Text -> Either Text ()
checkedAbsolute :: Text -> Either Text ()
checkedAbsolute Text
url = do
    Bool -> Either Text () -> Either Text ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((LowerPrefix -> Text -> Bool
isPrefixOfLowered LowerPrefix
httpsPrefix Text
url Bool -> Bool -> Bool
|| LowerPrefix -> Text -> Bool
isPrefixOfLowered LowerPrefix
httpPrefix Text
url) Bool -> Bool -> Bool
&& Bool -> Bool
not ((Char -> Bool) -> Text -> Bool
T.any Char -> Bool
isSpace Text
url)) (Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"invalid absolute Cargo URL")
    let authority :: Text
authority = (Char -> Bool) -> Text -> Text
T.takeWhile (Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`notElem` [Char
'/', Char
'?', Char
'#']) (Int -> Text -> Text
T.drop Int
3 ((Text, Text) -> Text
forall a b. (a, b) -> b
snd (HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
"://" Text
url)))
    Bool -> Either Text () -> Either Text ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Bool -> Bool
not (Text -> Bool
T.null Text
authority) Bool -> Bool -> Bool
&& Bool -> Bool
not ((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
'{', Char
'}']) Text
authority) Bool -> Bool -> Bool
&& Bool -> Bool
not (Text
"#" Text -> Text -> Bool
`T.isInfixOf` Text
url)) (Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"invalid Cargo authority")

checkedAddress :: ArtifactOrigin -> Text -> Either Text Text
checkedAddress :: ArtifactOrigin -> Text -> Either Text Text
checkedAddress ArtifactOrigin
origin Text
url = Text -> Either Text ()
checkedAbsolute Text
url Either Text () -> Either Text Text -> Either Text Text
forall a b. Either Text a -> Either Text b -> Either Text b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (LocationRefusal -> Text)
-> Either LocationRefusal Text -> Either Text Text
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Text -> LocationRefusal -> Text
forall a b. a -> b -> a
const Text
"unapproved Cargo URL authority or scheme") (ArtifactOrigin -> Text -> Either LocationRefusal Text
checkArtifactAddress ArtifactOrigin
origin Text
url)