module Darcs.Util.Download.Request
( UrlRequest(..)
, Cachable(..)
, UrlState(..)
, Q(..)
, readQ
, insertQ
, pushQ
, addUsingPriority
, deleteQ
, elemQ
, emptyQ
, nullQ
, Priority(..)
, ConnectionError(..)
) where
import Darcs.Prelude
import Data.List ( delete )
import Data.Map ( Map )
import Foreign.C.Types ( CInt )
data Priority = High
| Low
deriving Priority -> Priority -> Bool
(Priority -> Priority -> Bool)
-> (Priority -> Priority -> Bool) -> Eq Priority
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Priority -> Priority -> Bool
$c/= :: Priority -> Priority -> Bool
== :: Priority -> Priority -> Bool
$c== :: Priority -> Priority -> Bool
Eq
data Cachable = Cachable
| Uncachable
| MaxAge !CInt
deriving (Int -> Cachable -> ShowS
[Cachable] -> ShowS
Cachable -> String
(Int -> Cachable -> ShowS)
-> (Cachable -> String) -> ([Cachable] -> ShowS) -> Show Cachable
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Cachable] -> ShowS
$cshowList :: [Cachable] -> ShowS
show :: Cachable -> String
$cshow :: Cachable -> String
showsPrec :: Int -> Cachable -> ShowS
$cshowsPrec :: Int -> Cachable -> ShowS
Show, Cachable -> Cachable -> Bool
(Cachable -> Cachable -> Bool)
-> (Cachable -> Cachable -> Bool) -> Eq Cachable
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Cachable -> Cachable -> Bool
$c/= :: Cachable -> Cachable -> Bool
== :: Cachable -> Cachable -> Bool
$c== :: Cachable -> Cachable -> Bool
Eq)
data UrlRequest = UrlRequest
{ UrlRequest -> String
url :: String
, UrlRequest -> String
file :: FilePath
, UrlRequest -> Cachable
cachable :: Cachable
, UrlRequest -> Priority
priority :: Priority
}
type InProgressStatus = ( FilePath
, [FilePath]
, Cachable
)
data UrlState = UrlState
{ UrlState -> Map String InProgressStatus
inProgress :: Map String InProgressStatus
, UrlState -> Q String
waitToStart :: Q String
, UrlState -> Int
pipeLength :: Int
, UrlState -> String
randomJunk :: String
}
data Q a = Q [a] [a]
readQ :: Q a -> Maybe (a, Q a)
readQ :: Q a -> Maybe (a, Q a)
readQ (Q (a
x : [a]
xs) [a]
ys) = (a, Q a) -> Maybe (a, Q a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
x, [a] -> [a] -> Q a
forall a. [a] -> [a] -> Q a
Q [a]
xs [a]
ys)
readQ (Q [] [a]
ys) = do
a
x : [a]
xs <- [a] -> Maybe [a]
forall (m :: * -> *) a. Monad m => a -> m a
return ([a] -> Maybe [a]) -> [a] -> Maybe [a]
forall a b. (a -> b) -> a -> b
$ [a] -> [a]
forall a. [a] -> [a]
reverse [a]
ys
(a, Q a) -> Maybe (a, Q a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
x, [a] -> [a] -> Q a
forall a. [a] -> [a] -> Q a
Q [a]
xs [])
addUsingPriority :: Priority -> a -> Q a -> Q a
addUsingPriority :: Priority -> a -> Q a -> Q a
addUsingPriority Priority
High = a -> Q a -> Q a
forall a. a -> Q a -> Q a
pushQ
addUsingPriority Priority
Low = a -> Q a -> Q a
forall a. a -> Q a -> Q a
insertQ
insertQ :: a -> Q a -> Q a
insertQ :: a -> Q a -> Q a
insertQ a
y (Q [a]
xs [a]
ys) = [a] -> [a] -> Q a
forall a. [a] -> [a] -> Q a
Q [a]
xs (a
ya -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
ys)
pushQ :: a -> Q a -> Q a
pushQ :: a -> Q a -> Q a
pushQ a
x (Q [a]
xs [a]
ys) = [a] -> [a] -> Q a
forall a. [a] -> [a] -> Q a
Q (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs) [a]
ys
deleteQ :: Eq a => a -> Q a -> Q a
deleteQ :: a -> Q a -> Q a
deleteQ a
x (Q [a]
xs [a]
ys) = [a] -> [a] -> Q a
forall a. [a] -> [a] -> Q a
Q (a -> [a] -> [a]
forall a. Eq a => a -> [a] -> [a]
delete a
x [a]
xs) (a -> [a] -> [a]
forall a. Eq a => a -> [a] -> [a]
delete a
x [a]
ys)
elemQ :: Eq a => a -> Q a -> Bool
elemQ :: a -> Q a -> Bool
elemQ a
x (Q [a]
xs [a]
ys) = a
x a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [a]
xs Bool -> Bool -> Bool
|| a
x a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [a]
ys
emptyQ :: Q a
emptyQ :: Q a
emptyQ = [a] -> [a] -> Q a
forall a. [a] -> [a] -> Q a
Q [] []
nullQ :: Q a -> Bool
nullQ :: Q a -> Bool
nullQ (Q [] []) = Bool
True
nullQ Q a
_ = Bool
False
data ConnectionError = CouldNotResolveHost
| CouldNotConnectToServer
| OperationTimeout
deriving (ConnectionError -> ConnectionError -> Bool
(ConnectionError -> ConnectionError -> Bool)
-> (ConnectionError -> ConnectionError -> Bool)
-> Eq ConnectionError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ConnectionError -> ConnectionError -> Bool
$c/= :: ConnectionError -> ConnectionError -> Bool
== :: ConnectionError -> ConnectionError -> Bool
$c== :: ConnectionError -> ConnectionError -> Bool
Eq, ReadPrec [ConnectionError]
ReadPrec ConnectionError
Int -> ReadS ConnectionError
ReadS [ConnectionError]
(Int -> ReadS ConnectionError)
-> ReadS [ConnectionError]
-> ReadPrec ConnectionError
-> ReadPrec [ConnectionError]
-> Read ConnectionError
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ConnectionError]
$creadListPrec :: ReadPrec [ConnectionError]
readPrec :: ReadPrec ConnectionError
$creadPrec :: ReadPrec ConnectionError
readList :: ReadS [ConnectionError]
$creadList :: ReadS [ConnectionError]
readsPrec :: Int -> ReadS ConnectionError
$creadsPrec :: Int -> ReadS ConnectionError
Read, Int -> ConnectionError -> ShowS
[ConnectionError] -> ShowS
ConnectionError -> String
(Int -> ConnectionError -> ShowS)
-> (ConnectionError -> String)
-> ([ConnectionError] -> ShowS)
-> Show ConnectionError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ConnectionError] -> ShowS
$cshowList :: [ConnectionError] -> ShowS
show :: ConnectionError -> String
$cshow :: ConnectionError -> String
showsPrec :: Int -> ConnectionError -> ShowS
$cshowsPrec :: Int -> ConnectionError -> ShowS
Show)