module Ecluse.Core.Registry.PyPI.Document (
SimpleDocument,
simpleDocument,
simpleEnvelope,
simpleFiles,
restrictSimpleFiles,
simpleSourceCompanions,
simpleEncoding,
PackedSimple (..),
packedSimple,
packedSimpleDocument,
packedSimplePlan,
packedSimpleResident,
urlHole,
) where
import Data.Aeson (Encoding, Object, Value, toEncoding)
import Data.Aeson.Encoding qualified as Encoding
import Data.Aeson.Key (Key)
import Data.Aeson.KeyMap qualified as KeyMap
import Data.Primitive.SmallArray (smallArrayFromList)
import Data.Set qualified as Set
import Ecluse.Core.Package.Entry (EntryKey)
import Ecluse.Core.Registry (CompanionRequirement (DeclaredCompanions))
import Ecluse.Core.Registry.Json.Packed (DocTable, Packed, Piece (..), Pieces (ArrayPieces), RenderPlan (..), packedResident, packedValue, tableResident)
data SimpleDocument = SimpleDocument
{ SimpleDocument -> Object
simpleEnvelope :: Object
, SimpleDocument -> [(EntryKey, Value)]
simpleFiles :: [(EntryKey, Value)]
}
deriving stock (SimpleDocument -> SimpleDocument -> Bool
(SimpleDocument -> SimpleDocument -> Bool)
-> (SimpleDocument -> SimpleDocument -> Bool) -> Eq SimpleDocument
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SimpleDocument -> SimpleDocument -> Bool
== :: SimpleDocument -> SimpleDocument -> Bool
$c/= :: SimpleDocument -> SimpleDocument -> Bool
/= :: SimpleDocument -> SimpleDocument -> Bool
Eq, Int -> SimpleDocument -> ShowS
[SimpleDocument] -> ShowS
SimpleDocument -> String
(Int -> SimpleDocument -> ShowS)
-> (SimpleDocument -> String)
-> ([SimpleDocument] -> ShowS)
-> Show SimpleDocument
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SimpleDocument -> ShowS
showsPrec :: Int -> SimpleDocument -> ShowS
$cshow :: SimpleDocument -> String
show :: SimpleDocument -> String
$cshowList :: [SimpleDocument] -> ShowS
showList :: [SimpleDocument] -> ShowS
Show)
simpleDocument :: Object -> [(EntryKey, Value)] -> SimpleDocument
simpleDocument :: Object -> [(EntryKey, Value)] -> SimpleDocument
simpleDocument Object
envelope = Object -> [(EntryKey, Value)] -> SimpleDocument
SimpleDocument (Key -> Object -> Object
forall v. Key -> KeyMap v -> KeyMap v
KeyMap.delete Key
"files" Object
envelope)
restrictSimpleFiles :: Set EntryKey -> SimpleDocument -> SimpleDocument
restrictSimpleFiles :: Set EntryKey -> SimpleDocument -> SimpleDocument
restrictSimpleFiles Set EntryKey
keys SimpleDocument
document =
Object -> [(EntryKey, Value)] -> SimpleDocument
simpleDocument (SimpleDocument -> Object
simpleEnvelope SimpleDocument
document) (((EntryKey, Value) -> Bool)
-> [(EntryKey, Value)] -> [(EntryKey, Value)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((EntryKey -> Set EntryKey -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set EntryKey
keys) (EntryKey -> Bool)
-> ((EntryKey, Value) -> EntryKey) -> (EntryKey, Value) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EntryKey, Value) -> EntryKey
forall a b. (a, b) -> a
fst) (SimpleDocument -> [(EntryKey, Value)]
simpleFiles SimpleDocument
document))
simpleSourceCompanions :: CompanionRequirement
simpleSourceCompanions :: CompanionRequirement
simpleSourceCompanions = CompanionRequirement
DeclaredCompanions
data EnvelopePairs
= NoEnvelopePairs
| PendingFiles !Encoding.Series
| CompletePairs !Encoding.Series
envelopePair :: Encoding.Series -> Key -> Value -> EnvelopePairs -> EnvelopePairs
envelopePair :: Series -> Key -> Value -> EnvelopePairs -> EnvelopePairs
envelopePair Series
files Key
key Value
value !EnvelopePairs
rest = case Key -> Key -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Key
key Key
"files" of
Ordering
GT -> Series -> EnvelopePairs
PendingFiles (Series -> EnvelopePairs -> Series
prependPair (Key -> Value -> Series
encodeField Key
key Value
value) EnvelopePairs
rest)
Ordering
EQ -> Series -> EnvelopePairs
CompletePairs (Series -> EnvelopePairs -> Series
prependPair Series
files EnvelopePairs
rest)
Ordering
LT ->
let !suffix :: Series
suffix = Series -> EnvelopePairs -> Series
finishEnvelope Series
files EnvelopePairs
rest
in Series -> EnvelopePairs
CompletePairs (Key -> Value -> Series
encodeField Key
key Value
value Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Series
suffix)
finishEnvelope :: Encoding.Series -> EnvelopePairs -> Encoding.Series
finishEnvelope :: Series -> EnvelopePairs -> Series
finishEnvelope Series
files = \case
EnvelopePairs
NoEnvelopePairs -> Series
files
PendingFiles Series
rest -> Series
files Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Series
rest
CompletePairs Series
pairs -> Series
pairs
prependPair :: Encoding.Series -> EnvelopePairs -> Encoding.Series
prependPair :: Series -> EnvelopePairs -> Series
prependPair !Series
pair = \case
EnvelopePairs
NoEnvelopePairs -> Series
pair
PendingFiles Series
rest -> Series
pair Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Series
rest
CompletePairs Series
rest -> Series
pair Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Series
rest
encodeField :: Key -> Value -> Encoding.Series
encodeField :: Key -> Value -> Series
encodeField Key
key Value
value =
let !encoding :: Encoding
encoding = Value -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding Value
value
in Key -> Encoding -> Series
Encoding.pair Key
key Encoding
encoding
simpleEncoding :: SimpleDocument -> Encoding
simpleEncoding :: SimpleDocument -> Encoding
simpleEncoding SimpleDocument
document
| Object -> Bool
forall v. KeyMap v -> Bool
KeyMap.null Object
envelope = Series -> Encoding
Encoding.pairs Series
files
| Bool
otherwise = Series -> Encoding
Encoding.pairs (Series -> EnvelopePairs -> Series
finishEnvelope Series
files ((Key -> Value -> EnvelopePairs -> EnvelopePairs)
-> EnvelopePairs -> Object -> EnvelopePairs
forall v a. (Key -> v -> a -> a) -> a -> KeyMap v -> a
KeyMap.foldrWithKey (Series -> Key -> Value -> EnvelopePairs -> EnvelopePairs
envelopePair Series
files) EnvelopePairs
NoEnvelopePairs Object
envelope))
where
envelope :: Object
envelope = SimpleDocument -> Object
simpleEnvelope SimpleDocument
document
!filesEncoding :: Encoding
filesEncoding = ((EntryKey, Value) -> Encoding) -> [(EntryKey, Value)] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
Encoding.list (Value -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding (Value -> Encoding)
-> ((EntryKey, Value) -> Value) -> (EntryKey, Value) -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EntryKey, Value) -> Value
forall a b. (a, b) -> b
snd) (SimpleDocument -> [(EntryKey, Value)]
simpleFiles SimpleDocument
document)
files :: Series
files = Key -> Encoding -> Series
Encoding.pair Key
"files" Encoding
filesEncoding
data PackedSimple = PackedSimple
{ PackedSimple -> Object
packedEnvelope :: Object
, PackedSimple -> DocTable
packedTable :: DocTable
, PackedSimple -> [(EntryKey, Packed)]
packedFiles :: [(EntryKey, Packed)]
}
deriving stock (PackedSimple -> PackedSimple -> Bool
(PackedSimple -> PackedSimple -> Bool)
-> (PackedSimple -> PackedSimple -> Bool) -> Eq PackedSimple
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PackedSimple -> PackedSimple -> Bool
== :: PackedSimple -> PackedSimple -> Bool
$c/= :: PackedSimple -> PackedSimple -> Bool
/= :: PackedSimple -> PackedSimple -> Bool
Eq, Int -> PackedSimple -> ShowS
[PackedSimple] -> ShowS
PackedSimple -> String
(Int -> PackedSimple -> ShowS)
-> (PackedSimple -> String)
-> ([PackedSimple] -> ShowS)
-> Show PackedSimple
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PackedSimple -> ShowS
showsPrec :: Int -> PackedSimple -> ShowS
$cshow :: PackedSimple -> String
show :: PackedSimple -> String
$cshowList :: [PackedSimple] -> ShowS
showList :: [PackedSimple] -> ShowS
Show)
packedSimple :: Object -> DocTable -> [(EntryKey, Packed)] -> PackedSimple
packedSimple :: Object -> DocTable -> [(EntryKey, Packed)] -> PackedSimple
packedSimple Object
envelope = Object -> DocTable -> [(EntryKey, Packed)] -> PackedSimple
PackedSimple (Key -> Object -> Object
forall v. Key -> KeyMap v -> KeyMap v
KeyMap.delete Key
"files" Object
envelope)
packedSimpleDocument :: PackedSimple -> Maybe SimpleDocument
packedSimpleDocument :: PackedSimple -> Maybe SimpleDocument
packedSimpleDocument PackedSimple
document =
Object -> [(EntryKey, Value)] -> SimpleDocument
simpleDocument (PackedSimple -> Object
packedEnvelope PackedSimple
document) ([(EntryKey, Value)] -> SimpleDocument)
-> Maybe [(EntryKey, Value)] -> Maybe SimpleDocument
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((EntryKey, Packed) -> Maybe (EntryKey, Value))
-> [(EntryKey, Packed)] -> Maybe [(EntryKey, Value)]
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 (\(EntryKey
key, Packed
file) -> (EntryKey
key,) (Value -> (EntryKey, Value))
-> Maybe Value -> Maybe (EntryKey, Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DocTable -> Packed -> Maybe Value
packedValue (PackedSimple -> DocTable
packedTable PackedSimple
document) Packed
file) (PackedSimple -> [(EntryKey, Packed)]
packedFiles PackedSimple
document)
packedSimplePlan :: PackedSimple -> RenderPlan
packedSimplePlan :: PackedSimple -> RenderPlan
packedSimplePlan PackedSimple
document =
RenderPlan
{ planMembers :: Object
planMembers = PackedSimple -> Object
packedEnvelope PackedSimple
document
, planSlot :: Key
planSlot = Key
"files"
, planTables :: SmallArray DocTable
planTables = [DocTable] -> SmallArray DocTable
forall a. [a] -> SmallArray a
smallArrayFromList [PackedSimple -> DocTable
packedTable PackedSimple
document]
, planPieces :: Pieces
planPieces = [Piece] -> Pieces
ArrayPieces [Int -> Packed -> Piece
Piece Int
0 Packed
file | (EntryKey
_, Packed
file) <- PackedSimple -> [(EntryKey, Packed)]
packedFiles PackedSimple
document]
, planPrefix :: Maybe UrlPrefix
planPrefix = Maybe UrlPrefix
forall a. Maybe a
Nothing
}
packedSimpleResident :: PackedSimple -> Int
packedSimpleResident :: PackedSimple -> Int
packedSimpleResident PackedSimple
document = DocTable -> Int
tableResident (PackedSimple -> DocTable
packedTable PackedSimple
document) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [Int] -> Int
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum [Int
64 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Packed -> Int
packedResident Packed
file | (EntryKey
_, Packed
file) <- PackedSimple -> [(EntryKey, Packed)]
packedFiles PackedSimple
document]
urlHole :: [Text]
urlHole :: [Text]
urlHole = [Text
"url"]