@@ -9,8 +9,18 @@
9module Data.Yaml
9module Data.Yaml
10 ( -- * Types
10 ( -- * Types
11 Value (..)
11 Value (..)
13+ -- * Constructors and accessors
14 -- * Classes
24 -- * Classes
15 , ToJSON (..)
25 , ToJSON (..)
16 , FromJSON (..)
26 , FromJSON (..)
@@ -22,8 +32,11 @@ module Data.Yaml
24import qualified Text.Libyaml as Y
34import qualified Text.Libyaml as Y
25−import Data.Aeson (Value (..), ToJSON (..), FromJSON (..), object)
36+ ( Value (..), ToJSON (..), FromJSON (..), object
37+ , (.=) , (.:) , (.:?) , (.!=)
26−import Data.Aeson.Types (Pair, parseMaybe)
39+import Data.Aeson.Types (Pair, parseMaybe, parseEither, Parser)
27import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile)
40import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile)
28import Data.ByteString (ByteString)
41import Data.ByteString (ByteString)
29import qualified Data.ByteString.Char8 as S8
42import qualified Data.ByteString.Char8 as S8
@@ -109,16 +122,16 @@ instance MonadTrans PErrorT where
109instance MonadIO m => MonadIO (PErrorT m) where
122instance MonadIO m => MonadIO (PErrorT m) where
110 liftIO = lift . liftIO
123 liftIO = lift . liftIO
112−type Parser = StateT (Map.Map String Value) IO
125+type Parse = StateT (Map.Map String Value) IO
114−requireEvent :: Event -> C.Sink Event Parser ()
127+requireEvent :: Event -> C.Sink Event Parse ()
115requireEvent e = do
128requireEvent e = do
116 f <- CL.head
129 f <- CL.head
117 if f == Just e
130 if f == Just e
118 then return ()
131 then return ()
119 else liftIO $ throwIO $ UnexpectedEvent f $ Just e
132 else liftIO $ throwIO $ UnexpectedEvent f $ Just e
121−parse :: C.Sink Event Parser Value
134+parse :: C.Sink Event Parse Value
122parse = do
135parse = do
123 requireEvent EventStreamStart
136 requireEvent EventStreamStart
124 requireEvent EventDocumentStart
137 requireEvent EventDocumentStart
@@ -128,7 +141,7 @@ parse = do
128 return res
141 return res
130parseScalar :: ByteString -> Anchor
143parseScalar :: ByteString -> Anchor
131− -> C.Sink Event Parser Text
144+ -> C.Sink Event Parse Text
132parseScalar v a = do
145parseScalar v a = do
133 let res = decodeUtf8With lenientDecode v
146 let res = decodeUtf8With lenientDecode v
134 case a of
147 case a of
@@ -137,7 +150,7 @@ parseScalar v a = do
137 lift $ modify (Map.insert an $ String res)
150 lift $ modify (Map.insert an $ String res)
138 return res
151 return res
140−parseO :: C.Sink Event Parser Value
153+parseO :: C.Sink Event Parse Value
141parseO = do
154parseO = do
142 me <- CL.head
155 me <- CL.head
143 case me of
156 case me of
@@ -153,7 +166,7 @@ parseO = do
154parseS :: Y.Anchor
167parseS :: Y.Anchor
155 -> ([Value] -> [Value])
168 -> ([Value] -> [Value])
156− -> C.Sink Event Parser Value
169+ -> C.Sink Event Parse Value
157parseS a front = do
170parseS a front = do
158 me <- CL.peek
171 me <- CL.peek
159 case me of
172 case me of
@@ -171,7 +184,7 @@ parseS a front = do
172parseM :: Y.Anchor
185parseM :: Y.Anchor
173 -> M.HashMap Text Value
186 -> M.HashMap Text Value
174− -> C.Sink Event Parser Value
187+ -> C.Sink Event Parse Value
175parseM a front = do
188parseM a front = do
176 me <- CL.peek
189 me <- CL.peek
177 case me of
190 case me of
@@ -212,7 +225,7 @@ decodeFile :: FromJSON a
212decodeFile fp = decodeHelper (Y.decodeFile fp) >>= either throwIO return
225decodeFile fp = decodeHelper (Y.decodeFile fp) >>= either throwIO return
214decodeHelper :: FromJSON a
227decodeHelper :: FromJSON a
215− => C.Source Parser Y.Event
228+ => C.Source Parse Y.Event
216 -> IO (Either ParseException (Maybe a))
229 -> IO (Either ParseException (Maybe a))
217decodeHelper src = do
230decodeHelper src = do
218 x <- try $ flip evalStateT Map.empty $ C.runResourceT $ src C.$$ parse
231 x <- try $ flip evalStateT Map.empty $ C.runResourceT $ src C.$$ parse
@@ -225,3 +238,6 @@ decodeHelper src = do
226array :: [Value] -> Value
239array :: [Value] -> Value
227array = Array . V.fromList
240array = Array . V.fromList
242+parseMonad :: Monad m => (a -> Parser b) -> a -> m b
243+parseMonad p = either fail return . parseEither p