@@ -19,7 +19,7 @@ module Text.Libyaml
19 -- * Higher level functions
19 -- * Higher level functions
22− -- FIXME , encodeFile
22+ , encodeFile
23 , decodeFile
23 , decodeFile
@@ -33,9 +33,6 @@ import Foreign.Ptr
33import Foreign.ForeignPtr
33import Foreign.ForeignPtr
34import Foreign.Marshal.Alloc
34import Foreign.Marshal.Alloc
36−import Control.Applicative
37−import Control.Failure
39import Control.Exception (throwIO, Exception)
36import Control.Exception (throwIO, Exception)
40import Data.Typeable (Typeable)
37import Data.Typeable (Typeable)
@@ -213,9 +210,8 @@ makeString f a = do
213 cchar <- castPtr `fmap` f a
210 cchar <- castPtr `fmap` f a
214 peekCString cchar
211 peekCString cchar
216−parserParseOne :: MonadFailure YamlException m
213+parserParseOne :: Parser
218− -> IO (m Event)
214+ -> IO (Either YamlException Event)
219parserParseOne parser = withEventRaw $ \er -> do
215parserParseOne parser = withEventRaw $ \er -> do
220 res <- c_yaml_parser_parse parser er
216 res <- c_yaml_parser_parse parser er
@@ -225,9 +221,9 @@ parserParseOne parser = withEventRaw $ \er -> do
225 context <- makeString c_get_parser_error_context parser
221 context <- makeString c_get_parser_error_context parser
226 offset <- fromIntegral `fmap`
222 offset <- fromIntegral `fmap`
227 c_get_parser_error_offset parser
223 c_get_parser_error_offset parser
228− return $ failure $
224+ return $ Left $
229 YamlParserException problem context offset
225 YamlParserException problem context offset
230− else return `fmap` getEvent er
226+ else Right `fmap` getEvent er
231 c_yaml_event_delete er
227 c_yaml_event_delete er
232 return event
228 return event
@@ -296,19 +292,6 @@ getEvent er = do
296 YamlMappingStartEvent -> return EventMappingStart
292 YamlMappingStartEvent -> return EventMappingStart
297 YamlMappingEndEvent -> return EventMappingEnd
293 YamlMappingEndEvent -> return EventMappingEnd
299−data YAttempt v = YSuccess v | YFailure YamlException
300−instance Functor YAttempt where
302−instance Applicative YAttempt where
305−instance Monad YAttempt where
307− YSuccess v >>= f = f v
308− YFailure e >>= _ = YFailure e
309−instance Failure YamlException YAttempt where
312parserParse :: (a -> Event -> Either a a)
295parserParse :: (a -> Event -> Either a a)
314 -> Parser
297 -> Parser
@@ -316,8 +299,8 @@ parserParse :: (a -> Event -> Either a a)
316parserParse fold accum parser = do
299parserParse fold accum parser = do
317 event' <- parserParseOne parser
300 event' <- parserParseOne parser
318 case event' of
301 case event' of
319− YFailure e -> return $ Left e
302+ Left e -> return $ Left e
320− YSuccess event -> do
303+ Right event -> do
321 case fold accum event of
304 case fold accum event of
322 Left accum' -> return $ Right accum'
305 Left accum' -> return $ Right accum'
323 Right accum' -> parserParse fold accum' parser
306 Right accum' -> parserParse fold accum' parser
@@ -349,34 +332,34 @@ foreign import ccall unsafe "get_buffer_buff"
349foreign import ccall unsafe "get_buffer_used"
332foreign import ccall unsafe "get_buffer_used"
350 c_get_buffer_used :: Buffer -> IO CULong
333 c_get_buffer_used :: Buffer -> IO CULong
352−withBuffer :: MonadFailure YamlException m
335+withBuffer :: (Buffer -> IO (Either YamlException ()))
353− => (Buffer -> IO (YAttempt ()))
354− -> IO (m B.ByteString)
336+ -> IO (Either YamlException B.ByteString)
355withBuffer f = allocaBytes bufferSize $ \b -> do
337withBuffer f = allocaBytes bufferSize $ \b -> do
356 c_buffer_init b
338 c_buffer_init b
357 aRes <- f b
339 aRes <- f b
358 case aRes of
340 case aRes of
359− YFailure e -> return $ failure e
341+ Left e -> return $ Left e
360− YSuccess () -> do
342+ Right () -> do
361 ptr' <- c_get_buffer_buff b
343 ptr' <- c_get_buffer_buff b
362 len <- c_get_buffer_used b
344 len <- c_get_buffer_used b
363 fptr <- newForeignPtr_ $ castPtr ptr'
345 fptr <- newForeignPtr_ $ castPtr ptr'
364− return $ return $ B.fromForeignPtr fptr 0 $ fromIntegral len
346+ return $ Right $ B.fromForeignPtr fptr 0 $ fromIntegral len
366foreign import ccall unsafe "my_emitter_set_output"
348foreign import ccall unsafe "my_emitter_set_output"
367 c_my_emitter_set_output :: Emitter -> Buffer -> IO ()
349 c_my_emitter_set_output :: Emitter -> Buffer -> IO ()
369−withEmitter :: MonadFailure YamlException m
351+withEmitter :: (Emitter -> IO (Either YamlException ()))
370− => (Emitter -> IO (YAttempt ()))
371− -> IO (m B.ByteString)
352+ -> IO (Either YamlException B.ByteString)
372withEmitter f = allocaBytes emitterSize $ \e -> do
353withEmitter f = allocaBytes emitterSize $ \e -> do
373− res <- c_yaml_emitter_initialize e
354+ res <- c_yaml_emitter_initialize e
374− when (res == 0) $ throwIO YamlOutOfMemory
356+ then return $ Left YamlOutOfMemory
375− bs <- withBuffer $ \b -> do
358+ bs <- withBuffer $ \b -> do
376− c_my_emitter_set_output e b
359+ c_my_emitter_set_output e b
378− c_yaml_emitter_delete e
361+ c_yaml_emitter_delete e
379− return bs
362+ return bs
381foreign import ccall unsafe "yaml_emitter_emit"
364foreign import ccall unsafe "yaml_emitter_emit"
382 c_yaml_emitter_emit :: Emitter -> EventRaw -> IO CInt
365 c_yaml_emitter_emit :: Emitter -> EventRaw -> IO CInt
@@ -384,18 +367,20 @@ foreign import ccall unsafe "yaml_emitter_emit"
384foreign import ccall unsafe "get_emitter_error"
367foreign import ccall unsafe "get_emitter_error"
385 c_get_emitter_error :: Emitter -> IO (Ptr CUChar)
368 c_get_emitter_error :: Emitter -> IO (Ptr CUChar)
387−emitEvents :: MonadFailure YamlException m
370+emitEvents :: (a -> Maybe (Event, a))
388− => Emitter
372+ -> Emitter
389− -> [Event]
373+ -> IO (Either YamlException ())
390− -> IO (m ())
374+emitEvents unfold src emitter = do
391−emitEvents _ [] = return $ return ()
375+ case unfold src of
392−emitEvents emitter (e:rest) = do
376+ Nothing -> return $ Right ()
377+ Just (e, src') -> do
393− res <- toEventRaw e $ c_yaml_emitter_emit emitter
378+ res <- toEventRaw e $ c_yaml_emitter_emit emitter
394− if res == 0
379+ if res == 0
396− problem <- makeString c_get_emitter_error emitter
381+ problem <- makeString c_get_emitter_error emitter
397− return $ failure $ YamlEmitterException problem
382+ return $ Left $ YamlEmitterException problem
398− else emitEvents emitter rest
383+ else emitEvents unfold src' emitter
400foreign import ccall unsafe "yaml_stream_start_event_initialize"
385foreign import ccall unsafe "yaml_stream_start_event_initialize"
401 c_yaml_stream_start_event_initialize :: EventRaw -> CInt -> IO CInt
386 c_yaml_stream_start_event_initialize :: EventRaw -> CInt -> IO CInt
@@ -500,19 +485,29 @@ toEventRaw e f = withEventRaw $ \er -> do
500 c_yaml_mapping_end_event_initialize er
485 c_yaml_mapping_end_event_initialize er
501 EventAlias -> error "toEventRaw: EventAlias not supported"
486 EventAlias -> error "toEventRaw: EventAlias not supported"
502 EventNone -> error "toEventRaw: EventNone not supported"
487 EventNone -> error "toEventRaw: EventNone not supported"
503− unless (ret == 1) $ failure $ ToEventRawException ret
488+ unless (ret == 1) $ throwIO $ ToEventRawException ret
506newtype ToEventRawException = ToEventRawException CInt
491newtype ToEventRawException = ToEventRawException CInt
507 deriving (Show, Typeable)
492 deriving (Show, Typeable)
508instance Exception ToEventRawException
493instance Exception ToEventRawException
510−encode :: MonadFailure YamlException m
511− => [Event]
495+encode :: (a -> Maybe (Event, a))
512− -> IO (m B.ByteString)
497+ -> IO (Either YamlException B.ByteString)
513−encode = withEmitter . flip emitEvents
498+encode unfold src = withEmitter $ emitEvents unfold src
500+encodeFile :: FilePath
501+ -> (a -> Maybe (Event, a))
503+ -> IO (Either YamlException ())
515−-- FIXME encodeFile
504+encodeFile filePath unfold src = do
505+ res <- withEmitter $ emitEvents unfold src
507+ Left e -> return $ Left e
509+ Data.ByteString.writeFile filePath bs
517decode :: B.ByteString
512decode :: B.ByteString
518 -> (a -> Event -> Either a a)
513 -> (a -> Event -> Either a a)