module Ecluse.Core.Registry.RubyGems.Wire (
GemRowError (..),
decodeInfoRow,
infoRowIdentity,
infoRowVersion,
parseGemRequirement,
) where
import Control.Monad (foldM)
import Data.ByteString qualified as BS
import Data.Char (isAsciiLower, isAsciiUpper, 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 Ecluse.Core.Ecosystem (Ecosystem (RubyGems))
import Ecluse.Core.Package (HashAlg (SHA256), PackageName, mkHash)
import Ecluse.Core.Package.Entry (EntryKey)
import Ecluse.Core.Registry (ParseError (ParseError))
import Ecluse.Core.Registry.RubyGems.Document (GemDependency (..), GemRequirement, GemRow (..))
import Ecluse.Core.Registry.RubyGems.Project (GemCoordinate, mkCoordinate, mkGemPlatform, projectName, projectVersion)
import Ecluse.Core.Strict (strictElements)
import Ecluse.Core.Text.Iso8601 (readIso8601Utc)
import Ecluse.Core.Version (Version, mkVersion)
import Ecluse.Core.Version.Gem (isGemRequirementOperator)
data GemRowError
=
GemRowMalformed Text
|
GemRowUnsupported Text
deriving stock (GemRowError -> GemRowError -> Bool
(GemRowError -> GemRowError -> Bool)
-> (GemRowError -> GemRowError -> Bool) -> Eq GemRowError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GemRowError -> GemRowError -> Bool
== :: GemRowError -> GemRowError -> Bool
$c/= :: GemRowError -> GemRowError -> Bool
/= :: GemRowError -> GemRowError -> Bool
Eq, Int -> GemRowError -> ShowS
[GemRowError] -> ShowS
GemRowError -> String
(Int -> GemRowError -> ShowS)
-> (GemRowError -> String)
-> ([GemRowError] -> ShowS)
-> Show GemRowError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GemRowError -> ShowS
showsPrec :: Int -> GemRowError -> ShowS
$cshow :: GemRowError -> String
show :: GemRowError -> String
$cshowList :: [GemRowError] -> ShowS
showList :: [GemRowError] -> ShowS
Show)
infoRowIdentity :: PackageName -> ByteString -> Maybe GemCoordinate
infoRowIdentity :: PackageName -> ByteString -> Maybe GemCoordinate
infoRowIdentity PackageName
name ByteString
bytes = do
text <- Either UnicodeException Text -> Maybe Text
forall l r. Either l r -> Maybe r
rightToMaybe (ByteString -> Either UnicodeException Text
TE.decodeUtf8' (ByteString -> ByteString
identityPrefix ByteString
bytes))
rightToMaybe (parseIdentity name text)
infoRowVersion :: ByteString -> Maybe Version
infoRowVersion :: ByteString -> Maybe Version
infoRowVersion ByteString
bytes = do
raw <- Either UnicodeException Text -> Maybe Text
forall l r. Either l r -> Maybe r
rightToMaybe (ByteString -> Either UnicodeException Text
TE.decodeUtf8' ((Word8 -> Bool) -> ByteString -> ByteString
BS.takeWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
45) (ByteString -> ByteString
identityPrefix ByteString
bytes)))
rightToMaybe (projectVersion raw)
identityPrefix :: ByteString -> ByteString
identityPrefix :: ByteString -> ByteString
identityPrefix = (Word8 -> Bool) -> ByteString -> ByteString
BS.takeWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
32)
decodeInfoRow :: PackageName -> EntryKey -> ByteString -> Either GemRowError GemRow
decodeInfoRow :: PackageName -> EntryKey -> ByteString -> Either GemRowError GemRow
decodeInfoRow PackageName
name EntryKey
key ByteString
bytes = do
text <- (UnicodeException -> GemRowError)
-> Either UnicodeException Text -> Either GemRowError 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 (GemRowError -> UnicodeException -> GemRowError
forall a b. a -> b -> a
const (Text -> GemRowError
GemRowMalformed Text
"invalid UTF-8 in RubyGems info row")) (ByteString -> Either UnicodeException Text
TE.decodeUtf8' ByteString
bytes)
let (nativeIdentity, afterIdentity) = T.breakOn " " text
when (T.null afterIdentity) (malformed "missing RubyGems row separator")
coordinate <- first parseFailure (parseIdentity name nativeIdentity)
let (dependencyText, afterPipe) = T.breakOn "|" (T.drop 1 afterIdentity)
when (T.null afterPipe) (malformed "missing RubyGems metadata separator")
extras <- parseFields (T.drop 1 afterPipe)
when (Map.member "platform" extras) (Left (GemRowUnsupported "content-addressed RubyGems artifacts require newer clients"))
checksumText <- maybeToRight (GemRowMalformed "missing RubyGems SHA256") (Map.lookup "checksum" extras)
checksum <- first GemRowMalformed (mkHash SHA256 checksumText)
dependencies <- if T.null dependencyText then Right [] else traverse parseDependency (T.splitOn "," dependencyText)
when (Set.size (Set.fromList (map gdName dependencies)) /= length dependencies) (malformed "duplicate RubyGems dependency")
requiredRuby <- maybe (Right (">= 0" :| [])) parseGemRequirement (Map.lookup "ruby" extras)
requiredRubyGems <- maybe (Right (">= 0" :| [])) parseGemRequirement (Map.lookup "rubygems" extras)
let !published = Maybe UTCTime -> Maybe UTCTime
forall a. NFData a => a -> a
force (Text -> Map Text Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
"created_at" Map Text Text
extras Maybe Text -> (Text -> Maybe UTCTime) -> Maybe UTCTime
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 UTCTime
readIso8601Utc)
pure GemRow{grEntryKey = key, grCoordinate = coordinate, grDependencies = strictElements dependencies, grRequiredRuby = requiredRuby, grRequiredRubyGems = requiredRubyGems, grChecksum = checksum, grPublishedAt = published, grExtras = extras, grSource = BS.copy bytes}
where
parseDependency :: Text -> Either GemRowError GemDependency
parseDependency Text
raw = do
let (Text
dependencyName, Text
requirement) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
":" Text
raw
Bool -> Either GemRowError () -> Either GemRowError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text -> Bool
T.null Text
requirement) (Text -> Either GemRowError ()
forall a. Text -> Either GemRowError a
malformed Text
"missing RubyGems dependency requirement")
parsedName <- (ParseError -> GemRowError)
-> Either ParseError PackageName -> Either GemRowError PackageName
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ParseError -> GemRowError
parseFailure (Text -> Either ParseError PackageName
projectName Text
dependencyName)
GemDependency parsedName <$> parseGemRequirement (T.drop 1 requirement)
parseGemRequirement :: Text -> Either GemRowError GemRequirement
parseGemRequirement :: Text -> Either GemRowError (NonEmpty Text)
parseGemRequirement Text
raw = do
clauses <- GemRowError
-> Maybe (NonEmpty Text) -> Either GemRowError (NonEmpty Text)
forall l r. l -> Maybe r -> Either l r
maybeToRight (Text -> GemRowError
GemRowMalformed Text
"empty RubyGems requirement") ([Text] -> Maybe (NonEmpty Text)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty (HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"&" Text
raw))
strictElements <$> traverse parseClause clauses
where
parseClause :: Text -> Either GemRowError Text
parseClause Text
clause = do
let stripped :: Text
stripped = Text -> Text
T.strip Text
clause
(Text
operator, Text
rest) = (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
stripped
version :: Text
version = Text -> Text
T.strip Text
rest
Bool -> Either GemRowError () -> Either GemRowError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Text -> Bool
isGemRequirementOperator Text
operator Bool -> Bool -> Bool
&& Bool -> Bool
not (Text -> Bool
T.null Text
version) Bool -> Bool -> Bool
&& Either ParseError Version -> Bool
forall a b. Either a b -> Bool
isRight (Text -> Either ParseError Version
projectVersion Text
version)) (Text -> Either GemRowError ()
forall a. Text -> Either GemRowError a
malformed Text
"invalid RubyGems requirement")
Text -> Either GemRowError Text
forall a. a -> Either GemRowError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Text
T.copy Text
stripped)
parseIdentity :: PackageName -> Text -> Either ParseError GemCoordinate
parseIdentity :: PackageName -> Text -> Either ParseError GemCoordinate
parseIdentity PackageName
name Text
nativeIdentity = do
let (Text
version, Text
suffix) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
"-" Text
nativeIdentity
platform <- Text -> Either ParseError GemPlatform
mkGemPlatform (if Text -> Bool
T.null Text
suffix then Text
"ruby" else Int -> Text -> Text
T.drop Int
1 Text
suffix)
mkCoordinate name (mkVersion RubyGems version) platform
parseFields :: Text -> Either GemRowError (Map Text Text)
parseFields :: Text -> Either GemRowError (Map Text Text)
parseFields Text
raw = (Map Text Text -> Text -> Either GemRowError (Map Text Text))
-> Map Text Text -> [Text] -> Either GemRowError (Map Text Text)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Map Text Text -> Text -> Either GemRowError (Map Text Text)
insert Map Text Text
forall k a. Map k a
Map.empty (HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"," Text
raw)
where
insert :: Map Text Text -> Text -> Either GemRowError (Map Text Text)
insert Map Text Text
fields Text
field = do
let (Text
key, Text
separated) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
":" Text
field
Bool -> Either GemRowError () -> Either GemRowError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Bool -> Bool
not (Text -> Bool
T.null Text
key) Bool -> Bool -> Bool
&& (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
asciiField Text
key Bool -> Bool -> Bool
&& Bool -> Bool
not (Text -> Bool
T.null Text
separated) Bool -> Bool -> Bool
&& Bool -> Bool
not (Text -> Map Text Text -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member Text
key Map Text Text
fields)) (Text -> Either GemRowError ()
forall a. Text -> Either GemRowError a
malformed Text
"invalid or duplicate RubyGems metadata field")
Map Text Text -> Either GemRowError (Map Text Text)
forall a. a -> Either GemRowError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Text -> Map Text Text -> Map Text Text
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Text -> Text
T.copy Text
key) (Text -> Text
T.copy (Int -> Text -> Text
T.drop Int
1 Text
separated)) Map Text Text
fields)
asciiField :: Char -> Bool
asciiField Char
c = Char -> Bool
isAsciiLower Char
c Bool -> Bool -> Bool
|| Char -> Bool
isAsciiUpper Char
c Bool -> Bool -> Bool
|| Char -> Bool
isDigit Char
c Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'_'
parseFailure :: ParseError -> GemRowError
parseFailure :: ParseError -> GemRowError
parseFailure (ParseError Text
detail) = Text -> GemRowError
GemRowMalformed Text
detail
malformed :: Text -> Either GemRowError a
malformed :: forall a. Text -> Either GemRowError a
malformed = GemRowError -> Either GemRowError a
forall a b. a -> Either a b
Left (GemRowError -> Either GemRowError a)
-> (Text -> GemRowError) -> Text -> Either GemRowError a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> GemRowError
GemRowMalformed