@@ -89,6 +89,7 @@ module Pantry.Types
8989 , toCabalStringMap
9090 , unCabalStringMap
9191 , parsePackageIdentifierRevision
92+ , parseRawPackageLocationImmutables
9293 , Mismatch (.. )
9394 , PantryException (.. )
9495 , FuzzyResults (.. )
@@ -144,16 +145,18 @@ import Data.Aeson.Types
144145 , toJSONKeyText , withObject , withText
145146 )
146147import Data.Aeson.WarningParser
147- ( WarningParser , WithJSONWarnings , (..:) , (..:?) , (..!=)
148- , (.:) , (...:?) , jsonSubWarnings , jsonSubWarningsT
149- , noJSONWarnings , tellJSONField , withObjectWarnings
148+ ( JSONWarning (.. ), WarningParser , WithJSONWarnings (.. )
149+ , (..:) , (..:?) , (..!=) , (.:) , (...:?) , jsonSubWarnings
150+ , jsonSubWarningsT , noJSONWarnings , tellJSONField
151+ , withObjectWarnings
150152 )
151153import Data.ByteString.Builder
152154 ( byteString , toLazyByteString , wordDec )
153155import qualified Data.Conduit.Tar as Tar
154156import qualified Data.List.NonEmpty as NE
155157import qualified Data.Map.Strict as Map ( mapKeysMonotonic )
156158import Data.Text.Read ( decimal )
159+ import Data.Yaml ( decodeEither' )
157160import Distribution.CabalSpecVersion ( cabalSpecLatest )
158161#if MIN_VERSION_Cabal(3,4,0)
159162import Distribution.CabalSpecVersion ( cabalSpecToVersionDigits )
@@ -482,6 +485,21 @@ instance Pretty RawPackageLocationImmutable where
482485 ]
483486 ]
484487
488+ -- | Parse, 'Unresolved', one or more 'RawPackageLocationImmutable' from a valid
489+ -- YAML value. Alternatively, yields an exception if the given text cannot be
490+ -- decoded as a YAML value or it is decoded but with warnings.
491+ --
492+ -- @since 0.10.1
493+ parseRawPackageLocationImmutables ::
494+ Text
495+ -- ^ A YAML value.
496+ -> Either PantryException (Unresolved (NonEmpty RawPackageLocationImmutable ))
497+ parseRawPackageLocationImmutables t = case decodeEither' $ encodeUtf8 t of
498+ Left err -> Left $ RawPackageLocationImmutableParseFail t (SomeException err)
499+ Right (WithJSONWarnings unresolved warnings) -> case warnings of
500+ [] -> Right unresolved
501+ _ -> Left $ RawPackageLocationImmutableParseWarnings t warnings
502+
485503-- | Location for remote packages or archives assumed to be immutable.
486504--
487505-- @since 0.1.0.0
@@ -1069,6 +1087,8 @@ data Mismatch a = Mismatch
10691087-- @since 0.1.0.0
10701088data PantryException
10711089 = PackageIdentifierRevisionParseFail ! Text
1090+ | RawPackageLocationImmutableParseFail ! Text ! SomeException
1091+ | RawPackageLocationImmutableParseWarnings ! Text ! [JSONWarning ]
10721092 | InvalidCabalFile
10731093 ! (Either RawPackageLocationImmutable (Path Abs File ))
10741094 ! (Maybe Version )
@@ -1153,6 +1173,24 @@ instance Display PantryException where
11531173 " Error: [S-360]\n "
11541174 <> " Invalid package identifier (with optional revision): "
11551175 <> display text
1176+ display (RawPackageLocationImmutableParseFail text err) =
1177+ " Error: [S-925]\n "
1178+ <> " Invalid raw immutable package location: "
1179+ <> display text
1180+ <> " \n\n "
1181+ <> " The error encountered was:\n\n "
1182+ <> fromString (displayException err)
1183+ display (RawPackageLocationImmutableParseWarnings text warnings) =
1184+ " Error: [S-775]\n "
1185+ <> " Invalid raw immutable package location: "
1186+ <> display text
1187+ <> " \n\n "
1188+ <> " The warnings encountered were:\n\n "
1189+ <> fold
1190+ ( intersperse
1191+ " \n "
1192+ (map (\ warning -> " - " <> display warning) warnings)
1193+ )
11561194 display (InvalidCabalFile loc mversion errs warnings) =
11571195 " Error: [S-242]\n "
11581196 <> " Unable to parse cabal file from package "
@@ -1509,6 +1547,26 @@ instance Pretty PantryException where
15091547 [ flow " Invalid package identifier (with optional revision):"
15101548 , fromString $ T. unpack text
15111549 ]
1550+ pretty (RawPackageLocationImmutableParseFail text err) =
1551+ " [S-925]"
1552+ <> line
1553+ <> fillSep
1554+ [ flow " Invalid raw immutable package location:"
1555+ , fromString $ T. unpack text <> " ."
1556+ , flow " The error encountered was:"
1557+ ]
1558+ <> blankLine
1559+ <> string (displayException err)
1560+ pretty (RawPackageLocationImmutableParseWarnings text warnings) =
1561+ " [S-775]"
1562+ <> line
1563+ <> fillSep
1564+ [ flow " Invalid raw immutable package location:"
1565+ , fromString $ T. unpack text <> " ."
1566+ , flow " The warnings encountered were:"
1567+ ]
1568+ <> line
1569+ <> bulletedList (map (fromString . show ) warnings)
15121570 pretty (InvalidCabalFile loc mversion errs warnings) =
15131571 " [S-242]"
15141572 <> line
0 commit comments