{-# LANGUAGE TupleSections #-}
module Ecluse.Core.Registry.Cargo.Manifest (
CargoManifest,
CargoPublishMetadata,
decodeManifest,
manifestTextPaths,
publicationMetadata,
checkPublicationIndex,
checkUploadAgreement,
checkDependencyRegistries,
acceptedRegistries,
namesOtherRegistry,
parseRustRelease,
publishMetadataValue,
cratesIoIndex,
unstableDependencyFields,
usesUnstableDependencyField,
) where
import Control.Monad (foldM)
import Data.Aeson (Value (..), object, toJSON, (.=))
import Data.Aeson.Key qualified as Key
import Data.Aeson.KeyMap qualified as KeyMap
import Data.ByteString qualified as BS
import Data.Char (isDigit)
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set
import Data.Text qualified as T
import Data.Text.Encoding qualified as TE
import Toml qualified
import Toml.Syntax qualified as Syntax
import Ecluse.Core.Registry (ParseError (..), parseRefusal)
import Ecluse.Core.Registry.Archive.Path (resolveArchivePath)
import Ecluse.Core.Registry.Cargo.Source (CargoSource, parseSource, sourceComparisonKey)
import Ecluse.Core.Registry.Cargo.Target (CargoTarget, parseTarget)
import Ecluse.Core.Security.Limits (Limits (..))
import Ecluse.Core.Text (joinAlternatives)
import Ecluse.Core.Version.Cargo (CargoKey, parseCargo)
data CargoManifest = CargoManifest
{ CargoManifest -> Text
cmName :: Text
, CargoManifest -> Text
cmVersion :: Text
, CargoManifest -> Table
cmPackage :: Table
, CargoManifest -> Table
cmRoot :: Table
}
deriving stock (CargoManifest -> CargoManifest -> Bool
(CargoManifest -> CargoManifest -> Bool)
-> (CargoManifest -> CargoManifest -> Bool) -> Eq CargoManifest
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CargoManifest -> CargoManifest -> Bool
== :: CargoManifest -> CargoManifest -> Bool
$c/= :: CargoManifest -> CargoManifest -> Bool
/= :: CargoManifest -> CargoManifest -> Bool
Eq, Int -> CargoManifest -> ShowS
[CargoManifest] -> ShowS
CargoManifest -> String
(Int -> CargoManifest -> ShowS)
-> (CargoManifest -> String)
-> ([CargoManifest] -> ShowS)
-> Show CargoManifest
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CargoManifest -> ShowS
showsPrec :: Int -> CargoManifest -> ShowS
$cshow :: CargoManifest -> String
show :: CargoManifest -> String
$cshowList :: [CargoManifest] -> ShowS
showList :: [CargoManifest] -> ShowS
Show)
newtype CargoPublishMetadata = CargoPublishMetadata Value
deriving stock (CargoPublishMetadata -> CargoPublishMetadata -> Bool
(CargoPublishMetadata -> CargoPublishMetadata -> Bool)
-> (CargoPublishMetadata -> CargoPublishMetadata -> Bool)
-> Eq CargoPublishMetadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CargoPublishMetadata -> CargoPublishMetadata -> Bool
== :: CargoPublishMetadata -> CargoPublishMetadata -> Bool
$c/= :: CargoPublishMetadata -> CargoPublishMetadata -> Bool
/= :: CargoPublishMetadata -> CargoPublishMetadata -> Bool
Eq, Int -> CargoPublishMetadata -> ShowS
[CargoPublishMetadata] -> ShowS
CargoPublishMetadata -> String
(Int -> CargoPublishMetadata -> ShowS)
-> (CargoPublishMetadata -> String)
-> ([CargoPublishMetadata] -> ShowS)
-> Show CargoPublishMetadata
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CargoPublishMetadata -> ShowS
showsPrec :: Int -> CargoPublishMetadata -> ShowS
$cshow :: CargoPublishMetadata -> String
show :: CargoPublishMetadata -> String
$cshowList :: [CargoPublishMetadata] -> ShowS
showList :: [CargoPublishMetadata] -> ShowS
Show)
type Table = Map Text Toml.Value
decodeManifest :: Limits -> Text -> Text -> ByteString -> Either ParseError CargoManifest
decodeManifest :: Limits
-> Text -> Text -> ByteString -> Either ParseError CargoManifest
decodeManifest Limits
limits Text
name Text
version ByteString
bytes = do
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Limits -> Int
maxMetadataBytes Limits
limits Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 Bool -> Bool -> Bool
|| ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
1048576 (Limits -> Int
maxMetadataBytes Limits
limits)) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo manifest exceeds its byte bound")
text <- (UnicodeException -> ParseError)
-> Either UnicodeException Text -> Either ParseError 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 (ParseError -> UnicodeException -> ParseError
forall a b. a -> b -> a
const (Text -> ParseError
ParseError Text
"Cargo manifest is not UTF-8")) (ByteString -> Either UnicodeException Text
TE.decodeUtf8' ByteString
bytes)
boundSyntax (maxNestingDepth limits) text
parsed <- first (const (ParseError "invalid Cargo TOML manifest")) (Toml.parse text)
checkDepth (maxNestingDepth limits) 0 (Toml.Table' () (Toml.forgetTableAnns parsed))
let root = Table' () -> Table
tableValues (Table' Position -> Table' ()
forall a. Table' a -> Table' ()
Toml.forgetTableAnns Table' Position
parsed)
for_ (Map.lookup "cargo-features" root) $ \Value' ()
value -> do
features <- Value' () -> Either ParseError [Text]
strings Value' ()
value
unless (null features) (parseRefusal "Cargo unstable manifest features are unsupported")
packageValue <- aliased "package" "project" root >>= maybe (parseRefusal "Cargo manifest lacks package metadata") Right
package <- table packageValue
actualName <- required "name" package >>= string
actualVersion <- required "version" package >>= string
unless (actualName == name && actualVersion == version) (parseRefusal "Cargo manifest disagrees with the admitted native identity")
when (Map.member "workspace" root || Map.member "workspace" package) (parseRefusal "Cargo packaged manifest retains workspace inheritance")
for_ (Map.lookup "edition" package) $ \Value' ()
value -> do
edition <- Value' () -> Either ParseError Text
string Value' ()
value
unless (edition `elem` ["2015", "2018", "2021", "2024"]) (parseRefusal "unsupported Cargo edition")
pure (CargoManifest actualName actualVersion package root)
manifestTextPaths :: CargoManifest -> Set Text -> Either ParseError [Text]
manifestTextPaths :: CargoManifest -> Set Text -> Either ParseError [Text]
manifestTextPaths CargoManifest
manifest Set Text
inventory = do
readme <- CargoManifest -> Set Text -> Either ParseError (Maybe Text)
readmePath CargoManifest
manifest Set Text
inventory
licence <- optionalStringAlias "license-file" "license_file" (cmPackage manifest)
paths <- traverse checkedPath (maybeToList readme <> maybeToList licence)
for_ paths $ \Text
path -> Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member Text
path Set Text
inventory) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo manifest declares an absent text file")
pure (ordNub paths)
publicationMetadata :: CargoManifest -> Map Text ByteString -> Either ParseError CargoPublishMetadata
publicationMetadata :: CargoManifest
-> Map Text ByteString -> Either ParseError CargoPublishMetadata
publicationMetadata CargoManifest
manifest Map Text ByteString
content = do
let package :: Table
package = CargoManifest -> Table
cmPackage CargoManifest
manifest
(dependencies, features, rustVersion, links) <- CargoManifest
-> Either
ParseError ([Value], Map Text [Text], Maybe Text, Maybe Text)
resolverMetadata CargoManifest
manifest
authors <- optionalStrings "authors" package
keywords <- optionalStrings "keywords" package
categories <- optionalStrings "categories" package
badges <- optionalTable "badges" (cmRoot manifest) >>= traverse (table >=> traverse string)
readmeFile <- readmePath manifest (Map.keysSet content)
readme <- traverse (textContent content) readmeFile
licenceFile <- optionalStringAlias "license-file" "license_file" package
for_ licenceFile $ \Text
path -> Either ParseError Text -> Either ParseError ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Map Text ByteString -> Text -> Either ParseError Text
textContent Map Text ByteString
content Text
path)
fields <-
traverse
(\(Text
source, Text
target) -> (Text
target,) (Maybe Text -> (Text, Maybe Text))
-> Either ParseError (Maybe Text)
-> Either ParseError (Text, Maybe Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Table -> Either ParseError (Maybe Text)
optionalString Text
source Table
package)
[("description", "description"), ("documentation", "documentation"), ("homepage", "homepage"), ("license", "license"), ("repository", "repository")]
pure . CargoPublishMetadata . object $
[ "name" .= cmName manifest
, "vers" .= cmVersion manifest
, "deps" .= dependencies
, "features" .= features
, "authors" .= authors
, "keywords" .= keywords
, "categories" .= categories
, "badges" .= badges
, "readme" .= readme
, "readme_file" .= readmeFile
, "license_file" .= licenceFile
, "rust_version" .= rustVersion
, "links" .= links
]
<> [Key.fromText key .= value | (key, value) <- fields]
resolverMetadata :: CargoManifest -> Either ParseError ([Value], Map Text [Text], Maybe Text, Maybe Text)
resolverMetadata :: CargoManifest
-> Either
ParseError ([Value], Map Text [Text], Maybe Text, Maybe Text)
resolverMetadata CargoManifest
manifest =
(,,,)
([Value]
-> Map Text [Text]
-> Maybe Text
-> Maybe Text
-> ([Value], Map Text [Text], Maybe Text, Maybe Text))
-> Either ParseError [Value]
-> Either
ParseError
(Map Text [Text]
-> Maybe Text
-> Maybe Text
-> ([Value], Map Text [Text], Maybe Text, Maybe Text))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Table -> Either ParseError [Value]
manifestDependencies (CargoManifest -> Table
cmRoot CargoManifest
manifest)
Either
ParseError
(Map Text [Text]
-> Maybe Text
-> Maybe Text
-> ([Value], Map Text [Text], Maybe Text, Maybe Text))
-> Either ParseError (Map Text [Text])
-> Either
ParseError
(Maybe Text
-> Maybe Text
-> ([Value], Map Text [Text], Maybe Text, Maybe Text))
forall a b.
Either ParseError (a -> b)
-> Either ParseError a -> Either ParseError b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> Table -> Either ParseError Table
optionalTable Text
"features" (CargoManifest -> Table
cmRoot CargoManifest
manifest) Either ParseError Table
-> (Table -> Either ParseError (Map Text [Text]))
-> Either ParseError (Map Text [Text])
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Value' () -> Either ParseError [Text])
-> Table -> Either ParseError (Map Text [Text])
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) -> Map Text a -> f (Map Text b)
traverse Value' () -> Either ParseError [Text]
strings)
Either
ParseError
(Maybe Text
-> Maybe Text
-> ([Value], Map Text [Text], Maybe Text, Maybe Text))
-> Either ParseError (Maybe Text)
-> Either
ParseError
(Maybe Text -> ([Value], Map Text [Text], Maybe Text, Maybe Text))
forall a b.
Either ParseError (a -> b)
-> Either ParseError a -> Either ParseError b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Text -> Table -> Either ParseError (Maybe Text)
optionalStringAlias Text
"rust-version" Text
"rust_version" (CargoManifest -> Table
cmPackage CargoManifest
manifest)
Either
ParseError
(Maybe Text -> ([Value], Map Text [Text], Maybe Text, Maybe Text))
-> Either ParseError (Maybe Text)
-> Either
ParseError ([Value], Map Text [Text], Maybe Text, Maybe Text)
forall a b.
Either ParseError (a -> b)
-> Either ParseError a -> Either ParseError b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Table -> Either ParseError (Maybe Text)
optionalString Text
"links" (CargoManifest -> Table
cmPackage CargoManifest
manifest)
checkUploadAgreement :: Limits -> Text -> CargoManifest -> Value -> Either ParseError ()
checkUploadAgreement :: Limits -> Text -> CargoManifest -> Value -> Either ParseError ()
checkUploadAgreement Limits
limits Text
registry CargoManifest
manifest (Object Object
metadata) = do
(dependencies, features, rustVersion, links) <- CargoManifest
-> Either
ParseError ([Value], Map Text [Text], Maybe Text, Maybe Text)
resolverMetadata CargoManifest
manifest
void (manifestDependenciesWith refuseUnstableKeys (cmRoot manifest))
currentRegistry <- parseSource registry
expected <- traverse (uploadDependency currentRegistry) dependencies >>= foldM (insertDependency indexRepeat limits) Map.empty
uploaded <- requiredJson "deps" metadata >>= array >>= traverse (uploadDependency currentRegistry)
actual <- foldM (insertDependency indexRepeat limits) Map.empty uploaded
unless (Map.keysSet expected == Map.keysSet actual) (parseRefusal "Cargo upload and manifest dependency inventories disagree")
for_ (Map.toList expected) $ \(DependencyKey
key, Value
value) -> do
agreed <- Either ParseError Value
-> (Value -> Either ParseError Value)
-> Maybe Value
-> Either ParseError Value
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text -> Either ParseError Value
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo upload omits a manifest dependency") Value -> Either ParseError Value
forall a b. b -> Either a b
Right (DependencyKey -> Map DependencyKey Value -> Maybe Value
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup DependencyKey
key Map DependencyKey Value
actual)
checkDependency value agreed
uploadedFeatures <- requiredJson "features" metadata >>= jsonObject >>= traverse (array >=> traverse jsonString)
expectedFeatures <- effectiveFeatures dependencies features
actualFeatures <- effectiveFeatures uploaded (Map.fromList [(Key.toText key, values) | (key, values) <- KeyMap.toList uploadedFeatures])
unless (expectedFeatures == actualFeatures) (parseRefusal "Cargo upload and manifest feature graphs disagree")
actualLinks <- nullableString "links" metadata
unless (links == actualLinks) (parseRefusal "Cargo upload and manifest links declarations disagree")
expectedRust <- traverse parseRustRelease rustVersion
actualRust <- nullableString "rust_version" metadata >>= traverse parseRustRelease
unless (expectedRust == actualRust) (parseRefusal "Cargo upload and manifest Rust versions disagree")
checkUploadAgreement Limits
_ Text
_ CargoManifest
_ Value
_ = Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo upload metadata is not an object"
checkDependencyRegistries :: [Text] -> Value -> Either ParseError ()
checkDependencyRegistries :: [Text] -> Value -> Either ParseError ()
checkDependencyRegistries [Text]
own (Object Object
metadata) = do
accepted <- [Text] -> Either ParseError (Set Text)
acceptedRegistries [Text]
own
dependencies <- requiredJson "deps" metadata >>= array
for_ dependencies $ \case
Object Object
fields -> do
name <- Key -> Object -> Either ParseError Value
requiredJson Key
"name" Object
fields Either ParseError Value
-> (Value -> Either ParseError Text) -> Either ParseError Text
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> Either ParseError Text
jsonString
other <- namesOtherRegistry accepted fields
when other $
parseRefusal ("Cargo dependency '" <> name <> "' comes from a registry other than crates.io or this proxy: publish it through this proxy first, then depend on this proxy's copy")
Value
_ -> Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo upload dependency is not an object"
checkDependencyRegistries [Text]
_ Value
_ = Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo upload metadata is not an object"
acceptedRegistries :: [Text] -> Either ParseError (Set Text)
acceptedRegistries :: [Text] -> Either ParseError (Set Text)
acceptedRegistries [Text]
own = [Text] -> Set Text
forall a. Ord a => [a] -> Set a
Set.fromList ([Text] -> Set Text)
-> ([CargoSource] -> [Text]) -> [CargoSource] -> Set Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CargoSource -> Text) -> [CargoSource] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map CargoSource -> Text
sourceComparisonKey ([CargoSource] -> Set Text)
-> Either ParseError [CargoSource] -> Either ParseError (Set Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> Either ParseError CargoSource)
-> [Text] -> Either ParseError [CargoSource]
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 Text -> Either ParseError CargoSource
parseSource (Text
cratesIoIndex Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text]
own)
namesOtherRegistry :: Set Text -> KeyMap.KeyMap Value -> Either ParseError Bool
namesOtherRegistry :: Set Text -> Object -> Either ParseError Bool
namesOtherRegistry Set Text
accepted Object
fields = do
kind <- Key -> Object -> Either ParseError (Maybe Text)
nullableString Key
"kind" Object
fields
declared <- if kind == Just "dev" then Right Nothing else nullableString "registry" fields >>= traverse parseSource
pure (any ((`Set.notMember` accepted) . sourceComparisonKey) declared)
{-# INLINE namesOtherRegistry #-}
uploadDependency :: CargoSource -> Value -> Either ParseError Value
uploadDependency :: CargoSource -> Value -> Either ParseError Value
uploadDependency CargoSource
registry (Object Object
fields) = do
name <- Key -> Object -> Either ParseError Value
requiredJson Key
"name" Object
fields Either ParseError Value
-> (Value -> Either ParseError Text) -> Either ParseError Text
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> Either ParseError Text
jsonString
alias <- nullableString "explicit_name_in_toml" fields
kind <- fromMaybe "normal" <$> nullableString "kind" fields
source <- maybe (Right registry) parseSource =<< nullableString "registry" fields
pure . Object
$ KeyMap.insert "explicit_name_in_toml" (toJSON (alias >>= \Text
value -> Text
value Text -> Maybe () -> Maybe Text
forall a b. a -> Maybe b -> Maybe a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Text
value Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
name)))
. KeyMap.insert "kind" (String kind)
. KeyMap.insert "registry" (String (sourceComparisonKey source))
$ fields
uploadDependency CargoSource
_ Value
_ = Text -> Either ParseError Value
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo upload dependency is not an object"
effectiveFeatures :: [Value] -> Map Text [Text] -> Either ParseError (Map Text (Set Text))
effectiveFeatures :: [Value]
-> Map Text [Text] -> Either ParseError (Map Text (Set Text))
effectiveFeatures [Value]
dependencies Map Text [Text]
features = do
optionalDependencyAliases <- ([Maybe Text] -> [Text])
-> Either ParseError [Maybe Text] -> Either ParseError [Text]
forall a b. (a -> b) -> Either ParseError a -> Either ParseError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Maybe Text] -> [Text]
forall a. [Maybe a] -> [a]
catMaybes (Either ParseError [Maybe Text] -> Either ParseError [Text])
-> ((Value -> Either ParseError (Maybe Text))
-> Either ParseError [Maybe Text])
-> (Value -> Either ParseError (Maybe Text))
-> Either ParseError [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Value]
-> (Value -> Either ParseError (Maybe Text))
-> Either ParseError [Maybe Text]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Value]
dependencies ((Value -> Either ParseError (Maybe Text))
-> Either ParseError [Text])
-> (Value -> Either ParseError (Maybe Text))
-> Either ParseError [Text]
forall a b. (a -> b) -> a -> b
$ \case
Object Object
fields -> do
enabled <- Key -> Object -> Either ParseError Value
requiredJson Key
"optional" Object
fields Either ParseError Value
-> (Value -> Either ParseError Bool) -> Either ParseError Bool
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> Either ParseError Bool
jsonBool
name <- requiredJson "name" fields >>= jsonString
alias <- fromMaybe name <$> nullableString "explicit_name_in_toml" fields
pure (alias <$ guard enabled)
Value
_ -> Text -> Either ParseError (Maybe Text)
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo publication dependency is not an object"
let declared = [Text] -> Set Text
forall a. Ord a => [a] -> Set a
Set.fromList ([Text] -> Set Text) -> Map Text [Text] -> Map Text (Set Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map Text [Text]
features
suppressed = [Text] -> Set Text
forall a. Ord a => [a] -> Set a
Set.fromList [Text
alias | [Text]
values <- Map Text [Text] -> [[Text]]
forall k a. Map k a -> [a]
Map.elems Map Text [Text]
features, Text
value <- [Text]
values, Just Text
alias <- [Text -> Text -> Maybe Text
T.stripPrefix Text
"dep:" Text
value]]
implicit = [(Text, Set Text)] -> Map Text (Set Text)
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Text
alias, Text -> Set Text
forall a. a -> Set a
Set.singleton (Text
"dep:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
alias)) | Text
alias <- [Text]
optionalDependencyAliases, Text -> Map Text [Text] -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.notMember Text
alias Map Text [Text]
features, Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.notMember Text
alias Set Text
suppressed]
pure (Map.union declared implicit)
parseRustRelease :: Text -> Either ParseError CargoKey
parseRustRelease :: Text -> Either ParseError CargoKey
parseRustRelease Text
raw = do
let components :: [Text]
components = HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"." Text
raw
complete :: Text
complete = Text -> [Text] -> Text
T.intercalate Text
"." ([Text]
components [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> Int -> Text -> [Text]
forall a. Int -> a -> [a]
replicate (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
3 Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
components)) Text
"0")
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
components Int -> [Int] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Int
1, Int
2, Int
3] 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
raw)) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo Rust version is invalid")
Either ParseError CargoKey
-> (CargoKey -> Either ParseError CargoKey)
-> Maybe CargoKey
-> Either ParseError CargoKey
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text -> Either ParseError CargoKey
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo Rust version is invalid") CargoKey -> Either ParseError CargoKey
forall a b. b -> Either a b
Right (Text -> Maybe CargoKey
parseCargo Text
complete)
checkPublicationIndex :: Limits -> CargoPublishMetadata -> Value -> Either ParseError CargoPublishMetadata
checkPublicationIndex :: Limits
-> CargoPublishMetadata
-> Value
-> Either ParseError CargoPublishMetadata
checkPublicationIndex Limits
limits (CargoPublishMetadata (Object Object
metadata)) (Object Object
index) = do
[Key] -> (Key -> Either ParseError ()) -> Either ParseError ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Key
"name", Key
"vers"] ((Key -> Either ParseError ()) -> Either ParseError ())
-> (Key -> Either ParseError ()) -> Either ParseError ()
forall a b. (a -> b) -> a -> b
$ \Key
key ->
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
key Object
metadata Maybe Value -> Maybe Value -> Bool
forall a. Eq a => a -> a -> Bool
== Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
key Object
index) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo publication and source index identities disagree")
nativeDeps <- Key -> Object -> Either ParseError Value
requiredJson Key
"deps" Object
index Either ParseError Value
-> (Value -> Either ParseError [Value])
-> Either ParseError [Value]
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> Either ParseError [Value]
array
expectedDeps <- requiredJson "deps" metadata >>= array
projected <- traverse indexDependency nativeDeps
unless (length expectedDeps == length projected) (parseRefusal "Cargo manifest and index dependency counts disagree")
registry <- parseSource cratesIoIndex
byIdentity <- traverse (uploadDependency registry) projected >>= foldM (insertDependency indexRepeat limits) Map.empty
declared <- traverse (uploadDependency registry) expectedDeps >>= foldM (insertDependency "Cargo manifest repeats a dependency identity" limits) Map.empty
unless (Map.keysSet declared == Map.keysSet byIdentity) (parseRefusal "Cargo manifest and index dependency identities disagree")
for_ (Map.toList declared) $ \(DependencyKey
key, Value
expected) -> do
actual <- Either ParseError Value
-> (Value -> Either ParseError Value)
-> Maybe Value
-> Either ParseError Value
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text -> Either ParseError Value
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo manifest and index dependency identities disagree") Value -> Either ParseError Value
forall a b. b -> Either a b
Right (DependencyKey -> Map DependencyKey Value -> Maybe Value
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup DependencyKey
key Map DependencyKey Value
byIdentity)
checkDependency expected actual
sourceFeatures <- indexFeatures index
authoredFeatures <- requiredJson "features" metadata >>= jsonObject
for_ (KeyMap.toList authoredFeatures) $ \(Key
key, Value
value) -> do
source <- Either ParseError Value
-> (Value -> Either ParseError Value)
-> Maybe Value
-> Either ParseError Value
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text -> Either ParseError Value
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo index omits a manifest feature") Value -> Either ParseError Value
forall a b. b -> Either a b
Right (Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
key Object
sourceFeatures)
checkFeatureValues value source
authoredLinks <- nullableString "links" metadata
indexedLinks <- nullableString "links" index
unless (authoredLinks == indexedLinks) (parseRefusal "Cargo manifest and index links declarations disagree")
for_ (KeyMap.lookup "rust_version" index) $ \Value
value ->
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"rust_version" Object
metadata Maybe Value -> Maybe Value -> Bool
forall a. Eq a => a -> a -> Bool
== Value -> Maybe Value
forall a. a -> Maybe a
Just Value
value) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo manifest and index Rust versions disagree")
pure (CargoPublishMetadata (Object (KeyMap.insert "deps" (toJSON projected) (KeyMap.insert "features" (Object sourceFeatures) metadata))))
checkPublicationIndex Limits
_ CargoPublishMetadata
_ Value
_ = Text -> Either ParseError CargoPublishMetadata
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo source index entry is not an object"
publishMetadataValue :: CargoPublishMetadata -> Value
publishMetadataValue :: CargoPublishMetadata -> Value
publishMetadataValue (CargoPublishMetadata Value
value) = Value
value
boundSyntax :: Int -> Text -> Either ParseError ()
boundSyntax :: Int -> Text -> Either ParseError ()
boundSyntax Int
cap Text
text = Int -> Int -> Located Text -> Either ParseError ()
go Int
0 Int
0 (Position -> Text -> Located Text
forall a. Position -> a -> Located a
Syntax.Located Position
Syntax.startPos Text
text)
where
go :: Int -> Int -> Located Text -> Either ParseError ()
go Int
depth Int
dotted Located Text
input = do
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
cap Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 Bool -> Bool -> Bool
|| Int
depth Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
cap Bool -> Bool -> Bool
|| Int
dotted Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
cap) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo TOML nesting exceeds its bound")
(located, rest) <- (Located String -> ParseError)
-> Either (Located String) (Located Token, Located Text)
-> Either ParseError (Located Token, Located 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 (ParseError -> Located String -> ParseError
forall a b. a -> b -> a
const (Text -> ParseError
ParseError Text
"invalid Cargo TOML token")) (Context
-> Located Text
-> Either (Located String) (Located Token, Located Text)
Syntax.scanToken Context
Syntax.ValueContext Located Text
input)
case Syntax.locThing located of
Token
Syntax.TokEOF -> () -> Either ParseError ()
forall a b. b -> Either a b
Right ()
Token
Syntax.TokSquareO -> Int -> Int -> Located Text -> Either ParseError ()
go (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
0 Located Text
rest
Token
Syntax.Tok2SquareO -> Int -> Int -> Located Text -> Either ParseError ()
go (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) Int
0 Located Text
rest
Token
Syntax.TokCurlyO -> Int -> Int -> Located Text -> Either ParseError ()
go (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
0 Located Text
rest
Token
Syntax.TokSquareC -> Int -> Int -> Located Text -> Either ParseError ()
go (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) Int
0 Located Text
rest
Token
Syntax.Tok2SquareC -> Int -> Int -> Located Text -> Either ParseError ()
go (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2)) Int
0 Located Text
rest
Token
Syntax.TokCurlyC -> Int -> Int -> Located Text -> Either ParseError ()
go (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) Int
0 Located Text
rest
Token
Syntax.TokPeriod -> Int -> Int -> Located Text -> Either ParseError ()
go Int
depth (Int
dotted Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Located Text
rest
Token
Syntax.TokEquals -> Int -> Int -> Located Text -> Either ParseError ()
go Int
depth Int
0 Located Text
rest
Token
Syntax.TokComma -> Int -> Int -> Located Text -> Either ParseError ()
go Int
depth Int
0 Located Text
rest
Token
Syntax.TokNewline -> Int -> Int -> Located Text -> Either ParseError ()
go Int
depth Int
0 Located Text
rest
Token
_ -> Int -> Int -> Located Text -> Either ParseError ()
go Int
depth Int
dotted Located Text
rest
checkDepth :: Int -> Int -> Toml.Value -> Either ParseError ()
checkDepth :: Int -> Int -> Value' () -> Either ParseError ()
checkDepth Int
cap Int
depth Value' ()
value = do
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
cap Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 Bool -> Bool -> Bool
|| Int
depth Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
cap) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo TOML nesting exceeds its bound")
case Value' ()
value of
Toml.Table' ()
_ Table' ()
nested -> (Value' () -> Either ParseError ())
-> Table -> Either ParseError ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ (Int -> Int -> Value' () -> Either ParseError ()
checkDepth Int
cap (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) (Table' () -> Table
tableValues Table' ()
nested)
Toml.List' ()
_ [Value' ()]
values -> (Value' () -> Either ParseError ())
-> [Value' ()] -> Either ParseError ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ (Int -> Int -> Value' () -> Either ParseError ()
checkDepth Int
cap (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) [Value' ()]
values
Value' ()
_ -> () -> Either ParseError ()
forall a b. b -> Either a b
Right ()
tableValues :: Toml.Table -> Table
tableValues :: Table' () -> Table
tableValues (Toml.MkTable Map Text ((), Value' ())
values) = ((), Value' ()) -> Value' ()
forall a b. (a, b) -> b
snd (((), Value' ()) -> Value' ()) -> Map Text ((), Value' ()) -> Table
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map Text ((), Value' ())
values
table :: Toml.Value -> Either ParseError Table
table :: Value' () -> Either ParseError Table
table (Toml.Table' ()
_ Table' ()
value) = Table -> Either ParseError Table
forall a b. b -> Either a b
Right (Table' () -> Table
tableValues Table' ()
value)
table Value' ()
_ = Text -> Either ParseError Table
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo manifest field is not a table"
string :: Toml.Value -> Either ParseError Text
string :: Value' () -> Either ParseError Text
string (Toml.Text' ()
_ Text
value) = Text -> Either ParseError Text
forall a b. b -> Either a b
Right Text
value
string Value' ()
_ = Text -> Either ParseError Text
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo manifest field is not a string"
boolean :: Toml.Value -> Either ParseError Bool
boolean :: Value' () -> Either ParseError Bool
boolean (Toml.Bool' ()
_ Bool
value) = Bool -> Either ParseError Bool
forall a b. b -> Either a b
Right Bool
value
boolean Value' ()
_ = Text -> Either ParseError Bool
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo manifest field is not a boolean"
strings :: Toml.Value -> Either ParseError [Text]
strings :: Value' () -> Either ParseError [Text]
strings (Toml.List' ()
_ [Value' ()]
values) = (Value' () -> Either ParseError Text)
-> [Value' ()] -> Either ParseError [Text]
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 Value' () -> Either ParseError Text
string [Value' ()]
values
strings Value' ()
_ = Text -> Either ParseError [Text]
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo manifest field is not an array of strings"
required :: Text -> Table -> Either ParseError Toml.Value
required :: Text -> Table -> Either ParseError (Value' ())
required Text
key = Either ParseError (Value' ())
-> (Value' () -> Either ParseError (Value' ()))
-> Maybe (Value' ())
-> Either ParseError (Value' ())
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text -> Either ParseError (Value' ())
forall a. Text -> Either ParseError a
parseRefusal (Text
"Cargo manifest lacks " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
key)) Value' () -> Either ParseError (Value' ())
forall a b. b -> Either a b
Right (Maybe (Value' ()) -> Either ParseError (Value' ()))
-> (Table -> Maybe (Value' ()))
-> Table
-> Either ParseError (Value' ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Table -> Maybe (Value' ())
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
key
optionalTable :: Text -> Table -> Either ParseError Table
optionalTable :: Text -> Table -> Either ParseError Table
optionalTable Text
key Table
values = Either ParseError Table
-> (Value' () -> Either ParseError Table)
-> Maybe (Value' ())
-> Either ParseError Table
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Table -> Either ParseError Table
forall a b. b -> Either a b
Right Table
forall k a. Map k a
Map.empty) Value' () -> Either ParseError Table
table (Text -> Table -> Maybe (Value' ())
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
key Table
values)
optionalString :: Text -> Table -> Either ParseError (Maybe Text)
optionalString :: Text -> Table -> Either ParseError (Maybe Text)
optionalString Text
key = (Value' () -> Either ParseError Text)
-> Maybe (Value' ()) -> Either ParseError (Maybe Text)
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) -> Maybe a -> f (Maybe b)
traverse Value' () -> Either ParseError Text
string (Maybe (Value' ()) -> Either ParseError (Maybe Text))
-> (Table -> Maybe (Value' ()))
-> Table
-> Either ParseError (Maybe Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Table -> Maybe (Value' ())
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
key
optionalStrings :: Text -> Table -> Either ParseError [Text]
optionalStrings :: Text -> Table -> Either ParseError [Text]
optionalStrings Text
key Table
values = Either ParseError [Text]
-> (Value' () -> Either ParseError [Text])
-> Maybe (Value' ())
-> Either ParseError [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Text] -> Either ParseError [Text]
forall a b. b -> Either a b
Right []) Value' () -> Either ParseError [Text]
strings (Text -> Table -> Maybe (Value' ())
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
key Table
values)
aliased :: Text -> Text -> Table -> Either ParseError (Maybe Toml.Value)
aliased :: Text -> Text -> Table -> Either ParseError (Maybe (Value' ()))
aliased Text
primary Text
legacy Table
values = case (Text -> Table -> Maybe (Value' ())
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
primary Table
values, Text -> Table -> Maybe (Value' ())
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
legacy Table
values) of
(Just Value' ()
_, Just Value' ()
_) -> Text -> Either ParseError (Maybe (Value' ()))
forall a. Text -> Either ParseError a
parseRefusal Text
"ambiguous Cargo manifest spelling aliases"
(Maybe (Value' ())
preferred, Maybe (Value' ())
fallback) -> Maybe (Value' ()) -> Either ParseError (Maybe (Value' ()))
forall a. a -> Either ParseError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (Value' ())
preferred Maybe (Value' ()) -> Maybe (Value' ()) -> Maybe (Value' ())
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe (Value' ())
fallback)
optionalStringAlias :: Text -> Text -> Table -> Either ParseError (Maybe Text)
optionalStringAlias :: Text -> Text -> Table -> Either ParseError (Maybe Text)
optionalStringAlias Text
primary Text
legacy Table
values = Text -> Text -> Table -> Either ParseError (Maybe (Value' ()))
aliased Text
primary Text
legacy Table
values Either ParseError (Maybe (Value' ()))
-> (Maybe (Value' ()) -> Either ParseError (Maybe Text))
-> Either ParseError (Maybe Text)
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Value' () -> Either ParseError Text)
-> Maybe (Value' ()) -> Either ParseError (Maybe Text)
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) -> Maybe a -> f (Maybe b)
traverse Value' () -> Either ParseError Text
string
readmePath :: CargoManifest -> Set Text -> Either ParseError (Maybe Text)
readmePath :: CargoManifest -> Set Text -> Either ParseError (Maybe Text)
readmePath CargoManifest
manifest Set Text
inventory = case Text -> Table -> Maybe (Value' ())
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
"readme" (CargoManifest -> Table
cmPackage CargoManifest
manifest) of
Just (Toml.Bool' ()
_ Bool
False) -> Maybe Text -> Either ParseError (Maybe Text)
forall a. a -> Either ParseError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Text
forall a. Maybe a
Nothing
Just (Toml.Bool' ()
_ Bool
True) -> Maybe Text -> Either ParseError (Maybe Text)
forall a. a -> Either ParseError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"README.md")
Just Value' ()
value -> do
path <- Value' () -> Either ParseError Text
string Value' ()
value
Just path <$ checkedPath path
Maybe (Value' ())
Nothing -> Maybe Text -> Either ParseError (Maybe Text)
forall a. a -> Either ParseError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Text -> Bool) -> [Text] -> Maybe Text
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set Text
inventory) [Text
"README.md", Text
"README.txt", Text
"README"])
checkedPath :: Text -> Either ParseError Text
checkedPath :: Text -> Either ParseError Text
checkedPath = [Text] -> Text -> Either ParseError Text
resolveArchivePath []
textContent :: Map Text ByteString -> Text -> Either ParseError Text
textContent :: Map Text ByteString -> Text -> Either ParseError Text
textContent Map Text ByteString
content Text
path = do
canonical <- Text -> Either ParseError Text
checkedPath Text
path
bytes <- maybe (parseRefusal "Cargo declared text file has no regular content") Right (Map.lookup canonical content)
first (const (ParseError "Cargo publication text is not UTF-8")) (TE.decodeUtf8' bytes)
manifestDependencies :: Table -> Either ParseError [Value]
manifestDependencies :: Table -> Either ParseError [Value]
manifestDependencies = (Maybe Text
-> Text -> Text -> Value' () -> Either ParseError Value)
-> Table -> Either ParseError [Value]
forall a.
(Maybe Text -> Text -> Text -> Value' () -> Either ParseError a)
-> Table -> Either ParseError [a]
manifestDependenciesWith Maybe Text -> Text -> Text -> Value' () -> Either ParseError Value
dependency
manifestDependenciesWith :: (Maybe Text -> Text -> Text -> Toml.Value -> Either ParseError a) -> Table -> Either ParseError [a]
manifestDependenciesWith :: forall a.
(Maybe Text -> Text -> Text -> Value' () -> Either ParseError a)
-> Table -> Either ParseError [a]
manifestDependenciesWith Maybe Text -> Text -> Text -> Value' () -> Either ParseError a
visit Table
root = do
direct <- (Maybe Text -> Text -> Text -> Value' () -> Either ParseError a)
-> Maybe Text -> Table -> Either ParseError [a]
forall a.
(Maybe Text -> Text -> Text -> Value' () -> Either ParseError a)
-> Maybe Text -> Table -> Either ParseError [a]
dependencyGroups Maybe Text -> Text -> Text -> Value' () -> Either ParseError a
visit Maybe Text
forall a. Maybe a
Nothing Table
root
targets <- optionalTable "target" root
targeted <- forM (Map.toList targets) $ \(Text
target, Value' ()
value) -> Value' () -> Either ParseError Table
table Value' ()
value Either ParseError Table
-> (Table -> Either ParseError [a]) -> Either ParseError [a]
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe Text -> Text -> Text -> Value' () -> Either ParseError a)
-> Maybe Text -> Table -> Either ParseError [a]
forall a.
(Maybe Text -> Text -> Text -> Value' () -> Either ParseError a)
-> Maybe Text -> Table -> Either ParseError [a]
dependencyGroups Maybe Text -> Text -> Text -> Value' () -> Either ParseError a
visit (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
target)
pure (direct <> concat targeted)
dependencyGroups :: (Maybe Text -> Text -> Text -> Toml.Value -> Either ParseError a) -> Maybe Text -> Table -> Either ParseError [a]
dependencyGroups :: forall a.
(Maybe Text -> Text -> Text -> Value' () -> Either ParseError a)
-> Maybe Text -> Table -> Either ParseError [a]
dependencyGroups Maybe Text -> Text -> Text -> Value' () -> Either ParseError a
visit Maybe Text
target Table
values =
[[a]] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
([[a]] -> [a]) -> Either ParseError [[a]] -> Either ParseError [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Text, Text, Text) -> Either ParseError [a])
-> [(Text, Text, Text)] -> Either ParseError [[a]]
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
(Text, Text, Text) -> Either ParseError [a]
dependencyGroup
[(Text
"dependencies", Text
"dependencies", Text
"normal"), (Text
"dev-dependencies", Text
"dev_dependencies", Text
"dev"), (Text
"build-dependencies", Text
"build_dependencies", Text
"build")]
where
dependencyGroup :: (Text, Text, Text) -> Either ParseError [a]
dependencyGroup (Text
key, Text
legacy, Text
kind) = do
dependencies <- if Text
key Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
legacy then Text -> Table -> Either ParseError Table
optionalTable Text
key Table
values else Text -> Text -> Table -> Either ParseError (Maybe (Value' ()))
aliased Text
key Text
legacy Table
values Either ParseError (Maybe (Value' ()))
-> (Maybe (Value' ()) -> Either ParseError Table)
-> Either ParseError Table
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either ParseError Table
-> (Value' () -> Either ParseError Table)
-> Maybe (Value' ())
-> Either ParseError Table
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Table -> Either ParseError Table
forall a b. b -> Either a b
Right Table
forall k a. Map k a
Map.empty) Value' () -> Either ParseError Table
table
traverse (uncurry (visit target kind)) (Map.toList dependencies)
refuseUnstableKeys :: Maybe Text -> Text -> Text -> Toml.Value -> Either ParseError ()
refuseUnstableKeys :: Maybe Text -> Text -> Text -> Value' () -> Either ParseError ()
refuseUnstableKeys Maybe Text
_ Text
_ Text
alias = \case
Toml.Table' ()
_ Table' ()
declared
| (Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Text -> Table -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`Map.member` Table' () -> Table
tableValues Table' ()
declared) [Text]
unstableManifestKeys ->
Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal (Text
"Cargo manifest dependency '" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
alias Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"' carries an unstable " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Text] -> Text
joinAlternatives [Text]
unstableManifestKeys Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" key")
Value' ()
_ -> () -> Either ParseError ()
forall a b. b -> Either a b
Right ()
dependency :: Maybe Text -> Text -> Text -> Toml.Value -> Either ParseError Value
dependency :: Maybe Text -> Text -> Text -> Value' () -> Either ParseError Value
dependency Maybe Text
target Text
kind Text
alias Value' ()
value = do
details <- case Value' ()
value of
Toml.Text' ()
_ Text
requirement -> Table -> Either ParseError Table
forall a. a -> Either ParseError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Value' () -> Table
forall k a. k -> a -> Map k a
Map.singleton Text
"version" (() -> Text -> Value' ()
forall a. a -> Text -> Value' a
Toml.Text' () Text
requirement))
Value' ()
_ -> Value' () -> Either ParseError Table
table Value' ()
value
when (any (`Map.member` details) ["workspace", "path", "git", "branch", "rev", "tag", "registry"]) (parseRefusal "Cargo dependency retains an unresolved source declaration")
requirement <- required "version" details >>= string
when (T.null requirement) (parseRefusal "Cargo dependency has an empty version requirement")
original <- fromMaybe alias <$> optionalString "package" details
features <- optionalStrings "features" details
isOptional <- maybe (Right False) boolean (Map.lookup "optional" details)
defaultFeatures <- aliased "default-features" "default_features" details >>= maybe (Right True) boolean
registry <- fromMaybe cratesIoIndex <$> optionalString "registry-index" details
pure $
object
[ "name" .= original
, "version_req" .= requirement
, "features" .= features
, "optional" .= isOptional
, "default_features" .= defaultFeatures
, "target" .= target
, "kind" .= kind
, "registry" .= registry
, "explicit_name_in_toml" .= (if original == alias then Nothing else Just alias)
]
cratesIoIndex :: Text
cratesIoIndex :: Text
cratesIoIndex = Text
"https://github.com/rust-lang/crates.io-index"
unstableDependencyFields :: [Key.Key]
unstableDependencyFields :: [Key]
unstableDependencyFields = [Key
"artifact", Key
"bindep_target", Key
"lib", Key
"public"]
unstableManifestKeys :: [Text]
unstableManifestKeys :: [Text]
unstableManifestKeys = (Key -> Text) -> [Key] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Text
manifestKey (Text -> Text) -> (Key -> Text) -> Key -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> Text
Key.toText) [Key]
unstableDependencyFields
where
manifestKey :: Text -> Text
manifestKey = \case
Text
"bindep_target" -> Text
"target"
Text
field -> Text
field
usesUnstableDependencyField :: KeyMap.KeyMap Value -> Bool
usesUnstableDependencyField :: Object -> Bool
usesUnstableDependencyField Object
fields = (Key -> Bool) -> [Key] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Key -> Object -> Bool
forall a. Key -> KeyMap a -> Bool
`KeyMap.member` Object
fields) [Key]
unstableDependencyFields
indexDependency :: Value -> Either ParseError Value
indexDependency :: Value -> Either ParseError Value
indexDependency (Object Object
fields) = do
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Object -> Bool
usesUnstableDependencyField Object
fields) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"unsupported Cargo index dependency capability")
alias <- Key -> Object -> Either ParseError Value
requiredJson Key
"name" Object
fields Either ParseError Value
-> (Value -> Either ParseError Text) -> Either ParseError Text
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> Either ParseError Text
jsonString
original <- nullableString "package" fields
requirement <- requiredJson "req" fields >>= jsonString
features <- maybe (Right []) array (KeyMap.lookup "features" fields)
traverse_ jsonString features
isOptional <- maybe (Right False) jsonBool (KeyMap.lookup "optional" fields)
defaults <- maybe (Right True) jsonBool (KeyMap.lookup "default_features" fields)
target <- nullableString "target" fields
kind <- fromMaybe "normal" <$> nullableString "kind" fields
unless (kind `elem` ["normal", "dev", "build"]) (parseRefusal "unsupported Cargo dependency kind")
registry <- fromMaybe cratesIoIndex <$> nullableString "registry" fields
pure $
object
[ "name" .= fromMaybe alias original
, "version_req" .= requirement
, "features" .= features
, "optional" .= isOptional
, "default_features" .= defaults
, "target" .= target
, "kind" .= kind
, "registry" .= registry
, "explicit_name_in_toml" .= (alias <$ original)
]
indexDependency Value
_ = Text -> Either ParseError Value
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo index dependency is not an object"
type DependencyKey = (Text, Maybe Text, Maybe CargoTarget, Text)
dependencyKey :: Limits -> Value -> Either ParseError DependencyKey
dependencyKey :: Limits -> Value -> Either ParseError DependencyKey
dependencyKey Limits
limits (Object Object
fields) =
(,,,)
(Text -> Maybe Text -> Maybe CargoTarget -> Text -> DependencyKey)
-> Either ParseError Text
-> Either
ParseError
(Maybe Text -> Maybe CargoTarget -> Text -> DependencyKey)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Key -> Object -> Either ParseError Value
requiredJson Key
"name" Object
fields Either ParseError Value
-> (Value -> Either ParseError Text) -> Either ParseError Text
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> Either ParseError Text
jsonString)
Either
ParseError
(Maybe Text -> Maybe CargoTarget -> Text -> DependencyKey)
-> Either ParseError (Maybe Text)
-> Either ParseError (Maybe CargoTarget -> Text -> DependencyKey)
forall a b.
Either ParseError (a -> b)
-> Either ParseError a -> Either ParseError b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Key -> Object -> Either ParseError (Maybe Text)
nullableString Key
"explicit_name_in_toml" Object
fields
Either ParseError (Maybe CargoTarget -> Text -> DependencyKey)
-> Either ParseError (Maybe CargoTarget)
-> Either ParseError (Text -> DependencyKey)
forall a b.
Either ParseError (a -> b)
-> Either ParseError a -> Either ParseError b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Key -> Object -> Either ParseError (Maybe Text)
nullableString Key
"target" Object
fields Either ParseError (Maybe Text)
-> (Maybe Text -> Either ParseError (Maybe CargoTarget))
-> Either ParseError (Maybe CargoTarget)
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Text -> Either ParseError CargoTarget)
-> Maybe Text -> Either ParseError (Maybe CargoTarget)
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) -> Maybe a -> f (Maybe b)
traverse (Limits -> Text -> Either ParseError CargoTarget
parseTarget Limits
limits))
Either ParseError (Text -> DependencyKey)
-> Either ParseError Text -> Either ParseError DependencyKey
forall a b.
Either ParseError (a -> b)
-> Either ParseError a -> Either ParseError b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Key -> Object -> Either ParseError Value
requiredJson Key
"kind" Object
fields Either ParseError Value
-> (Value -> Either ParseError Text) -> Either ParseError Text
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> Either ParseError Text
jsonString)
dependencyKey Limits
_ Value
_ = Text -> Either ParseError DependencyKey
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo publication dependency is not an object"
insertDependency :: Text -> Limits -> Map DependencyKey Value -> Value -> Either ParseError (Map DependencyKey Value)
insertDependency :: Text
-> Limits
-> Map DependencyKey Value
-> Value
-> Either ParseError (Map DependencyKey Value)
insertDependency Text
refusal Limits
limits Map DependencyKey Value
values Value
dependencyValue = do
key <- Limits -> Value -> Either ParseError DependencyKey
dependencyKey Limits
limits Value
dependencyValue
when (Map.member key values) (parseRefusal refusal)
pure (Map.insert key dependencyValue values)
indexRepeat :: Text
indexRepeat :: Text
indexRepeat = Text
"Cargo index repeats a dependency identity"
checkDependency :: Value -> Value -> Either ParseError ()
checkDependency :: Value -> Value -> Either ParseError ()
checkDependency (Object Object
expected) (Object Object
actual) = do
[Key] -> (Key -> Either ParseError ()) -> Either ParseError ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Key
"optional", Key
"default_features", Key
"registry"] ((Key -> Either ParseError ()) -> Either ParseError ())
-> (Key -> Either ParseError ()) -> Either ParseError ()
forall a b. (a -> b) -> a -> b
$ \Key
key ->
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
key Object
expected Maybe Value -> Maybe Value -> Bool
forall a. Eq a => a -> a -> Bool
== Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
key Object
actual) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo manifest and index dependency facts disagree")
expectedFeatures <- Key -> Object -> Either ParseError Value
requiredJson Key
"features" Object
expected
actualFeatures <- requiredJson "features" actual
checkFeatureValues expectedFeatures actualFeatures
expectedRequirement <- requiredJson "version_req" expected >>= jsonString
actualRequirement <- requiredJson "version_req" actual >>= jsonString
unless (normalRequirement expectedRequirement == normalRequirement actualRequirement) (parseRefusal "Cargo manifest and index dependency requirements disagree")
checkDependency Value
_ Value
_ = Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo publication dependency is not an object"
checkFeatureValues :: Value -> Value -> Either ParseError ()
checkFeatureValues :: Value -> Value -> Either ParseError ()
checkFeatureValues Value
expected Value
actual = do
expectedValues <- Value -> Either ParseError [Value]
array Value
expected Either ParseError [Value]
-> ([Value] -> Either ParseError [Text])
-> Either ParseError [Text]
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Value -> Either ParseError Text)
-> [Value] -> Either ParseError [Text]
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 Value -> Either ParseError Text
jsonString
actualValues <- array actual >>= traverse jsonString
unless (Set.fromList expectedValues == Set.fromList actualValues) (parseRefusal "Cargo manifest and index feature declarations disagree")
normalRequirement :: Text -> Set Text
normalRequirement :: Text -> Set Text
normalRequirement = [Text] -> Set Text
forall a. Ord a => [a] -> Set a
Set.fromList ([Text] -> Set Text) -> (Text -> [Text]) -> Text -> Set Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
comparator ([Text] -> [Text]) -> (Text -> [Text]) -> Text -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
","
where
comparator :: Text -> Text
comparator Text
value =
let withoutBuild :: Text
withoutBuild = (Text, Text) -> Text
forall a b. (a, b) -> a
fst (HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
"+" (Text -> Text
T.strip Text
value))
(Text
operator, Text
release) = (Char -> Bool) -> Text -> (Text, Text)
T.span (Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Char
'=', Char
'>', Char
'<', Char
'~', Char
'^']) Text
withoutBuild
(Text
core, Text
prerelease) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
"-" (Text -> Text
T.stripStart Text
release)
parts :: [Text]
parts = HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"." Text
core
wildcard :: a -> Bool
wildcard a
part = a
part a -> [a] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [a
"*", a
"x", a
"X"]
([Text]
numbers, [Text]
suffix) = (Text -> Bool) -> [Text] -> ([Text], [Text])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break Text -> Bool
forall {a}. (Eq a, IsString a) => a -> Bool
wildcard [Text]
parts
nativeWildcard :: Bool
nativeWildcard = Bool -> Bool
not ([Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
numbers) Bool -> Bool -> Bool
&& Bool -> Bool
not ([Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
suffix) Bool -> Bool -> Bool
&& (Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Text -> Bool
forall {a}. (Eq a, IsString a) => a -> Bool
wildcard [Text]
suffix Bool -> Bool -> Bool
&& [Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
parts Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
3
effectiveOperator :: Text
effectiveOperator
| Bool -> Bool
not (Text -> Bool
T.null Text
operator) = Text
operator
| [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
suffix, Just (Char
firstChar, Text
_) <- Text -> Maybe (Char, Text)
T.uncons Text
core, Char -> Bool
isDigit Char
firstChar = Text
"^"
| Bool
otherwise = Text
""
nativeCore :: Text
nativeCore
| Text -> Bool
forall {a}. (Eq a, IsString a) => a -> Bool
wildcard Text
core = Text
"*"
| Bool
nativeWildcard = Text -> [Text] -> Text
T.intercalate Text
"." ([Text]
numbers [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Text
"*" | Text -> Bool
T.null Text
operator])
| Bool
otherwise = Text
core
in Text
effectiveOperator Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
nativeCore Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
prerelease
indexFeatures :: KeyMap.KeyMap Value -> Either ParseError (KeyMap.KeyMap Value)
indexFeatures :: Object -> Either ParseError Object
indexFeatures Object
index = do
primary <- Either ParseError Object
-> (Value -> Either ParseError Object)
-> Maybe Value
-> Either ParseError Object
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Object -> Either ParseError Object
forall a b. b -> Either a b
Right Object
forall v. KeyMap v
KeyMap.empty) Value -> Either ParseError Object
jsonObject (Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"features" Object
index)
extra <- maybe (Right KeyMap.empty) jsonObject (KeyMap.lookup "features2" index)
traverse_ (array >=> traverse_ jsonString) primary
traverse_ (array >=> traverse_ jsonString) extra
pure (KeyMap.unionWith merge primary extra)
where
merge :: Value -> Value -> Value
merge (Array Array
firstValues) (Array Array
nextValues) = Array -> Value
Array (Array
firstValues Array -> Array -> Array
forall a. Semigroup a => a -> a -> a
<> Array
nextValues)
merge Value
firstValue Value
_ = Value
firstValue
requiredJson :: Key.Key -> KeyMap.KeyMap Value -> Either ParseError Value
requiredJson :: Key -> Object -> Either ParseError Value
requiredJson Key
key = Either ParseError Value
-> (Value -> Either ParseError Value)
-> Maybe Value
-> Either ParseError Value
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text -> Either ParseError Value
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo index lacks a required publication field") Value -> Either ParseError Value
forall a b. b -> Either a b
Right (Maybe Value -> Either ParseError Value)
-> (Object -> Maybe Value) -> Object -> Either ParseError Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
key
nullableString :: Key.Key -> KeyMap.KeyMap Value -> Either ParseError (Maybe Text)
nullableString :: Key -> Object -> Either ParseError (Maybe Text)
nullableString Key
key Object
values = case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
key Object
values of
Maybe Value
Nothing -> Maybe Text -> Either ParseError (Maybe Text)
forall a b. b -> Either a b
Right Maybe Text
forall a. Maybe a
Nothing
Just Value
Null -> Maybe Text -> Either ParseError (Maybe Text)
forall a b. b -> Either a b
Right Maybe Text
forall a. Maybe a
Nothing
Just Value
value -> Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text)
-> Either ParseError Text -> Either ParseError (Maybe Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Either ParseError Text
jsonString Value
value
jsonString :: Value -> Either ParseError Text
jsonString :: Value -> Either ParseError Text
jsonString (String Text
value) = Text -> Either ParseError Text
forall a b. b -> Either a b
Right Text
value
jsonString Value
_ = Text -> Either ParseError Text
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo index publication field is not a string"
jsonBool :: Value -> Either ParseError Bool
jsonBool :: Value -> Either ParseError Bool
jsonBool (Bool Bool
value) = Bool -> Either ParseError Bool
forall a b. b -> Either a b
Right Bool
value
jsonBool Value
_ = Text -> Either ParseError Bool
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo index publication field is not a boolean"
jsonObject :: Value -> Either ParseError (KeyMap.KeyMap Value)
jsonObject :: Value -> Either ParseError Object
jsonObject (Object Object
value) = Object -> Either ParseError Object
forall a b. b -> Either a b
Right Object
value
jsonObject Value
_ = Text -> Either ParseError Object
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo index publication field is not an object"
array :: Value -> Either ParseError [Value]
array :: Value -> Either ParseError [Value]
array (Array Array
values) = [Value] -> Either ParseError [Value]
forall a b. b -> Either a b
Right (Array -> [Value]
forall a. Vector a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Array
values)
array Value
_ = Text -> Either ParseError [Value]
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo index publication field is not an array"