better type for Retriever

Putting a callback in the Retriever type allows for the callback to
remove the retrieved file when it's done with it.

I did not really want to make Retriever be fixed to Annex Bool,
but when I tried to use Annex a, I got into some type of type mess.
This commit is contained in:
Joey Hess 2014-07-29 18:40:40 -04:00
parent 47e522979c
commit bc9e4697b9
4 changed files with 53 additions and 37 deletions

View file

@ -1,4 +1,4 @@
{- Types for Storer and Retriever
{- Types for Storer and Retriever actions for remotes.
-
- Copyright 2014 Joey Hess <joey@kitenet.net>
-
@ -10,7 +10,6 @@
module Types.StoreRetrieve where
import Common.Annex
import Annex.Content
import Utility.Metered
import qualified Data.ByteString.Lazy as L
@ -28,25 +27,7 @@ data ContentSource
-- Can throw exceptions.
type Storer = Key -> ContentSource -> MeterUpdate -> Annex Bool
-- Action that retrieves a Key's content from a remote.
-- Action that retrieves a Key's content from a remote, passing it to a
-- callback.
-- Throws exception if key is not present, or remote is not accessible.
type Retriever = Key -> MeterUpdate -> Annex ContentSource
fileStorer :: (Key -> FilePath -> MeterUpdate -> Annex Bool) -> Storer
fileStorer a k (FileContent f) m = a k f m
fileStorer a k (ByteContent b) m = withTmp k $ \tmp -> do
liftIO $ L.writeFile tmp b
a k tmp m
byteStorer :: (Key -> L.ByteString -> MeterUpdate -> Annex Bool) -> Storer
byteStorer a k c m = withBytes c $ \b -> a k b m
fileRetriever :: (Key -> MeterUpdate -> Annex FilePath) -> Retriever
fileRetriever a k m = FileContent <$> a k m
byteRetriever :: (Key -> Annex L.ByteString) -> Retriever
byteRetriever a k _m = ByteContent <$> a k
withBytes :: ContentSource -> (L.ByteString -> Annex a) -> Annex a
withBytes (ByteContent b) a = a b
withBytes (FileContent f) a = a =<< liftIO (L.readFile f)
type Retriever = Key -> MeterUpdate -> (ContentSource -> Annex Bool) -> Annex Bool