{-# LANGUAGE TypeSynonymInstances  #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}

-- | Defines an interface for things that can be written to disk.
module Achille.Writable where

import Data.Text                as Text
import qualified Data.Text.Lazy as LT
import Data.Text.Encoding (encodeUtf8)
import qualified Data.Text.Lazy.Encoding as LT (encodeUtf8)
import Data.ByteString      as BS
import Data.ByteString.Lazy as LBS

import qualified Data.ByteString.Lazy as ByteString

import Achille.Internal.IO as AchilleIO


-- | Class for things that can be saved.
class Writable m a where
    write :: FilePath -> a -> m ()

instance AchilleIO m => Writable m [Char] where
    write :: FilePath -> FilePath -> m ()
write to :: FilePath
to = FilePath -> Text -> m ()
forall (m :: * -> *) a. Writable m a => FilePath -> a -> m ()
write FilePath
to (Text -> m ()) -> (FilePath -> Text) -> FilePath -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> Text
Text.pack

instance AchilleIO m => Writable m Text where
    write :: FilePath -> Text -> m ()
write to :: FilePath
to = FilePath -> ByteString -> m ()
forall (m :: * -> *). AchilleIO m => FilePath -> ByteString -> m ()
AchilleIO.writeFile FilePath
to (ByteString -> m ()) -> (Text -> ByteString) -> Text -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
encodeUtf8

instance AchilleIO m => Writable m LT.Text where
    write :: FilePath -> Text -> m ()
write to :: FilePath
to = FilePath -> ByteString -> m ()
forall (m :: * -> *). AchilleIO m => FilePath -> ByteString -> m ()
AchilleIO.writeFileLazy FilePath
to (ByteString -> m ()) -> (Text -> ByteString) -> Text -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
LT.encodeUtf8

instance AchilleIO m => Writable m BS.ByteString where
    write :: FilePath -> ByteString -> m ()
write = FilePath -> ByteString -> m ()
forall (m :: * -> *). AchilleIO m => FilePath -> ByteString -> m ()
AchilleIO.writeFile

instance AchilleIO m => Writable m LBS.ByteString where
    write :: FilePath -> ByteString -> m ()
write = FilePath -> ByteString -> m ()
forall (m :: * -> *). AchilleIO m => FilePath -> ByteString -> m ()
AchilleIO.writeFileLazy