-- Copyright 2016 TensorFlow authors.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

-- | Conduit wrappers for TensorFlow.Records.

{-# LANGUAGE Rank2Types #-}
module TensorFlow.Records.Conduit
  (
  -- * Encode/Decode
    encodeTFRecords
  , decodeTFRecords

  -- * Source/Sink
  , sinkTFRecords
  , sourceTFRecords
  ) where

import Control.Monad.Catch (MonadThrow)
import Control.Monad.Trans.Resource (MonadResource)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import Data.Conduit ((.|), ConduitT)
import Data.Conduit.Binary (sinkFile, sourceFile)
import Data.Conduit.Cereal (conduitGet2, conduitPut)

import TensorFlow.Records (getTFRecord, putTFRecord)

-- | Decode TFRecords from a stream of bytes.
decodeTFRecords :: MonadThrow m => ConduitT B.ByteString BL.ByteString m ()
decodeTFRecords :: ConduitT ByteString ByteString m ()
decodeTFRecords = Get ByteString -> ConduitT ByteString ByteString m ()
forall (m :: * -> *) o.
MonadThrow m =>
Get o -> ConduitT ByteString o m ()
conduitGet2 Get ByteString
getTFRecord

-- | Read TFRecords from a file.
sourceTFRecords :: (MonadResource m, MonadThrow m) => FilePath -> ConduitT i BL.ByteString m ()
sourceTFRecords :: FilePath -> ConduitT i ByteString m ()
sourceTFRecords path :: FilePath
path = FilePath -> ConduitT i ByteString m ()
forall (m :: * -> *) i.
MonadResource m =>
FilePath -> ConduitT i ByteString m ()
sourceFile FilePath
path ConduitT i ByteString m ()
-> ConduitM ByteString ByteString m ()
-> ConduitT i ByteString m ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
.| ConduitM ByteString ByteString m ()
forall (m :: * -> *).
MonadThrow m =>
ConduitT ByteString ByteString m ()
decodeTFRecords

-- | Encode TFRecords to a stream of bytes.
encodeTFRecords :: Monad m => ConduitT BL.ByteString B.ByteString m ()
encodeTFRecords :: ConduitT ByteString ByteString m ()
encodeTFRecords = Putter ByteString -> ConduitT ByteString ByteString m ()
forall (m :: * -> *) a.
Monad m =>
Putter a -> ConduitT a ByteString m ()
conduitPut Putter ByteString
putTFRecord

-- | Write TFRecords to a file.
sinkTFRecords :: (MonadResource m) => FilePath -> ConduitT BL.ByteString o m ()
sinkTFRecords :: FilePath -> ConduitT ByteString o m ()
sinkTFRecords path :: FilePath
path = ConduitT ByteString ByteString m ()
forall (m :: * -> *).
Monad m =>
ConduitT ByteString ByteString m ()
encodeTFRecords ConduitT ByteString ByteString m ()
-> ConduitM ByteString o m () -> ConduitT ByteString o m ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
.| FilePath -> ConduitM ByteString o m ()
forall (m :: * -> *) o.
MonadResource m =>
FilePath -> ConduitT ByteString o m ()
sinkFile FilePath
path