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

-- | Strict native index records retain additive fields and distinguish null from absence.
module Ecluse.Core.Registry.Cargo.Wire (
    CargoRecordError (..),
    decodeCargoRecord,
    validateCargoRecord,
    recordIdentity,
    cargoTreeCharge,
) where

import Data.Aeson (Value (..))
import Data.Aeson.Key qualified as Key
import Data.Aeson.KeyMap qualified as KeyMap
import Data.Aeson.Parser (jsonNoDup')
import Data.Attoparsec.ByteString (endOfInput, parseOnly, skipWhile)
import Data.ByteString qualified as BS
import Data.Scientific (toBoundedInteger)

import Ecluse.Core.Registry.JsonStream (isJsonSpace)
import Ecluse.Core.Registry.ServedDocument (stringField)
import Ecluse.Core.Security (LimitError (TooDeeplyNested))
import Ecluse.Core.Server.MemoryModel (saturatingCharge)
import Ecluse.Core.Version.Cargo (cargoUniquenessKey, parseCargo)

-- | Malformed rows drop individually. Resource refusals stop the whole source read.
data CargoRecordError = CargoRecordMalformed Text | CargoRecordBound LimitError
    deriving stock (CargoRecordError -> CargoRecordError -> Bool
(CargoRecordError -> CargoRecordError -> Bool)
-> (CargoRecordError -> CargoRecordError -> Bool)
-> Eq CargoRecordError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CargoRecordError -> CargoRecordError -> Bool
== :: CargoRecordError -> CargoRecordError -> Bool
$c/= :: CargoRecordError -> CargoRecordError -> Bool
/= :: CargoRecordError -> CargoRecordError -> Bool
Eq, Int -> CargoRecordError -> ShowS
[CargoRecordError] -> ShowS
CargoRecordError -> String
(Int -> CargoRecordError -> ShowS)
-> (CargoRecordError -> String)
-> ([CargoRecordError] -> ShowS)
-> Show CargoRecordError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CargoRecordError -> ShowS
showsPrec :: Int -> CargoRecordError -> ShowS
$cshow :: CargoRecordError -> String
show :: CargoRecordError -> String
$cshowList :: [CargoRecordError] -> ShowS
showList :: [CargoRecordError] -> ShowS
Show)

-- | Decode exactly one bounded object and reject duplicate member names at every depth.
decodeCargoRecord :: Int -> ByteString -> Either CargoRecordError Value
decodeCargoRecord :: Int -> ByteString -> Either CargoRecordError Value
decodeCargoRecord Int
depth ByteString
bytes = do
    (LimitError -> CargoRecordError)
-> Either LimitError () -> Either CargoRecordError ()
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 LimitError -> CargoRecordError
CargoRecordBound (Int -> ByteString -> Either LimitError ()
checkDepthBeforeDecode Int
depth ByteString
bytes)
    value <- (String -> CargoRecordError)
-> Either String Value -> Either CargoRecordError 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 -> String -> CargoRecordError
forall a b. a -> b -> a
const (Text -> CargoRecordError
CargoRecordMalformed Text
"malformed Cargo index record")) (Parser Value -> ByteString -> Either String Value
forall a. Parser a -> ByteString -> Either String a
parseOnly (Parser Value
jsonNoDup' Parser Value -> Parser ByteString () -> Parser Value
forall a b.
Parser ByteString a -> Parser ByteString b -> Parser ByteString a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* (Word8 -> Bool) -> Parser ByteString ()
skipWhile Word8 -> Bool
isJsonSpace Parser Value -> Parser ByteString () -> Parser Value
forall a b.
Parser ByteString a -> Parser ByteString b -> Parser ByteString a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser ByteString ()
forall t. Chunk t => Parser t ()
endOfInput) ByteString
bytes)
    unless (withinDepth depth value) (Left (CargoRecordBound (TooDeeplyNested depth)))
    case value of
        Object Object
_ -> Value -> Either CargoRecordError Value
forall a b. b -> Either a b
Right Value
value
        Value
_ -> CargoRecordError -> Either CargoRecordError Value
forall a b. a -> Either a b
Left (Text -> CargoRecordError
CargoRecordMalformed Text
"Cargo index record is not an object")

-- | Reserve bounded native parser trees and comparison structures before retaining them.
cargoTreeCharge :: Integer -> Int
cargoTreeCharge :: Integer -> Int
cargoTreeCharge Integer
bytes = Integer -> Int
saturatingCharge (Integer
65536 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
64 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
max Integer
0 Integer
bytes)

