@@ -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"