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

{- | Native Cargo SemVer with u64 release numbers and ASCII identifiers.
Numeric prerelease identifiers compare by length and text, without a machine-integer limit.
-}
module Ecluse.Core.Version.Cargo (
    CargoKey,
    parseCargo,
    renderCargo,
    isCargoStable,
    cargoUniquenessKey,
) where

import Data.ByteString qualified as BS
import Data.Char (isDigit)
import Data.Text qualified as T

import Ecluse.Core.Strict (strictElements)
import Ecluse.Core.Text (isAsciiAlphaNum, readDecimalText)
import Ecluse.Core.Version.Token (withinVersionLength)

-- | A native ordering key. Equality and precedence exclude build metadata.
data CargoKey = CargoKey Word64 Word64 Word64 [Identifier] (Maybe Text)
    deriving stock (Int -> CargoKey -> ShowS
[CargoKey] -> ShowS
CargoKey -> String
(Int -> CargoKey -> ShowS)
-> (CargoKey -> String) -> ([CargoKey] -> ShowS) -> Show CargoKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CargoKey -> ShowS
showsPrec :: Int -> CargoKey -> ShowS
$cshow :: CargoKey -> String
show :: CargoKey -> String
$cshowList :: [CargoKey] -> ShowS
showList :: [CargoKey] -> ShowS
Show)

data Identifier = Numeric ByteString | Alphanumeric Text
    deriving stock (Identifier -> Identifier -> Bool
(Identifier -> Identifier -> Bool)
-> (Identifier -> Identifier -> Bool) -> Eq Identifier
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Identifier -> Identifier -> Bool
== :: Identifier -> Identifier -> Bool
$c/= :: Identifier -> Identifier -> Bool
/= :: Identifier -> Identifier -> Bool
Eq, Int -> Identifier -> ShowS
[Identifier] -> ShowS
Identifier -> String
(Int -> Identifier -> ShowS)
-> (Identifier -> String)
-> ([Identifier] -> ShowS)
-> Show Identifier
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Identifier -> ShowS
showsPrec :: Int -> Identifier -> ShowS
$cshow :: Identifier -> String
show :: Identifier -> String
$cshowList :: [Identifier] -> ShowS
showList :: [Identifier] -> ShowS
Show)

instance Eq CargoKey where
    CargoKey
a == :: CargoKey -> CargoKey -> Bool
== CargoKey
b = CargoKey -> CargoKey -> Ordering
forall a. Ord a => a -> a -> Ordering
compare CargoKey
a CargoKey
b Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
EQ

instance Ord CargoKey where
    compare :: CargoKey -> CargoKey -> Ordering
compare (CargoKey Word64
a Word64
b Word64
c [Identifier]
pre Maybe Text
_) (CargoKey Word64
x Word64
y Word64
z [Identifier]
other Maybe Text
_) =
        (Word64, Word64, Word64) -> (Word64, Word64, Word64) -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Word64
a, Word64
b, Word64
c) (Word64
x, Word64
y, Word64
z) Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> [Identifier] -> [Identifier] -> Ordering
comparePrerelease [Identifier]
pre [Identifier]
other

instance Ord Identifier where
    compare :: Identifier -> Identifier -> Ordering
compare (Numeric ByteString
a) (Numeric ByteString
b) = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (ByteString -> Int
BS.length ByteString
a) (ByteString -> Int
BS.length ByteString
b) Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> ByteString -> ByteString -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ByteString
a ByteString
b
    compare (Alphanumeric Text
a) (Alphanumeric Text
b) = Text -> Text -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Text
a Text
b
    compare Numeric{} Alphanumeric{} = Ordering
LT
    compare Alphanumeric{} Numeric{} = Ordering
GT

-- | Parse three release numbers and native suffixes within the shared version-text bound.
parseCargo :: Text -> Maybe CargoKey
parseCargo :: Text -> Maybe CargoKey
parseCargo Text
raw = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Text -> Bool
withinVersionLength Text
raw)
    let (Text
version, Text
buildPart) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
"+" Text
raw
        (Text
release, Text
prePart) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
"-" Text
version
    (major, minor, patch) <- case HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"." Text
release of
        [Text
a, Text
b, Text
c] -> (,,) (Word64 -> Word64 -> Word64 -> (Word64, Word64, Word64))
-> Maybe Word64
-> Maybe (Word64 -> Word64 -> (Word64, Word64, Word64))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe Word64
releaseNumber Text
a Maybe (Word64 -> Word64 -> (Word64, Word64, Word64))
-> Maybe Word64 -> Maybe (Word64 -> (Word64, Word64, Word64))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Maybe Word64
releaseNumber Text
b Maybe (Word64 -> (Word64, Word64, Word64))
-> Maybe Word64 -> Maybe (Word64, Word64, Word64)
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Maybe Word64
releaseNumber Text
c
        [Text]