-- | A syntactically native version claims uniqueness even when another record field is invalid.
recordIdentity :: Value -> Maybe Text
recordIdentity :: Value -> Maybe Text
recordIdentity = \case
    Object Object
fields -> Key -> Object -> Maybe Text
stringField Key
"vers" Object
fields Maybe Text -> (Text -> Maybe CargoKey) -> Maybe CargoKey
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Maybe CargoKey
parseCargo Maybe CargoKey -> (CargoKey -> Text) -> Maybe Text
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> CargoKey -> Text
cargoUniquenessKey
    Value
_ -> Maybe Text
forall a. Maybe a
Nothing

-- | Validate supported installation schema without interpreting Cargo's resolver expressions.
validateCargoRecord :: Value -> Either Text ()
validateCargoRecord :: Value -> Either Text ()
validateCargoRecord = \case
    Object Object
fields -> do
        (Key -> Either Text ()) -> [Key] -> Either Text ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((Value -> Bool) -> Object -> Key -> Either Text ()
required Value -> Bool
isString Object
fields) [Key
"name", Key
"vers", Key
"cksum"]
        (Value -> Bool) -> Object -> Key -> Either Text ()
required Value -> Bool
isArray Object
fields Key
"deps"
        case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"deps" Object
fields of
            Just (Array Array
dependencies) -> (Value -> Either Text ()) -> Array -> Either Text ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ Value -> Either Text ()
dependency Array
dependencies
            Maybe Value
_ -> Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"invalid Cargo dependencies"
        (Value -> Bool) -> Object -> Key -> Either Text ()
optionalField Value -> Bool
featureMap Object
fields Key
"features"
        (Value -> Bool) -> Object -> Key -> Either Text ()
optionalField Value -> Bool
isBool Object
fields Key
"yanked"
        (Value -> Bool) -> Object -> Key -> Either Text ()
optionalField Value -> Bool
featureMap Object
fields Key
"features2"
        (Key -> Either Text ()) -> [Key] -> Either Text ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((Value -> Bool) -> Object -> Key -> Either Text ()
optionalField Value -> Bool
nullableText Object
fields) [Key
"links", Key
"rust_version"]
        case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"v" Object
fields of
            Maybe Value
Nothing -> () -> Either Text ()
forall a b. b -> Either a b
Right ()
            Just (Number Scientific
number) | Just Int
schema <- forall i. (Integral i, Bounded i) => Scientific -> Maybe i
toBoundedInteger @Int Scientific
number, Int
schema Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 Bool -> Bool -> Bool
|| Int
schema Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2 -> () -> Either Text ()
forall a b. b -> Either a b
Right ()
            Maybe Value
_ -> Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"unsupported or invalid Cargo index schema"
    Value
_ -> Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"Cargo index record is not an object"
  where
    dependency :: Value -> Either Text ()
dependency = \case
        Object Object
fields -> do
            (Key -> Either Text ()) -> [Key] -> Either Text ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((Value -> Bool) -> Object -> Key -> Either Text ()
required Value -> Bool
isString Object
fields) [Key
"name", Key
"req"]
            (Value -> Bool) -> Object -> Key -> Either Text ()
optionalField Value -> Bool
stringArray Object
fields Key
"features"
            (Key -> Either Text ()) -> [Key] -> Either Text ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((Value -> Bool) -> Object -> Key -> Either Text ()
optionalField Value -> Bool
isBool Object
fields) [Key
"optional", Key
"default_features", Key
"lib"]
            (Key -> Either Text ()) -> [Key] -> Either Text ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((Value -> Bool) -> Object -> Key -> Either Text ()
optionalField Value -> Bool
nullableText Object
fields) [Key
"target", Key
"kind", Key
"registry", Key
"package", Key
"bindep_target"]
        Value
_ -> Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"invalid Cargo dependency"

required :: (Value -> Bool) -> KeyMap.KeyMap Value -> Key.Key -> Either Text ()
required :: (Value -> Bool) -> Object -> Key -> Either Text ()
required Value -> Bool
predicate Object
fields Key
key = case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
key Object
fields of
    Just Value
value | Value -> Bool
predicate Value
value -> () -> 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 field: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Key -> Text
Key.toText Key
key)

