]> code.delx.au - offlineimap/commitdiff
Changed Connection to use StateT instead of State, with Either
authorJohn Goerzen <jgoerzen@complete.org>
Tue, 12 Aug 2008 13:24:39 +0000 (08:24 -0500)
committerJohn Goerzen <jgoerzen@complete.org>
Tue, 12 Aug 2008 13:24:39 +0000 (08:24 -0500)
This allows us to handle "fail" in a pure way.

src/Network/IMAP/Connection.hs
testsrc/TestConnection.hs

index 2c06d20451cdc758515e9d415ca2bec54dae1474..ace53e85d82509a1fb071bd18713104e7752b487 100644 (file)
@@ -19,10 +19,11 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 module Network.IMAP.Connection where
 import Network.IMAP.Types
 import Control.Monad.State
+import Control.Monad.Error
 import Data.List.Utils(spanList)
 import Data.List(genericSplitAt, genericLength, intercalate)
 
-type IMAPState = State (IMAPString, IMAPString)
+type IMAPState = StateT (IMAPString, IMAPString) (Either String)
 
 {- | Set up an IMAPConnection that runs in the State monad.
 
@@ -62,16 +63,16 @@ newStringConnection =
 runStringConnection ::
        IMAPString               -- ^ Buffer to send to clients
     -> (IMAPConnection IMAPState -> IMAPState a) -- ^ Function to run
-    -> (a, (String, String))    -- ^ Results: func result, buffer status
+    -> Either String (a, (String, String))    -- ^ Results: func result, buffer status
 runStringConnection sbuf func =
-    runState (func newStringConnection) (sbuf::String, []::String)
+    runStateT (func newStringConnection) (sbuf::String, []::String)
 
 {- | Runs a State monad with a String connection, initializing it with 
 the passed lines.  -}
 runLinesConnection ::
        [IMAPString]             -- ^ Buffer to send to clients
     -> (IMAPConnection IMAPState -> IMAPState a) -- ^ Function to run
-    -> (a, (String, String))    -- ^ Results: func result, buffer status
+    -> Either String (a, (String, String))    -- ^ Results: func result, buffer status
 runLinesConnection sbuf func 
     | sbuf == [] = 
         -- For the empty input, no \r\n after.
index a8444113bed51c8129cc5162c5a22aaf4e4c97fc..681d45e3ca5270be420fadfa6fc46d152f32213a 100644 (file)
@@ -28,16 +28,16 @@ import Network.IMAP.Connection
 import Network.IMAP.Types
 
 prop_identity :: String -> Bool
-prop_identity f = runStringConnection f (\_ -> return ()) == ((), (f, []))
+prop_identity f = runStringConnection f (\_ -> return ()) == Right ((), (f, []))
 
 prop_linesidentity :: String -> Bool
 prop_linesidentity f =
-    runLinesConnection [f] (\_ -> return ()) == ((), (f ++ "\r\n", []))
+    runLinesConnection [f] (\_ -> return ()) == Right ((), (f ++ "\r\n", []))
 
 prop_lineslistidentity :: [String] -> Property
 prop_lineslistidentity f =
     and (map (notElem '\r') f)  ==> 
-        runLinesConnection f (\_ -> return ()) @?= ((), (expected, []))
+        runLinesConnection f (\_ -> return ()) @?= Right ((), (expected, []))
     where expected = expectedString f
 
 expectedString f =
@@ -49,13 +49,13 @@ prop_readLine :: [String] -> Property
 prop_readLine s =
     (not (null s)) && (and (map (notElem '\r') s)) ==> 
         runLinesConnection s readLine @?=
-            (head s, (expectedString (tail s), []))
+            Right (head s, (expectedString (tail s), []))
 
 prop_readBytes :: String -> Int -> Property
 prop_readBytes s l =
     l <= length s && l >= 0 ==> 
       runStringConnection s (\c -> readBytes c (fromIntegral l)) ==
-                         (take l s, (drop l s, []))
+                         Right (take l s, (drop l s, []))
 
 q :: Testable a => String -> a -> HU.Test
 q = qccheck (defaultConfig {configMaxTest = 250, configMaxFail = 5000})