module Ecluse.Core.Registry.RubyGems.Archive.Yaml (
YamlValue (..),
decodeYaml,
yamlMapping,
yamlSequence,
yamlText,
yamlNativeText,
yamlNativeString,
yamlField,
yamlTag,
) where
import Conduit
import Control.Monad.Except (throwError)
import Data.ByteString qualified as BS
import Data.Char (isAlpha, isDigit, isHexDigit, isSpace)
import Data.Map.Strict qualified as Map
import Data.Text qualified as T
import Data.Text.Encoding qualified as TE
import Data.Time.Calendar (fromGregorianValid)
import Text.Libyaml qualified as Y
import UnliftIO.Exception (try)
import Ecluse.Core.Registry (ParseError (..))
import Ecluse.Core.Registry.RubyGems.Marshal (inertNodeBudget)
import Ecluse.Core.Security.Limits (Limits (..))
data YamlValue = YamlScalar Y.Tag Y.Style Text | YamlSequence Y.Tag [YamlValue] | YamlMapping Y.Tag (Map Text YamlValue)
deriving stock (YamlValue -> YamlValue -> Bool
(YamlValue -> YamlValue -> Bool)
-> (YamlValue -> YamlValue -> Bool) -> Eq YamlValue
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: YamlValue -> YamlValue -> Bool
== :: YamlValue -> YamlValue -> Bool
$c/= :: YamlValue -> YamlValue -> Bool
/= :: YamlValue -> YamlValue -> Bool
Eq, Int -> YamlValue -> ShowS
[YamlValue] -> ShowS
YamlValue -> String
(Int -> YamlValue -> ShowS)
-> (YamlValue -> String)
-> ([YamlValue] -> ShowS)
-> Show YamlValue
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> YamlValue -> ShowS
showsPrec :: Int -> YamlValue -> ShowS
$cshow :: YamlValue -> String
show :: YamlValue -> String
$cshowList :: [YamlValue] -> ShowS
showList :: [YamlValue] -> ShowS
Show)
data YamlState = YamlState
{ YamlState -> Limits
ysLimits :: Limits
, YamlState -> Int
ysNodes :: Int
, YamlState -> Map String (YamlValue, Int, Int)
ysAliases :: Map Y.AnchorName (YamlValue, Int, Int)
}
type Decoder = StateT YamlState (ConduitT Y.Event Void (ExceptT ParseError (ResourceT IO)))
decodeYaml :: Limits -> (Int -> IO ()) -> ByteString -> IO (Either ParseError YamlValue)
decodeYaml :: Limits
-> (Int -> IO ()) -> ByteString -> IO (Either ParseError YamlValue)
decodeYaml Limits
limits Int -> IO ()
charge ByteString
bytes
| ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Limits -> Int
maxMetadataBytes Limits
limits = Either ParseError YamlValue -> IO (Either ParseError YamlValue)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ParseError -> Either ParseError YamlValue
forall a b. a -> Either a b
Left (Text -> ParseError
ParseError Text
"YAML exceeds its byte bound"))
| Bool
otherwise = do
Int -> IO ()
charge (Int
65536 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
64 Int -> Int -> Int
forall a. Num a => a -> a -> a
* ByteString -> Int
BS.length ByteString
bytes)
result <- IO (Either ParseError YamlValue)
-> IO (Either YamlException (Either ParseError YamlValue))
forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> m (Either e a)
try (ResourceT IO (Either ParseError YamlValue)
-> IO (Either ParseError YamlValue)
forall (m :: * -> *) a. MonadUnliftIO m => ResourceT m a -> m a
runResourceT (ResourceT IO (Either ParseError YamlValue)
-> IO (Either ParseError YamlValue))
-> (ConduitT () Void (ExceptT ParseError (ResourceT IO)) YamlValue
-> ResourceT IO (Either ParseError YamlValue))
-> ConduitT () Void (ExceptT ParseError (ResourceT IO)) YamlValue
-> IO (Either ParseError YamlValue)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExceptT ParseError (ResourceT IO) YamlValue
-> ResourceT IO (Either ParseError YamlValue)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT ParseError (ResourceT IO) YamlValue
-> ResourceT IO (Either ParseError YamlValue))
-> (ConduitT () Void (ExceptT ParseError (ResourceT IO)) YamlValue
-> ExceptT ParseError (ResourceT IO) YamlValue)
-> ConduitT () Void (ExceptT ParseError (ResourceT IO)) YamlValue
-> ResourceT IO (Either ParseError YamlValue)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConduitT () Void (ExceptT ParseError (ResourceT IO)) YamlValue
-> ExceptT ParseError (ResourceT IO) YamlValue
forall (m :: * -> *) r. Monad m => ConduitT () Void m r -> m r
runConduit (ConduitT () Void (ExceptT ParseError (ResourceT IO)) YamlValue
-> IO (Either ParseError YamlValue))
-> ConduitT () Void (ExceptT ParseError (ResourceT IO)) YamlValue
-> IO (Either ParseError YamlValue)
forall a b. (a -> b) -> a -> b
$ ByteString
-> ConduitM () Event (ExceptT ParseError (ResourceT IO)) ()
forall (m :: * -> *) i.
MonadResource m =>
ByteString -> ConduitM i Event m ()
Y.decode ByteString
bytes ConduitM () Event (ExceptT ParseError (ResourceT IO)) ()
-> ConduitT
Event Void (ExceptT ParseError (ResourceT IO)) YamlValue
-> ConduitT () Void (ExceptT ParseError (ResourceT IO)) YamlValue
forall (m :: * -> *) a b c r.
Monad m =>
ConduitT a b m () -> ConduitT b c m r -> ConduitT a c m r
.| StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
-> YamlState
-> ConduitT
Event Void (ExceptT ParseError (ResourceT IO)) YamlValue
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
document (Limits -> Int -> Map String (YamlValue, Int, Int) -> YamlState
YamlState Limits
limits Int
0 Map String (YamlValue, Int, Int)
forall k a. Map k a
Map.empty))
pure $ case result of
Left (YamlException
_ :: Y.YamlException) -> ParseError -> Either ParseError YamlValue
forall a b. a -> Either a b
Left (Text -> ParseError
ParseError Text
"malformed YAML stream")
Right Either ParseError YamlValue
decoded -> Either ParseError YamlValue
decoded
where
document :: StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
document = do
Event -> Decoder ()
expect Event
Y.EventStreamStart
Event -> Decoder ()
expect Event
Y.EventDocumentStart
value <- Decoder Event
next Decoder Event
-> (Event
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue)
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
forall a b.
StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
-> (a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
b)
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int
-> Event
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
node Int
0
expect Y.EventDocumentEnd
expect Y.EventStreamEnd
lift await >>= \case Maybe Event
Nothing -> () -> Decoder ()
forall a.
a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (); Just Event
_ -> Text -> Decoder ()
forall a. Text -> Decoder a
refuse Text
"YAML has trailing events"
pure value
node :: Int -> Y.Event -> Decoder YamlValue
node :: Int
-> Event
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
node Int
depth Event
event = do
limits <- (YamlState -> Limits)
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
Limits
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets YamlState -> Limits
ysLimits
when (depth > maxNestingDepth limits) (refuse "YAML nesting exceeds its bound")
consume 1
case event of
Y.EventAlias String
anchor -> do
references <- (YamlState -> Map String (YamlValue, Int, Int))
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
(Map String (YamlValue, Int, Int))
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets YamlState -> Map String (YamlValue, Int, Int)
ysAliases
case Map.lookup anchor references of
Maybe (YamlValue, Int, Int)
Nothing -> Text
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
forall a. Text -> Decoder a
refuse Text
"invalid or cyclic YAML alias"
Just (YamlValue
value, Int
cost, Int
height) -> do
Bool -> Decoder () -> Decoder ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
height Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Limits -> Int
maxNestingDepth Limits
limits) (Text -> Decoder ()
forall a. Text -> Decoder a
refuse Text
"YAML referenced depth exceeds its bound")
Int -> Decoder ()
consume Int
cost
YamlValue
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
forall a.
a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure YamlValue
value
Y.EventScalar ByteString
bytes Tag
tag Style
style Anchor
anchor -> do
Tag -> Decoder ()
checkTag Tag
tag
value <- (UnicodeException
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
Text)
-> (Text
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
Text)
-> Either UnicodeException Text
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
Text
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
Text
-> UnicodeException
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
Text
forall a b. a -> b -> a
const (Text
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
Text
forall a. Text -> Decoder a
refuse Text
"YAML scalar is not UTF-8")) Text
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
Text
forall a.
a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Either UnicodeException Text
TE.decodeUtf8' ByteString
bytes)
retain anchor 1 (YamlScalar tag style value)
Y.EventSequenceStart Tag
tag SequenceStyle
_ Anchor
anchor -> do
Tag -> Decoder ()
checkTag Tag
tag
before <- (YamlState -> Int)
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
Int
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets YamlState -> Int
ysNodes
values <- sequenceValues (depth + 1)
after <- gets ysNodes
retain anchor (after - before + 1) (YamlSequence tag values)
Y.EventMappingStart Tag
tag MappingStyle
_ Anchor
anchor -> do
Tag -> Decoder ()
checkTag Tag
tag
before <- (YamlState -> Int)
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
Int
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets YamlState -> Int
ysNodes
values <- mappingValues (depth + 1) Map.empty
after <- gets ysNodes
retain anchor (after - before + 1) (YamlMapping tag values)
Event
_ -> Text
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
forall a. Text -> Decoder a
refuse Text
"unexpected YAML framing event"
sequenceValues :: Int -> Decoder [YamlValue]
sequenceValues :: Int -> Decoder [YamlValue]
sequenceValues Int
depth =
Decoder Event
next Decoder Event
-> (Event -> Decoder [YamlValue]) -> Decoder [YamlValue]
forall a b.
StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
-> (a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
b)
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Event
Y.EventSequenceEnd -> [YamlValue] -> Decoder [YamlValue]
forall a.
a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
Event
event -> (:) (YamlValue -> [YamlValue] -> [YamlValue])
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
([YamlValue] -> [YamlValue])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int
-> Event
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
node Int
depth Event
event StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
([YamlValue] -> [YamlValue])
-> Decoder [YamlValue] -> Decoder [YamlValue]
forall a b.
StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
(a -> b)
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Decoder [YamlValue]
sequenceValues Int
depth
mappingValues :: Int -> Map Text YamlValue -> Decoder (Map Text YamlValue)
mappingValues :: Int -> Map Text YamlValue -> Decoder (Map Text YamlValue)
mappingValues Int
depth Map Text YamlValue
held =
Decoder Event
next Decoder Event
-> (Event -> Decoder (Map Text YamlValue))
-> Decoder (Map Text YamlValue)
forall a b.
StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
-> (a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
b)
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Event
Y.EventMappingEnd -> Map Text YamlValue -> Decoder (Map Text YamlValue)
forall a.
a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Map Text YamlValue
held
Event
event -> do
keyValue <- Int
-> Event
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
node Int
depth Event
event
key <- checked (yamlText keyValue)
when (Map.member key held || key == "<<") (refuse "duplicate YAML key or merge key")
value <- next >>= node depth
mappingValues depth (Map.insert key value held)
retain :: Y.Anchor -> Int -> YamlValue -> Decoder YamlValue
retain :: Anchor
-> Int
-> YamlValue
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
retain Anchor
anchor Int
cost YamlValue
value = do
Anchor -> (String -> Decoder ()) -> Decoder ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ Anchor
anchor ((String -> Decoder ()) -> Decoder ())
-> (String -> Decoder ()) -> Decoder ()
forall a b. (a -> b) -> a -> b
$ \String
name -> do
references <- (YamlState -> Map String (YamlValue, Int, Int))
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
(Map String (YamlValue, Int, Int))
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets YamlState -> Map String (YamlValue, Int, Int)
ysAliases
when (Map.member name references) (refuse "duplicate YAML anchor")
modify' (\YamlState
cursor -> YamlState
cursor{ysAliases = Map.insert name (value, cost, valueDepth value) references})
YamlValue
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlValue
forall a.
a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure YamlValue
value
valueDepth :: YamlValue -> Int
valueDepth :: YamlValue -> Int
valueDepth = \case
YamlScalar{} -> Int
0
YamlSequence Tag
_ [YamlValue]
values -> [YamlValue] -> Int
descendants [YamlValue]
values
YamlMapping Tag
_ Map Text YamlValue
fields -> [YamlValue] -> Int
descendants (Map Text YamlValue -> [YamlValue]
forall k a. Map k a -> [a]
Map.elems Map Text YamlValue
fields)
where
descendants :: [YamlValue] -> Int
descendants [YamlValue]
values = (Int -> Int -> Int) -> Int -> [Int] -> Int
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 ((YamlValue -> Int) -> [YamlValue] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ((Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+) (Int -> Int) -> (YamlValue -> Int) -> YamlValue -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. YamlValue -> Int
valueDepth) [YamlValue]
values)
consume :: Int -> Decoder ()
consume :: Int -> Decoder ()
consume Int
amount = do
cursor <- StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
YamlState
forall s (m :: * -> *). MonadState s m => m s
get
when (amount > inertNodeBudget (ysLimits cursor) - ysNodes cursor) (refuse "YAML node or alias work exceeds its bound")
modify' (\YamlState
current -> YamlState
current{ysNodes = ysNodes current + amount})
checkTag :: Y.Tag -> Decoder ()
checkTag :: Tag -> Decoder ()
checkTag = \case
Y.UriTag String
tag | String -> Text
forall a. ToText a => a -> Text
toText String
tag Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text
"!ruby/object:Gem::Specification", Text
"!ruby/object:Gem::Version", Text
"!ruby/object:Gem::Requirement", Text
"!ruby/object:Gem::Dependency", Text
"!ruby/object:Gem::Platform", Text
"tag:yaml.org,2002:timestamp", Text
"!ruby/symbol"] -> () -> Decoder ()
forall a.
a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Y.UriTag String
_ -> Text -> Decoder ()
forall a. Text -> Decoder a
refuse Text
"unsupported YAML tag"
Tag
Y.SetTag -> Text -> Decoder ()
forall a. Text -> Decoder a
refuse Text
"unsupported YAML set"
Tag
_ -> () -> Decoder ()
forall a.
a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
next :: Decoder Y.Event
next :: Decoder Event
next = ConduitT
Event Void (ExceptT ParseError (ResourceT IO)) (Maybe Event)
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
(Maybe Event)
forall (m :: * -> *) a. Monad m => m a -> StateT YamlState m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ConduitT
Event Void (ExceptT ParseError (ResourceT IO)) (Maybe Event)
forall (m :: * -> *) i o. Monad m => ConduitT i o m (Maybe i)
await StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
(Maybe Event)
-> (Maybe Event -> Decoder Event) -> Decoder Event
forall a b.
StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
-> (a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
b)
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Decoder Event
-> (Event -> Decoder Event) -> Maybe Event -> Decoder Event
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text -> Decoder Event
forall a. Text -> Decoder a
refuse Text
"truncated YAML document") Event -> Decoder Event
forall a.
a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
expect :: Y.Event -> Decoder ()
expect :: Event -> Decoder ()
expect Event
wanted = Decoder Event
next Decoder Event -> (Event -> Decoder ()) -> Decoder ()
forall a b.
StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
-> (a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
b)
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Event
actual -> Bool -> Decoder () -> Decoder ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Event
actual Event -> Event -> Bool
forall a. Eq a => a -> a -> Bool
== Event
wanted) (Text -> Decoder ()
forall a. Text -> Decoder a
refuse Text
"unexpected YAML framing")
checked :: Either ParseError a -> Decoder a
checked :: forall a. Either ParseError a -> Decoder a
checked = (ParseError -> Decoder a)
-> (a -> Decoder a) -> Either ParseError a -> Decoder a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (ConduitT Event Void (ExceptT ParseError (ResourceT IO)) a
-> Decoder a
forall (m :: * -> *) a. Monad m => m a -> StateT YamlState m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ConduitT Event Void (ExceptT ParseError (ResourceT IO)) a
-> Decoder a)
-> (ParseError
-> ConduitT Event Void (ExceptT ParseError (ResourceT IO)) a)
-> ParseError
-> Decoder a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExceptT ParseError (ResourceT IO) a
-> ConduitT Event Void (ExceptT ParseError (ResourceT IO)) a
forall (m :: * -> *) a. Monad m => m a -> ConduitT Event Void m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ExceptT ParseError (ResourceT IO) a
-> ConduitT Event Void (ExceptT ParseError (ResourceT IO)) a)
-> (ParseError -> ExceptT ParseError (ResourceT IO) a)
-> ParseError
-> ConduitT Event Void (ExceptT ParseError (ResourceT IO)) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> ExceptT ParseError (ResourceT IO) a
forall a. ParseError -> ExceptT ParseError (ResourceT IO) a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError) a -> Decoder a
forall a.
a
-> StateT
YamlState
(ConduitT Event Void (ExceptT ParseError (ResourceT IO)))
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
refuse :: Text -> Decoder a
refuse :: forall a. Text -> Decoder a
refuse = Either ParseError a -> Decoder a
forall a. Either ParseError a -> Decoder a
checked (Either ParseError a -> Decoder a)
-> (Text -> Either ParseError a) -> Text -> Decoder a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> Either ParseError a
forall a b. a -> Either a b
Left (ParseError -> Either ParseError a)
-> (Text -> ParseError) -> Text -> Either ParseError a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ParseError
ParseError
yamlMapping :: YamlValue -> Either ParseError (Map Text YamlValue)
yamlMapping :: YamlValue -> Either ParseError (Map Text YamlValue)
yamlMapping (YamlMapping Tag
_ Map Text YamlValue
values) = Map Text YamlValue -> Either ParseError (Map Text YamlValue)
forall a b. b -> Either a b
Right Map Text YamlValue
values
yamlMapping YamlValue
_ = ParseError -> Either ParseError (Map Text YamlValue)
forall a b. a -> Either a b
Left (Text -> ParseError
ParseError Text
"expected YAML mapping")
yamlSequence :: YamlValue -> Either ParseError [YamlValue]
yamlSequence :: YamlValue -> Either ParseError [YamlValue]
yamlSequence (YamlSequence Tag
_ [YamlValue]
values) = [YamlValue] -> Either ParseError [YamlValue]
forall a b. b -> Either a b
Right [YamlValue]
values
yamlSequence YamlValue
_ = ParseError -> Either ParseError [YamlValue]
forall a b. a -> Either a b
Left (Text -> ParseError
ParseError Text
"expected YAML sequence")
yamlText :: YamlValue -> Either ParseError Text
yamlText :: YamlValue -> Either ParseError Text
yamlText (YamlScalar Tag
_ Style
_ Text
value) = Text -> Either ParseError Text
forall a b. b -> Either a b
Right Text
value
yamlText YamlValue
_ = ParseError -> Either ParseError Text
forall a b. a -> Either a b
Left (Text -> ParseError
ParseError Text
"expected YAML scalar")
yamlNativeText :: YamlValue -> Either ParseError Text
yamlNativeText :: YamlValue -> Either ParseError Text
yamlNativeText YamlValue
value = YamlValue -> Either ParseError (Maybe Text)
yamlNativeString YamlValue
value Either ParseError (Maybe Text)
-> (Maybe Text -> Either ParseError Text) -> Either ParseError Text
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ParseError -> Maybe Text -> Either ParseError Text
forall l r. l -> Maybe r -> Either l r
maybeToRight ParseError
notNativeString
yamlNativeString :: YamlValue -> Either ParseError (Maybe Text)
yamlNativeString :: YamlValue -> Either ParseError (Maybe Text)
yamlNativeString = \case
YamlScalar Tag
tag Style
style Text
value
| Tag
tag Tag -> Tag -> Bool
forall a. Eq a => a -> a -> Bool
== Tag
Y.StrTag Bool -> Bool -> Bool
|| (Tag
tag Tag -> Tag -> Bool
forall a. Eq a => a -> a -> Bool
== Tag
Y.NoTag Bool -> Bool -> Bool
&& Style
style Style -> [Style] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Style
Y.SingleQuoted, Style
Y.DoubleQuoted, Style
Y.Literal, Style
Y.Folded]) -> Maybe Text -> Either ParseError (Maybe Text)
forall a b. b -> Either a b
Right (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
value)
| Tag
tag Tag -> Tag -> Bool
forall a. Eq a => a -> a -> Bool
== Tag
Y.FloatTag Bool -> Bool -> Bool
|| Tag
tag Tag -> Tag -> Bool
forall a. Eq a => a -> a -> Bool
== String -> Tag
Y.UriTag String
"!ruby/symbol" -> ParseError -> Either ParseError (Maybe Text)
forall a b. a -> Either a b
Left ParseError
notNativeString
| Bool
otherwise -> Text -> Either ParseError (Maybe Text)
plainNativeString Text
value
YamlValue
_ -> ParseError -> Either ParseError (Maybe Text)
forall a b. a -> Either a b
Left (Text -> ParseError
ParseError Text
"expected YAML scalar")
plainNativeString :: Text -> Either ParseError (Maybe Text)
plainNativeString :: Text -> Either ParseError (Maybe Text)
plainNativeString Text
value
| Text -> Bool
T.null Text
value = Maybe Text -> Either ParseError (Maybe Text)
forall a b. b -> Either a b
Right Maybe Text
forall a. Maybe a
Nothing
| Bool
wordLike Bool -> Bool -> Bool
|| (Char -> Bool) -> Text -> Bool
T.any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\n') Text
value = Either ParseError (Maybe Text)
keyword
| Bool
gregorianDate Bool -> Bool -> Bool
|| Text -> Text
T.toLower Text
value Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text
".inf", Text
"+.inf", Text
"-.inf", Text
".nan"] Bool -> Bool -> Bool
|| Text -> Bool
psychNumber Text
value = ParseError -> Either ParseError (Maybe Text)
forall a b. a -> Either a b
Left ParseError
notNativeString
| Bool
otherwise = Maybe Text -> Either ParseError (Maybe Text)
forall a b. b -> Either a b
Right (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
value)
where
lower :: Text
lower = Text -> Text
T.toLower Text
value
wordClass :: Char -> Bool
wordClass Char
c = Char -> Bool
isAlpha Char
c Bool -> Bool -> Bool
|| Char -> Bool
isSpace Char
c Bool -> Bool -> Bool
|| Char
c Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` (String
"_!@#$%^&*(){}<>|/\\~;=" :: String)
wordLike :: Bool
wordLike = case Text -> String
T.unpack (Int -> Text -> Text
T.take Int
2 Text
value) of
Char
c0 : String
rest -> Char -> Bool
wordClass Char
c0 Bool -> Bool -> Bool
|| (Char
c0 Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`notElem` (String
"0123456789.:-" :: String) Bool -> Bool -> Bool
&& (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Char -> Bool
wordClass String
rest)
[] -> Bool
False
keyword :: Either ParseError (Maybe Text)
keyword
| Text -> Int -> Ordering
T.compareLength Text
value Int
5 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT Bool -> Bool -> Bool
|| Int -> Text -> Text
T.take Int
1 Text
lower Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`notElem` [Text
"y", Text
"t", Text
"o", Text
"n", Text
"f", Text
"~"] = Maybe Text -> Either ParseError (Maybe Text)
forall a b. b -> Either a b
Right (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
value)
| Text
value Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"~" Bool -> Bool -> Bool
|| Text
lower Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"null" = Maybe Text -> Either ParseError (Maybe Text)
forall a b. b -> Either a b
Right Maybe Text
forall a. Maybe a
Nothing
| Text
lower Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text
"yes", Text
"true", Text
"on", Text
"no", Text
"false", Text
"off"] = ParseError -> Either ParseError (Maybe Text)
forall a b. a -> Either a b
Left ParseError
notNativeString
| Bool
otherwise = Maybe Text -> Either ParseError (Maybe Text)
forall a b. b -> Either a b
Right (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
value)
gregorianDate :: Bool
gregorianDate = case HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"-" Text
value of
[Text
year, Text
month, Text
day]
| Text -> Int -> Ordering
T.compareLength Text
year Int
4 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
EQ Bool -> Bool -> Bool
&& (Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\Text
part -> Bool -> Bool
not (Text -> Bool
T.null Text
part) Bool -> Bool -> Bool
&& (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit Text
part) [Text
year, Text
month, Text
day] Bool -> Bool -> Bool
&& Text -> Int -> Ordering
T.compareLength Text
month Int
2 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
/= Ordering
GT Bool -> Bool -> Bool
&& Text -> Int -> Ordering
T.compareLength Text
day Int
2 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
/= Ordering
GT ->
Maybe Day -> Bool
forall a. Maybe a -> Bool
isJust (Integer -> Int -> Int -> Maybe Day
fromGregorianValid (Text -> Integer
readDigits Text
year) (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Text -> Integer
readDigits Text
month)) (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Text -> Integer
readDigits Text
day)))
[Text]
_ -> Bool
False
readDigits :: Text -> Integer
readDigits = (Integer -> Char -> Integer) -> Integer -> Text -> Integer
forall a. (a -> Char -> a) -> a -> Text -> a
T.foldl' (\Integer
total Char
digit -> Integer
total Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
10 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
digit Int -> Int -> Int
forall a. Num a => a -> a -> a
- Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
'0')) Integer
0
psychNumber :: Text -> Bool
psychNumber :: Text -> Bool
psychNumber Text
signed = Bool
integer Bool -> Bool -> Bool
|| Bool
float
where
text :: Text
text = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
signed (Text -> Text -> Maybe Text
T.stripPrefix Text
"-" Text
signed Maybe Text -> Maybe Text -> Maybe Text
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Text -> Maybe Text
T.stripPrefix Text
"+" Text
signed)
separated :: Char -> Bool
separated = (Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Char
'_', Char
','])
radix :: Text -> (Char -> Bool) -> Bool
radix Text
prefix Char -> Bool
digit = case Text -> Text -> Maybe Text
T.stripPrefix Text
prefix Text
text of
Just Text
rest -> case Text -> Maybe (Char, Text)
T.uncons ((Char -> Bool) -> Text -> Text
T.dropWhile Char -> Bool
separated Text
rest) of
Just (Char
leading, Text
others) -> Char -> Bool
digit Char
leading Bool -> Bool -> Bool
&& (Char -> Bool) -> Text -> Bool
T.all (\Char
c -> Char -> Bool
digit Char
c Bool -> Bool -> Bool
|| Char -> Bool
separated Char
c) Text
others
Maybe (Char, Text)
Nothing -> Bool
False
Maybe Text
Nothing -> Bool
False
decimal :: Bool
decimal = case Text -> Maybe (Char, Text)
T.uncons Text
text of
Just (Char
'0', Text
rest) -> Text -> Bool
T.null Text
rest
Just (Char
leading, Text
rest) -> Char
leading Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Char
'1' .. Char
'9'] Bool -> Bool -> Bool
&& String -> Bool
grouped (Text -> String
T.unpack Text
rest)
Maybe (Char, Text)
Nothing -> Bool
False
grouped :: String -> Bool
grouped = \case
[] -> Bool
True
Char
c : String
cs | Char -> Bool
isDigit Char
c -> String -> Bool
grouped String
cs
Char
c : Char
d : String
cs | Char -> Bool
separated Char
c Bool -> Bool -> Bool
&& Char -> Bool
isDigit Char
d -> String -> Bool
grouped String
cs
String
_ -> Bool
False
integer :: Bool
integer = Text -> (Char -> Bool) -> Bool
radix Text
"0b" (Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Char
'0', Char
'1']) Bool -> Bool -> Bool
|| Text -> (Char -> Bool) -> Bool
radix Text
"0x" Char -> Bool
isHexDigit Bool -> Bool -> Bool
|| Text -> (Char -> Bool) -> Bool
radix Text
"0" (Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Char
'0' .. Char
'7']) Bool -> Bool -> Bool
|| Bool
decimal
float :: Bool
float = case HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
"." Text
text of
(Text
whole, Text
point)
| Just Text
after <- Text -> Text -> Maybe Text
T.stripPrefix Text
"." Text
point ->
let (Text
_, Text
scale) = (Char -> Bool) -> Text -> (Text, Text)
T.span Char -> Bool
isDigit Text
after
in (Text -> Bool
T.null Text
whole Bool -> Bool -> Bool
|| ((Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit (Int -> Text -> Text
T.take Int
1 Text
whole) Bool -> Bool -> Bool
&& (Char -> Bool) -> Text -> Bool
T.all (\Char
c -> Char -> Bool
isDigit Char
c Bool -> Bool -> Bool
|| Char -> Bool
separated Char
c) Text
whole))
Bool -> Bool -> Bool
&& Bool -> Bool
not (Text -> Bool
T.null Text
whole Bool -> Bool -> Bool
&& Text -> Bool
T.null Text
after)
Bool -> Bool -> Bool
&& (Text -> Bool
T.null Text
scale Bool -> Bool -> Bool
|| Text -> Bool
validScale Text
scale)
(Text, Text)
_ -> Bool
False
validScale :: Text -> Bool
validScale Text
scale = case Text -> String
T.unpack Text
scale of
Char
e : Char
sign : digits :: String
digits@(Char
_ : String
_) -> Char
e Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Char
'e', Char
'E'] Bool -> Bool -> Bool
&& Char
sign Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Char
'-', Char
'+'] Bool -> Bool -> Bool
&& (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isDigit String
digits
String
_ -> Bool
False
notNativeString :: ParseError
notNativeString :: ParseError
notNativeString = Text -> ParseError
ParseError Text
"YAML scalar does not load as a native string"
yamlField :: Text -> Map Text YamlValue -> Either ParseError YamlValue
yamlField :: Text -> Map Text YamlValue -> Either ParseError YamlValue
yamlField Text
key = ParseError -> Maybe YamlValue -> Either ParseError YamlValue
forall l r. l -> Maybe r -> Either l r
maybeToRight (Text -> ParseError
ParseError (Text
"missing YAML field: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
key)) (Maybe YamlValue -> Either ParseError YamlValue)
-> (Map Text YamlValue -> Maybe YamlValue)
-> Map Text YamlValue
-> Either ParseError YamlValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Map Text YamlValue -> Maybe YamlValue
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
key
yamlTag :: YamlValue -> Y.Tag
yamlTag :: YamlValue -> Tag
yamlTag (YamlScalar Tag
tag Style
_ Text
_) = Tag
tag
yamlTag (YamlSequence Tag
tag [YamlValue]
_) = Tag
tag
yamlTag (YamlMapping Tag
tag Map Text YamlValue
_) = Tag
tag