module Network.Stream
( Stream(..)
, ConnError(..)
, Result
, bindE
, fmapE
, failParse
, failWith
, failMisc
) where
data ConnError
= ErrorReset
| ErrorClosed
| ErrorParse String
| ErrorMisc String
deriving(Int -> ConnError -> ShowS
[ConnError] -> ShowS
ConnError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ConnError] -> ShowS
$cshowList :: [ConnError] -> ShowS
show :: ConnError -> String
$cshow :: ConnError -> String
showsPrec :: Int -> ConnError -> ShowS
$cshowsPrec :: Int -> ConnError -> ShowS
Show,ConnError -> ConnError -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ConnError -> ConnError -> Bool
$c/= :: ConnError -> ConnError -> Bool
== :: ConnError -> ConnError -> Bool
$c== :: ConnError -> ConnError -> Bool
Eq)
failMisc :: String -> Result a
failMisc :: forall a. String -> Result a
failMisc String
x = forall a. ConnError -> Result a
failWith (String -> ConnError
ErrorMisc String
x)
failParse :: String -> Result a
failParse :: forall a. String -> Result a
failParse String
x = forall a. ConnError -> Result a
failWith (String -> ConnError
ErrorParse String
x)
failWith :: ConnError -> Result a
failWith :: forall a. ConnError -> Result a
failWith ConnError
x = forall a b. a -> Either a b
Left ConnError
x
bindE :: Result a -> (a -> Result b) -> Result b
bindE :: forall a b. Result a -> (a -> Result b) -> Result b
bindE (Left ConnError
e) a -> Result b
_ = forall a b. a -> Either a b
Left ConnError
e
bindE (Right a
v) a -> Result b
f = a -> Result b
f a
v
fmapE :: (a -> Result b) -> IO (Result a) -> IO (Result b)
fmapE :: forall a b. (a -> Result b) -> IO (Result a) -> IO (Result b)
fmapE a -> Result b
f IO (Result a)
a = do
Result a
x <- IO (Result a)
a
case Result a
x of
Left ConnError
e -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left ConnError
e)
Right a
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Result b
f a
r)
type Result a = Either ConnError
a
class Stream x where
readLine :: x -> IO (Result String)
readBlock :: x -> Int -> IO (Result String)
writeBlock :: x -> String -> IO (Result ())
close :: x -> IO ()
closeOnEnd :: x -> Bool -> IO ()