on master help sign in
Recent attempt changes
commit88482b52b5842748878334284bb225727b8f6b40 authorMichael Snoyman <michael@snoyman.com> dateMon 26 Oct 2009, 07:30 (17 years ago) parent278ba7d
2 files changed+12−12
Text/Libyaml.hs+4−7 Text/Yaml.hs+8−5
Text/Libyaml.hs+4−7
@@ -46,8 +46,7 @@ foreign import ccall unsafe "yaml_parser_set_input_string"
4646 -> IO ()
4747
4848withParser :: B.ByteString -> (Parser -> IO a) -> IO a
49−withParser bs f = do
50− allocaBytes parserSize $ \p ->
49+withParser bs f = allocaBytes parserSize $ \p ->
5150 do
5251 _res <- c_yaml_parser_initialize p
5352 -- FIXME check res
@@ -62,7 +61,7 @@ withParser bs f = do
6261 return ret
6362
6463withEventRaw :: (EventRaw -> IO a) -> IO a
65−withEventRaw f = allocaBytes eventSize $ f
64+withEventRaw = allocaBytes eventSize
6665
6766foreign import ccall unsafe "yaml_parser_parse"
6867 c_yaml_parser_parse :: Parser -> EventRaw -> IO CInt
@@ -184,8 +183,7 @@ foreign import ccall unsafe "get_buffer_used"
184183 c_get_buffer_used :: Buffer -> IO CULong
185184
186185withBuffer :: (Buffer -> IO (Attempt ())) -> IO (Attempt B.ByteString)
187−withBuffer f = do
188− allocaBytes bufferSize $ \b -> do
186+withBuffer f = allocaBytes bufferSize $ \b -> do
189187 c_buffer_init b
190188 aRes <- f b
191189 case aRes of
@@ -200,8 +198,7 @@ foreign import ccall unsafe "my_emitter_set_output"
200198 c_my_emitter_set_output :: Emitter -> Buffer -> IO ()
201199
202200withEmitter :: (Emitter -> IO (Attempt ())) -> IO (Attempt B.ByteString)
203−withEmitter f = do
204− allocaBytes emitterSize $ \e -> do
201+withEmitter f = allocaBytes emitterSize $ \e -> do
205202 _res <- c_yaml_emitter_initialize e
206203 -- FIXME check res
207204 bs <- withBuffer $ \b -> do
Text/Yaml.hs+8−5
@@ -32,8 +32,7 @@ import Data.Object
3232import Data.Object.Raw
3333import System.IO.Unsafe
3434import Control.Arrow (first, (***))
35−import Control.Monad (replicateM)
36−import Data.Attempt
35+import Control.Monad (replicateM, liftM)
3736import qualified Data.Attempt.Helper as A
3837import Control.Monad.Trans
3938import Control.Monad.Attempt
@@ -68,7 +67,7 @@ decodeFile path = liftIO (B.readFile path) >>= decode'
6867encode' :: (StrictByteString bs, ToObject o Raw Raw) => o -> IO bs
6968encode' o =
7069 let events = objectToEvents $ toObject o
71− result = withEmitter $ flip emitEvents $ events
70+ result = withEmitter $ flip emitEvents events
7271 in fromStrictByteString `fmap` A.join result
7372
7473decode' :: (StrictByteString bs, FromObject o Raw Raw, MonadIO m,
@@ -77,7 +76,11 @@ decode' :: (StrictByteString bs, FromObject o Raw Raw, MonadIO m,
7776 -> m o
7877decode' bs = do
7978 events <- liftIO $ withParser (toStrictByteString bs) parserParse
80− attempt failure (fromObject . eventsToRawObject) events
79+ attempt failure (myFA . fromObject . eventsToRawObject) events
80+ where
81+ myFA :: MonadAttempt m => Attempt v -> m v
82+ myFA (Success v) = return v
83+ myFA (Failure e) = failure e
8184
8285objectToEvents :: RawObject -> [Event]
8386objectToEvents y = concat
@@ -132,7 +135,7 @@ newtype MyString = MyString String
132135instance ToScalar MyString Raw where
133136 toScalar (MyString s) = toScalar s
134137instance FromScalar MyString Raw where
135− fromScalar raw = MyString `fmap` fromScalar raw
138+ fromScalar raw = MyString `liftM` fromScalar raw
136139
137140propEncodeDecode :: Object MyString MyString -> Bool
138141propEncodeDecode o = fromAttempt (decode (encode o :: B.ByteString))