{-

This file is part of the Haskell package playlists. It is subject to
the license terms in the LICENSE file found in the top-level directory
of this distribution and at git://pmade.com/playlists/LICENSE. No part
of playlists package, including this file, may be copied, modified,
propagated, or distributed except according to the terms contained in
the LICENSE file.

-}

--------------------------------------------------------------------------------
module Text.Playlist.Internal.ReadWrite
       ( parserForFormat
       , parsePlaylist
       , generatePlaylist
       ) where

--------------------------------------------------------------------------------
import qualified Data.Attoparsec.ByteString as Atto
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Builder as BL

--------------------------------------------------------------------------------
import qualified Text.Playlist.M3U.Reader as M3U
import qualified Text.Playlist.M3U.Writer as M3U
import qualified Text.Playlist.PLS.Reader as PLS
import qualified Text.Playlist.PLS.Writer as PLS
import Text.Playlist.Types

--------------------------------------------------------------------------------
-- | Return the appropriate attoparsec parser for the given playlist format.
parserForFormat :: Format -> Atto.Parser Playlist
parserForFormat :: Format -> Parser Playlist
parserForFormat Format
M3U = Parser Playlist
M3U.parsePlaylist
parserForFormat Format
PLS = Parser Playlist
PLS.parsePlaylist

--------------------------------------------------------------------------------
-- | Parse a playlist from a @ByteString@.  Parsing may fail in which
-- case an error message is returned in @Left@.
--
-- > content <- BS.getContents
-- > case parsePlaylist M3U content of
-- >  Left err -> fail $ "failed to parse playlist: " ++ err
-- >  Right x  -> return x
parsePlaylist :: Format -> ByteString -> Either String Playlist
parsePlaylist :: Format -> ByteString -> Either String Playlist
parsePlaylist Format
f = Parser Playlist -> ByteString -> Either String Playlist
forall a. Parser a -> ByteString -> Either String a
Atto.parseOnly (Format -> Parser Playlist
parserForFormat Format
f)

--------------------------------------------------------------------------------
-- | Generate a lazy @ByteString@ containing playlist data from the
-- given playlist and in the given format.
--
-- > BL.putStr $ generatePlaylist M3U somePlaylist
generatePlaylist :: Format -> Playlist -> BL.ByteString
generatePlaylist :: Format -> Playlist -> ByteString
generatePlaylist Format
M3U = Builder -> ByteString
BL.toLazyByteString (Builder -> ByteString)
-> (Playlist -> Builder) -> Playlist -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Playlist -> Builder
M3U.writePlaylist
generatePlaylist Format
PLS = Builder -> ByteString
BL.toLazyByteString (Builder -> ByteString)
-> (Playlist -> Builder) -> Playlist -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Playlist -> Builder
PLS.writePlaylist