_ -> Maybe (Word64, Word64, Word64)
forall a. Maybe a
Nothing
    prerelease <- if T.null prePart then pure [] else traverse identifier (T.splitOn "." (T.drop 1 prePart))
    build <- if T.null buildPart then pure Nothing else Just <$> buildIdentifiers (T.drop 1 buildPart)
    pure (CargoKey major minor patch (strictElements prerelease) build)

releaseNumber :: Text -> Maybe Word64
releaseNumber :: Text -> Maybe Word64
releaseNumber Text
raw = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Text -> Bool
validNumber Text
raw Bool -> Bool -> Bool
&& Text -> Int -> Ordering
T.compareLength Text
raw Int
20 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
/= Ordering
GT)
    number <- Text -> Maybe Integer
forall a. Integral a => Text -> Maybe a
readDecimalText Text
raw :: Maybe Integer
    guard (number <= toInteger (maxBound :: Word64))
    pure (fromInteger number)

validNumber :: Text -> Bool
validNumber :: Text -> Bool
validNumber Text
raw =
    Bool -> Bool
not (Text -> Bool
T.null Text
raw)
        Bool -> Bool -> Bool
&& (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit Text
raw
        Bool -> Bool -> Bool
&& (Text -> Int -> Ordering
T.compareLength Text
raw Int
1 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
EQ Bool -> Bool -> Bool
|| Bool -> Bool
not (Text -> Text -> Bool
T.isPrefixOf Text
"0" Text
raw))

identifier :: Text -> Maybe Identifier
identifier :: Text -> Maybe Identifier
identifier Text
raw = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Text -> Bool
validIdentifier Text
raw)
    if (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit Text
raw
        then ByteString -> Identifier
Numeric (Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 Text
raw) Identifier -> Maybe () -> Maybe Identifier
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 -> Bool
validNumber Text
raw)
        else Identifier -> Maybe Identifier
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Identifier
Alphanumeric (Text -> Text
T.copy Text
raw))

buildIdentifiers :: Text -> Maybe Text
buildIdentifiers :: Text -> Maybe Text
buildIdentifiers Text
raw = Text -> Text
T.copy Text
raw 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 -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Text -> Bool
validIdentifier (HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"." Text
raw))

validIdentifier :: Text -> Bool
validIdentifier :: Text -> Bool
validIdentifier Text
raw = Bool -> Bool
not (Text -> Bool
T.null Text
raw) Bool -> Bool -> Bool
&& (Char -> Bool) -> Text -> Bool
T.all (\Char
c -> Char -> Bool
isAsciiAlphaNum Char
c Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-') Text
raw

comparePrerelease :: [Identifier] -> [Identifier] -> Ordering
comparePrerelease :: [Identifier] -> [Identifier] -> Ordering
comparePrerelease [] [] = Ordering
EQ
comparePrerelease [] [Identifier]
_ = Ordering
GT
comparePrerelease [Identifier]
_ [] = Ordering
LT
comparePrerelease [Identifier]
a [Identifier]
b = [Identifier] -> [Identifier] -> Ordering
forall a. Ord a => a -> a -> Ordering
compare [Identifier]
a [Identifier]
b

-- | Render native spelling, including build metadata excluded from ordering.
renderCargo :: CargoKey -> Text
renderCargo :: CargoKey -> Text
renderCargo key :: CargoKey
key@(CargoKey Word64
_ Word64
_ Word64
_ [Identifier]
_ Maybe Text
build) = CargoKey -> Text
cargoUniquenessKey CargoKey
key Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" (Text
"+" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) Maybe Text
build

-- | Whether the version has no prerelease suffix.
isCargoStable :: CargoKey -> Bool
isCargoStable :: CargoKey -> Bool
isCargoStable (CargoKey Word64
_ Word64
_ Word64
_ [Identifier]
pre Maybe Text
_) = [Identifier] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Identifier]
pre

-- | The native uniqueness spelling, excluding build metadata and retaining prerelease identity.
cargoUniquenessKey :: CargoKey -> Text
cargoUniquenessKey :: CargoKey -> Text
cargoUniquenessKey (CargoKey Word64
major Word64
minor Word64
patch [Identifier]
pre Maybe Text
_) =
    Text -> [Text] -> Text
T.intercalate Text
"." ((Word64 -> Text) -> [Word64] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Word64 -> Text
forall b a. (Show a, IsString b) => a -> b
show [Word64
major, Word64
minor, Word64
patch])
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> if [Identifier] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Identifier]
pre then Text
"" else Text
"-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
T.intercalate Text
"." ((Identifier -> Text) -> [Identifier] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Identifier -> Text
renderIdentifier [Identifier]
pre)

renderIdentifier :: Identifier -> Text
renderIdentifier :: Identifier -> Text
renderIdentifier = \case
    Numeric ByteString
raw -> ByteString -> Text
forall a b. ConvertUtf8 a b => b -> a
decodeUtf8 ByteString
raw
    Alphanumeric Text
raw -> Text
raw