on master help sign in
Switch to Attempt
commit1e63f8454b972c482d822b4a3bb62e1f524a3ac9 authorMichael Snoyman <michael@snoyman.com> dateWed 21 Oct 2009, 00:56 (17 years ago) parente9d57ad
3 files changed+32−18
Text/Libyaml.hs+2 Text/Yaml.hs+27−16 yaml.cabal+3−2
Text/Libyaml.hs+2
@@ -70,6 +70,7 @@ foreign import ccall unsafe "yaml_event_delete"
7070foreign import ccall "print_parser_error"
7171 c_print_parser_error :: Parser -> IO ()
7272
73+-- FIXME more grace error handling
7374parserParseOne :: Parser -> IO Event
7475parserParseOne parser = withEventRaw $ \er -> do
7576 res <- c_yaml_parser_parse parser er
@@ -204,6 +205,7 @@ foreign import ccall unsafe "yaml_emitter_emit"
204205foreign import ccall unsafe "print_emitter_error"
205206 c_print_emitter_error :: Emitter -> IO ()
206207
208+-- FIXME more graceful error handling
207209emitEvents :: Emitter -> [Event] -> IO ()
208210emitEvents _ [] = return ()
209211emitEvents emitter (e:rest) = do
Text/Yaml.hs+27−16
@@ -33,6 +33,10 @@ import Data.Object.Raw
3333import System.IO.Unsafe
3434import Control.Arrow (first, (***))
3535import Control.Monad (replicateM)
36+import Data.Attempt
37+import Control.Monad.Trans
38+import Control.Monad.Attempt
39+import Control.Monad.Attempt.Class
3640
3741import Test.Framework (testGroup, Test)
3842import Test.Framework.Providers.HUnit
@@ -43,18 +47,22 @@ import Test.QuickCheck
4347encode :: (StrictByteString bs, ToObject o Raw Raw) => o -> bs
4448encode = unsafePerformIO . encode'
4549
46−decode :: (MonadFail m, StrictByteString bs, FromObject o Raw Raw)
50+decode :: (StrictByteString bs, FromObject o Raw Raw, MonadAttempt m)
4751 => bs
4852 -> m o
49−decode = unsafePerformIO . decode'
53+decode bs = unsafePerformIO $ do
54+ a <- runAttemptT $ decode' bs -- FIXME
55+ case a of
56+ Success s -> return $ success s
57+ Failure e -> return $ failure e
5058
5159encodeFile :: ToObject o Raw Raw => FilePath -> o -> IO ()
5260encodeFile path o = encode' o >>= B.writeFile path
5361
54−decodeFile :: (MonadFail m, FromObject o Raw Raw) => FilePath -> IO (m o)
55−decodeFile path = do
56− c <- B.readFile path
57− decode' c
62+decodeFile :: (FromObject o Raw Raw, MonadAttempt m, MonadIO m)
63+ => FilePath
64+ -> m o
65+decodeFile path = liftIO (B.readFile path) >>= decode'
5866
5967encode' :: (StrictByteString bs, ToObject o Raw Raw) => o -> IO bs
6068encode' o =
@@ -62,13 +70,15 @@ encode' o =
6270 result = withEmitter $ flip emitEvents $ events
6371 in fromStrictByteString `fmap` result
6472
65−decode' :: (MonadFail m, StrictByteString bs, FromObject o Raw Raw)
73+decode' :: (StrictByteString bs, FromObject o Raw Raw, MonadIO m,
74+ MonadAttempt m)
6675 => bs
67− -> IO (m o)
76+ -> m o
6877decode' bs = do
69− let bs' = toStrictByteString bs
70− events = withParser bs' parserParse
71− (fromObject . eventsToRawObject) `fmap` events
78+ events <- liftIO $ withParser (toStrictByteString bs) parserParse
79+ case fromObject $ eventsToRawObject events of -- FIXME something better in attempt
80+ Failure e -> failure e
81+ Success s -> success s
7282
7383objectToEvents :: RawObject -> [Event]
7484objectToEvents y = concat
@@ -126,7 +136,8 @@ instance FromScalar MyString Raw where
126136 fromScalar raw = MyString `fmap` fromScalar raw
127137
128138propEncodeDecode :: Object MyString MyString -> Bool
129−propEncodeDecode o = decode (encode o :: B.ByteString) == Just o
139+propEncodeDecode o = fromAttempt (decode (encode o :: B.ByteString))
140+ == Just o
130141
131142caseEmptyStrings :: Assertion
132143caseEmptyStrings = do
@@ -135,12 +146,12 @@ caseEmptyStrings = do
135146 , ("baz", "")
136147 , ("bin", "")
137148 ]
138− ma = m
139149 let m' = map (toStrictByteString *** toStrictByteString) m
140150 let m'' = map (toLazyByteString *** toLazyByteString) m
141− Just ma @=? decode (encode ma :: String)
142− Just m' @=? decode (encode m' :: String)
143− Just m'' @=? decode (encode m'' :: String)
151+ let test' x = Just x @=? fromAttempt (decode (encode x :: String))
152+ test' m
153+ test' m'
154+ test' m''
144155
145156testSuite :: Test
146157testSuite = testGroup "Text.Yaml"
yaml.cabal+3−2
@@ -25,9 +25,10 @@ library
2525 test-framework-quickcheck,
2626 test-framework-hunit,
2727 HUnit,
28− safe,
2928 time,
30− QuickCheck >= 1 && < 2
29+ QuickCheck >= 1 && < 2,
30+ attempt,
31+ transformers >= 0.1.4.0
3132 exposed-modules: Text.Yaml
3233 other-modules: Text.Libyaml
3334 ghc-options: -Wall