@@ -9,8 +9,18 @@
99module Data.Yaml
1010 ( -- * Types
1111 Value (..)
12+ , Parse
13+ -- * Constructors and accessors
1214 , object
1315 , array
16+ , (.=)
17+ , (.:)
18+ , (.:?)
19+ , (.!=)
20+ -- * Parsing
21+ , parseMonad
22+ , parseEither
23+ , parseMaybe
1424 -- * Classes
1525 , ToJSON (..)
1626 , FromJSON (..)
@@ -22,8 +32,11 @@ module Data.Yaml
2232 ) where
2333
2434import qualified Text.Libyaml as Y
25−import Data.Aeson (Value (..), ToJSON (..), FromJSON (..), object)
26−import Data.Aeson.Types (Pair, parseMaybe)
35+import Data.Aeson
36+ ( Value (..), ToJSON (..), FromJSON (..), object
37+ , (.=) , (.:) , (.:?) , (.!=)
38+ )
39+import Data.Aeson.Types (Pair, parseMaybe, parseEither, Parser)
2740import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile)
2841import Data.ByteString (ByteString)
2942import qualified Data.ByteString.Char8 as S8
@@ -109,16 +122,16 @@ instance MonadTrans PErrorT where
109122instance MonadIO m => MonadIO (PErrorT m) where
110123 liftIO = lift . liftIO
111124
112−type Parser = StateT (Map.Map String Value) IO
125+type Parse = StateT (Map.Map String Value) IO
113126
114−requireEvent :: Event -> C.Sink Event Parser ()
127+requireEvent :: Event -> C.Sink Event Parse ()
115128requireEvent e = do
116129 f <- CL.head
117130 if f == Just e
118131 then return ()
119132 else liftIO $ throwIO $ UnexpectedEvent f $ Just e
120133
121−parse :: C.Sink Event Parser Value
134+parse :: C.Sink Event Parse Value
122135parse = do
123136 requireEvent EventStreamStart
124137 requireEvent EventDocumentStart
@@ -128,7 +141,7 @@ parse = do
128141 return res
129142
130143parseScalar :: ByteString -> Anchor
131− -> C.Sink Event Parser Text
144+ -> C.Sink Event Parse Text
132145parseScalar v a = do
133146 let res = decodeUtf8With lenientDecode v
134147 case a of
@@ -137,7 +150,7 @@ parseScalar v a = do
137150 lift $ modify (Map.insert an $ String res)
138151 return res
139152
140−parseO :: C.Sink Event Parser Value
153+parseO :: C.Sink Event Parse Value
141154parseO = do
142155 me <- CL.head
143156 case me of
@@ -153,7 +166,7 @@ parseO = do
153166
154167parseS :: Y.Anchor
155168 -> ([Value] -> [Value])
156− -> C.Sink Event Parser Value
169+ -> C.Sink Event Parse Value
157170parseS a front = do
158171 me <- CL.peek
159172 case me of
@@ -171,7 +184,7 @@ parseS a front = do
171184
172185parseM :: Y.Anchor
173186 -> M.HashMap Text Value
174− -> C.Sink Event Parser Value
187+ -> C.Sink Event Parse Value
175188parseM a front = do
176189 me <- CL.peek
177190 case me of
@@ -212,7 +225,7 @@ decodeFile :: FromJSON a
212225decodeFile fp = decodeHelper (Y.decodeFile fp) >>= either throwIO return
213226
214227decodeHelper :: FromJSON a
215− => C.Source Parser Y.Event
228+ => C.Source Parse Y.Event
216229 -> IO (Either ParseException (Maybe a))
217230decodeHelper src = do
218231 x <- try $ flip evalStateT Map.empty $ C.runResourceT $ src C.$$ parse
@@ -225,3 +238,6 @@ decodeHelper src = do
225238
226239array :: [Value] -> Value
227240array = Array . V.fromList
241+
242+parseMonad :: Monad m => (a -> Parser b) -> a -> m b
243+parseMonad p = either fail return . parseEither p