module Ormolu.Utils.IO
( writeFileUtf8,
readFileUtf8,
getContentsUtf8,
)
where
import Control.Exception (throwIO)
import Control.Monad.IO.Class
import Data.ByteString (ByteString)
import Data.ByteString qualified as B
import Data.Text (Text)
import Data.Text.Encoding qualified as TE
writeFileUtf8 :: (MonadIO m) => FilePath -> Text -> m ()
writeFileUtf8 :: forall (m :: * -> *). MonadIO m => FilePath -> Text -> m ()
writeFileUtf8 FilePath
p = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ByteString -> IO ()
B.writeFile FilePath
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TE.encodeUtf8
readFileUtf8 :: (MonadIO m) => FilePath -> m Text
readFileUtf8 :: forall (m :: * -> *). MonadIO m => FilePath -> m Text
readFileUtf8 FilePath
p = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (FilePath -> IO ByteString
B.readFile FilePath
p) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *). MonadIO m => ByteString -> m Text
decodeUtf8
getContentsUtf8 :: (MonadIO m) => m Text
getContentsUtf8 :: forall (m :: * -> *). MonadIO m => m Text
getContentsUtf8 = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ByteString
B.getContents forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *). MonadIO m => ByteString -> m Text
decodeUtf8
decodeUtf8 :: (MonadIO m) => ByteString -> m Text
decodeUtf8 :: forall (m :: * -> *). MonadIO m => ByteString -> m Text
decodeUtf8 = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall e a. Exception e => e -> IO a
throwIO forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either UnicodeException Text
TE.decodeUtf8'