optionalField :: (Value -> Bool) -> KeyMap.KeyMap Value -> Key.Key -> Either Text ()
optionalField :: (Value -> Bool) -> Object -> Key -> Either Text ()
optionalField Value -> Bool
predicate Object
fields Key
key = case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
key Object
fields of
    Maybe Value
Nothing -> () -> Either Text ()
forall a b. b -> Either a b
Right ()
    Just Value
value -> Bool -> Either Text () -> Either Text ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Value -> Bool
predicate Value
value) (Text -> Either Text ()
forall a b. a -> Either a b
Left (Text
"invalid Cargo field: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Key -> Text
Key.toText Key
key))

isString, nullableText, isBool, isArray, stringArray, featureMap :: Value -> Bool
isString :: Value -> Bool
isString = \case String Text
_ -> Bool
True; Value
_ -> Bool
False
nullableText :: Value -> Bool
nullableText = \case Value
Null -> Bool
True; Value
value -> Value -> Bool
isString Value
value
isBool :: Value -> Bool
isBool = \case Bool Bool
_ -> Bool
True; Value
_ -> Bool
False
isArray :: Value -> Bool
isArray = \case Array Array
_ -> Bool
True; Value
_ -> Bool
False
stringArray :: Value -> Bool
stringArray = \case Array Array
entries -> (Value -> Bool) -> Array -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Value -> Bool
isString Array
entries; Value
_ -> Bool
False
featureMap :: Value -> Bool
featureMap = \case Object Object
fields -> (Value -> Bool) -> Object -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Value -> Bool
stringArray Object
fields; Value
_ -> Bool
False

withinDepth :: Int -> Value -> Bool
withinDepth :: Int -> Value -> Bool
withinDepth Int
depth Value
value
    | Int
depth Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Bool
False
    | Bool
otherwise = case Value
value of
        Object Object
fields -> (Value -> Bool) -> Object -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Int -> Value -> Bool
withinDepth (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) Object
fields
        Array Array
items -> (Value -> Bool) -> Array -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Int -> Value -> Bool
withinDepth (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) Array
items
        Value
_ -> Bool
True

-- Reject deep container chains before aeson allocates their tree.
checkDepthBeforeDecode :: Int -> ByteString -> Either LimitError ()
checkDepthBeforeDecode :: Int -> ByteString -> Either LimitError ()
checkDepthBeforeDecode Int
limit ByteString
bytes = Int -> Int -> Bool -> Bool -> Either LimitError ()
go Int
0 Int
0 Bool
False Bool
False
  where
    go :: Int -> Int -> Bool -> Bool -> Either LimitError ()
go !Int
at !Int
depth Bool
quoted Bool
escaped = case ByteString -> Int -> Maybe Word8
BS.indexMaybe ByteString
bytes Int
at of
        Maybe Word8
Nothing -> () -> Either LimitError ()
forall a b. b -> Either a b
Right ()
        Just Word8
byte
            | Bool
quoted Bool -> Bool -> Bool
&& Bool
escaped -> Int -> Bool -> Bool -> Either LimitError ()
next Int
depth Bool
True Bool
False
            | Bool
quoted -> case Word8
byte of
                Word8
92 -> Int -> Bool -> Bool -> Either LimitError ()
next Int
depth Bool
True Bool
True
                Word8
34 -> Int -> Bool -> Bool -> Either LimitError ()
next Int
depth Bool
False Bool
False
                Word8
_ -> Int -> Bool -> Bool -> Either LimitError ()
next Int
depth Bool
True Bool
False
            | Word8
byte Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
34 -> Int -> Bool -> Bool -> Either LimitError ()
next Int
depth Bool
True Bool
False
            | Word8
byte Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
91 Bool -> Bool -> Bool
|| Word8
byte Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
123 -> if Int
depth Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
limit then LimitError -> Either LimitError ()
forall a b. a -> Either a b
Left (Int -> LimitError
TooDeeplyNested Int
limit) else Int -> Bool -> Bool -> Either LimitError ()
next (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Bool
False Bool
False
            | Word8
byte Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
93 Bool -> Bool -> Bool
|| Word8
byte Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
125 -> Int -> Bool -> Bool -> Either LimitError ()
next (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)) Bool
False Bool
False
            | Bool
otherwise -> Int -> Bool -> Bool -> Either LimitError ()
next Int
depth Bool
False Bool
False
      where
        next :: Int -> Bool -> Bool -> Either LimitError ()
next = Int -> Int -> Bool -> Bool -> Either LimitError ()
go (Int
at Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)