-- |
-- Module      : Crypto.Store.PEM
-- License     : BSD-style
-- Maintainer  : Olivier Chéron <olivier.cheron@gmail.com>
-- Stability   : experimental
-- Portability : unknown
--
-- Extend module "Data.PEM".
module Crypto.Store.PEM
    ( readPEMs
    , writePEMs
    , pemsWriteBS
    , pemsWriteLBS
    , module Data.PEM
    ) where

import Data.PEM
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L

-- | Read a PEM file from disk.
readPEMs :: FilePath -> IO [PEM]
readPEMs :: FilePath -> IO [PEM]
readPEMs FilePath
filepath = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. HasCallStack => FilePath -> a
error forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either FilePath [PEM]
pemParseLBS forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> IO ByteString
L.readFile FilePath
filepath

-- | Convert a list of PEM elements to a bytestring.
pemsWriteBS :: [PEM] -> B.ByteString
pemsWriteBS :: [PEM] -> ByteString
pemsWriteBS = ByteString -> ByteString
L.toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PEM] -> ByteString
pemsWriteLBS

-- | Convert a list of PEM elements to a lazy bytestring.
pemsWriteLBS :: [PEM] -> L.ByteString
pemsWriteLBS :: [PEM] -> ByteString
pemsWriteLBS = [ByteString] -> ByteString
L.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map PEM -> ByteString
pemWriteLBS

-- | Write a PEM file to disk.
writePEMs :: FilePath -> [PEM] -> IO ()
writePEMs :: FilePath -> [PEM] -> IO ()
writePEMs FilePath
filepath = FilePath -> ByteString -> IO ()
L.writeFile FilePath
filepath forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PEM] -> ByteString
pemsWriteLBS