Permalink
Browse files

Tweak iteratee/enumerator map types

  • Loading branch information...
1 parent 8ba8f06 commit 1d1c0a134a185c1d01d4923fddabdb1e523f6006 @gregorycollins gregorycollins committed Dec 27, 2010
Showing with 52 additions and 25 deletions.
  1. +4 −3 src/Snap/Internal/Http/Types.hs
  2. +1 −1 src/Snap/Internal/Types.hs
  3. +45 −19 src/Snap/Iteratee.hs
  4. +2 −2 src/Snap/Util/GZip.hs
@@ -372,9 +372,10 @@ rspBodyMap f b = Enum $ f $ rspBodyToEnum b
------------------------------------------------------------------------------
rspBodyToEnum :: ResponseBody -> Enumerator Builder IO a
rspBodyToEnum (Enum e) = e
-rspBodyToEnum (SendFile fp Nothing) = I.mapEnum fromByteString $ I.enumFile fp
-rspBodyToEnum (SendFile fp (Just s)) = I.mapEnum fromByteString $
- I.enumFilePartial fp s
+rspBodyToEnum (SendFile fp Nothing) =
+ I.mapEnum toByteString fromByteString $ I.enumFile fp
+rspBodyToEnum (SendFile fp (Just s)) =
+ I.mapEnum toByteString fromByteString $ I.enumFilePartial fp s
------------------------------------------------------------------------------
@@ -251,7 +251,7 @@ transformRequestBody trans = do
let ioref = rqBody req
senum <- liftIO $ readIORef ioref
let (SomeEnumerator enum') = senum
- let enum = mapEnum fromByteString enum'
+ let enum = mapEnum toByteString fromByteString enum'
liftIO $ writeIORef ioref (SomeEnumerator enumEOF)
origRsp <- getResponse
View
@@ -641,29 +641,55 @@ enumFilePartial fp rng@(start,end) st@(Continue k) = do
------------------------------------------------------------------------------
+mapEnum :: (Monad m) =>
+ (aOut -> aIn)
+ -> (aIn -> aOut)
+ -> Enumerator aIn m a
+ -> Enumerator aOut m a
+mapEnum f g enum outStep = do
+ let z = I.map g outStep
+ let p = joinI z
+ let q = enum $$ p
+ (I.joinI . I.map f) $$ q
+
+
+------------------------------------------------------------------------------
mapIter :: (Monad m) =>
(aOut -> aIn)
- -> Iteratee aOut m a
+ -> (aIn -> aOut)
-> Iteratee aIn m a
-mapIter f iter = iter >>== check
+ -> Iteratee aOut m a
+mapIter f g iter = do
+ step <- lift $ runIteratee iter
+ mapStep step
where
- check (Continue k) = k EOF >>== \s -> case s of
- Continue _ -> error "divergent iteratee"
- _ -> check s
- check (Yield x rest) = yield x (fmap f rest)
- check (Error e) = throwError e
+ -- mapStep :: Step aIn m a -> Iteratee aOut m a
+ mapStep (Continue k) = continue $ wrapK k
+ mapStep (Yield x rest) = yield x (fmap g rest)
+ mapStep (Error e) = throwError e
+
+ -- wrapK :: (Stream aIn -> Iteratee aIn m a)
+ -- -> (Stream aOut -> Iteratee aOut m a)
+ wrapK k streamOut = mapIter f g iterIn
+ where
+ streamIn = fmap f streamOut
+ iterIn = k streamIn
------------------------------------------------------------------------------
-mapEnum :: (Monad m) =>
- (aOut -> aIn)
- -> Enumerator aOut m a
- -> Enumerator aIn m a
-mapEnum f enum builderStep = do
- -- z :: Iteratee ByteString m (Step Builder m a)
- let z = I.map f builderStep
- -- p :: Iteratee ByteString m a
- let p = joinI z
- -- q :: Iteratee ByteString m a
- let q = enum $$ p
- mapIter f q
+-- testIt :: IO ()
+-- testIt = do
+-- xs <- run_ (eInt $$ consume)
+-- putStrLn (show xs)
+
+-- ys <- run_ (eInt $$ replicateM 3 iter1)
+-- putStrLn (show ys)
+
+-- where
+-- eBS :: Enumerator ByteString IO a
+-- eBS = enumList 1 ["12345", "300", "200", "400"]
+
+-- eInt :: Enumerator Int IO a
+-- eInt = mapEnum (S.pack . show) (read . S.unpack) eBS
+
+-- iter1 = mapIter (S.pack . show) (read . S.unpack) I.head
View
@@ -180,9 +180,9 @@ compressEnumerator compFunc enum' origStep = do
readEnd <- liftIO $ newChan
tid <- liftIO $ forkIO $ threadProc readEnd writeEnd
- let enum = mapEnum toByteString enum'
+ let enum = mapEnum fromByteString toByteString enum'
let outEnum = enum (f readEnd writeEnd tid step)
- mapIter fromByteString outEnum
+ mapIter toByteString fromByteString outEnum
where
--------------------------------------------------------------------------

0 comments on commit 1d1c0a1

Please sign in to comment.