Eventually-decentralized project hosting and management platform

[[ 🗃 ^WvWbo vervis ]] :: [📥 Inbox] [📤 Outbox] [🐤 Followers] [🤝 Collaborators] [🛠 Changes]

Clone

HTTPS: darcs clone https://vervis.peers.community/repos/WvWbo

SSH: darcs clone USERNAME@vervis.peers.community:WvWbo

Tags

TODO

src / Data / Text / Lazy / UTF8 /

Local.hs

{- This file is part of Vervis.
 -
 - Written in 2016 by fr33domlover <fr33domlover@riseup.net>.
 -
 - ♡ Copying is an act of love. Please copy, reuse and share.
 -
 - The author(s) have dedicated all copyright and related and neighboring
 - rights to this software to the public domain worldwide. This software is
 - distributed without any warranty.
 -
 - You should have received a copy of the CC0 Public Domain Dedication along
 - with this software. If not, see
 - <http://creativecommons.org/publicdomain/zero/1.0/>.
 -}

-- | Utilities for conversion between lazy 'Text' and UTF8-encoded lazy
-- 'ByteString'
module Data.Text.Lazy.UTF8.Local
    ( encode
    , decodeStrict
    , decodeLenient
    , encodeFilename
    , encodeSource
    , decodeFilename
    , decodeSource
    )
where

import Prelude ()

import Data.ByteString.Lazy (ByteString)
import Data.Text.Lazy (Text)

import qualified Data.Text.Lazy.Encoding as TLE
import qualified Data.Text.Encoding.Error as TEE

encode :: Text -> ByteString
encode = TLE.encodeUtf8

decodeStrict :: ByteString -> Text
decodeStrict = TLE.decodeUtf8With TEE.strictDecode

decodeLenient :: ByteString -> Text
decodeLenient = TLE.decodeUtf8With TEE.lenientDecode

-- | Encode text in a way appropriate for filenames. This is simply set to
-- 'encode'.
encodeFilename :: Text -> ByteString
encodeFilename = encode

-- | Encode text in a way appropriate for source content. This is simply set to
-- 'encode'.
encodeSource :: Text -> ByteString
encodeSource = encode

-- | Decode text in a way appropriate for filenames. Since these names may be
-- used for reading and writing to the file system, errors here must not be
-- ignored, therefore the conversion is strict.
decodeFilename :: ByteString -> Text
decodeFilename = decodeStrict

-- | Encode text in a way appropriate for source content. Even in the case of
-- an encoding error, the application shouldn't fail. It should still display
-- the content, so that the valid parts are visible and the error too is
-- visible to the user. Therefore the conversion is lenient.
decodeSource :: ByteString -> Text
decodeSource = decodeLenient
[See repo JSON]