packages feed

string-interpolate-0.2.0.2 / src / lib / Data / String / Interpolate / Conversion.hs

{-# LANGUAGE DataKinds             #-}{-# LANGUAGE FlexibleInstances     #-}{-# LANGUAGE KindSignatures        #-}{-# LANGUAGE MultiParamTypeClasses #-}{-# LANGUAGE PackageImports        #-}{-# LANGUAGE TypeFamilies          #-}{-# LANGUAGE UndecidableInstances  #-}module Data.String.Interpolate.Conversion  ( IsCustomSink, InterpSink(..), Interpolatable(..)  , SpaceChompable(..)  , bsToTextBuilder, lbsToTextBuilder, encodeCharUTF8  )whereimport Data.Int              ( Int64 )import Data.Char             ( isSpace )import Data.Monoid           ( (<>) )import Data.Proxyimport Data.String           ( IsString, fromString )import Data.Text.Conversionsimport qualified Data.ByteString         as Bimport qualified Data.ByteString.Builder as LBimport qualified Data.ByteString.Lazy    as LBimport qualified Data.Text               as Timport qualified Data.Text.Lazy          as LT hiding ( singleton )import qualified Data.Text.Lazy.Builder  as LTimport qualified "utf8-string" Data.ByteString.Lazy.UTF8 as LUTF8import qualified "utf8-string" Data.ByteString.UTF8      as UTF8import "base" Control.Category ( (>>>) )import "base" Text.Show ( ShowS, showString, showChar )-- |-- We wrap the builders in B so that we can add a phantom type parameter.-- This gives the inner `interpolate's enough information to know where-- they're going and pick an instance, forcing all the types into lockstep.newtype B dst a = B { unB :: a }  deriving (Eq, Show)-- | Does this type require special behavior when something is interpolated /into/ it?type family IsCustomSink dst where  IsCustomSink T.Text = 'True  IsCustomSink LT.Text = 'True  IsCustomSink LT.Builder = 'True  IsCustomSink B.ByteString = 'True  IsCustomSink LB.ByteString = 'True  IsCustomSink LB.Builder = 'True  IsCustomSink _ = 'False-- | Something that can be interpolated into.class IsCustomSink dst ~ flag => InterpSink (flag :: Bool) dst where  type Builder flag dst :: *  -- | Meant to be used only for verbatim parts of the interpolation.  ofString :: Proxy flag -> String -> B dst (Builder flag dst)  -- |  -- `build' should be 'in-order'; that is, the left builder comes from  -- a string on the left, and the right builder comes from a string on the right.  build :: Proxy flag -> B dst (Builder flag dst) -> B dst (Builder flag dst) -> B dst (Builder flag dst)  finalize :: Proxy flag -> B dst (Builder flag dst) -> dst-- |-- Represents that we can interpolate objects of type src into a an-- interpolation string that returns type dst.class InterpSink flag dst => Interpolatable (flag :: Bool) src dst where  interpolate :: Proxy flag -> src -> B dst (Builder flag dst)-- |-- We can collapse whitespace in the given type.class SpaceChompable a where  chompSpaces :: a -> ainstance (IsCustomSink str ~ 'False, IsString str) => InterpSink 'False str where  type Builder 'False str = ShowS  ofString _ = B . showString  build _ (B f) (B g) = B $ f . g  finalize _ = fromString . ($ "") . unBinstance InterpSink 'True T.Text where  type Builder 'True T.Text = LT.Builder  ofString _ = B . LT.fromString  build _ (B l) (B r) = B $ l <> r  finalize _ = LT.toStrict . LT.toLazyText . unBinstance InterpSink 'True LT.Text where  type Builder 'True LT.Text = LT.Builder  ofString _ = B . LT.fromString  build _ (B l) (B r) = B $ l <> r  finalize _ = LT.toLazyText . unBinstance InterpSink 'True LT.Builder where  type Builder 'True LT.Builder = LT.Builder  ofString _ = B . LT.fromString  build _ (B l) (B r) = B $ l <> r  finalize _ = unBinstance InterpSink 'True B.ByteString where  type Builder 'True B.ByteString = LB.Builder  ofString _ = B . LB.byteString . unUTF8 . convertText  build _ (B l) (B r) = B $ l <> r  finalize _ = LB.toStrict . LB.toLazyByteString . unBinstance InterpSink 'True LB.ByteString where  type Builder 'True LB.ByteString = LB.Builder  ofString _ = B . LB.lazyByteString . unUTF8 . convertText  build _ (B l) (B r) = B $ l <> r  finalize _ = LB.toLazyByteString . unBinstance InterpSink 'True LB.Builder where  type Builder 'True LB.Builder = LB.Builder  ofString _ = B . LB.lazyByteString . unUTF8 . convertText  build _ (B l) (B r) = B $ l <> r  finalize _ = unBinstance {-# OVERLAPPABLE #-} (Show src, IsString dst, IsCustomSink dst ~ 'False) => Interpolatable 'False src dst where  interpolate _ = B . showsinstance {-# OVERLAPS #-} (IsString dst, IsCustomSink dst ~ 'False) => Interpolatable 'False Char dst where  interpolate _ = B . showCharinstance {-# OVERLAPS #-} (IsString dst, IsCustomSink dst ~ 'False) => Interpolatable 'False String dst where  interpolate _ = B . showStringinstance {-# OVERLAPS #-} (IsString dst, IsCustomSink dst ~ 'False) => Interpolatable 'False T.Text dst where  interpolate _ = B . showString . T.unpackinstance {-# OVERLAPS #-} (IsString dst, IsCustomSink dst ~ 'False) => Interpolatable 'False LT.Text dst where  interpolate _ = B . showString . LT.unpackinstance {-# OVERLAPS #-} (IsString dst, IsCustomSink dst ~ 'False) => Interpolatable 'False LT.Builder dst where  interpolate _ = B . showString . LT.unpack . LT.toLazyTextinstance {-# OVERLAPS #-} (IsString dst, IsCustomSink dst ~ 'False) => Interpolatable 'False B.ByteString dst where  interpolate _ = B . showString . UTF8.toStringinstance {-# OVERLAPS #-} (IsString dst, IsCustomSink dst ~ 'False) => Interpolatable 'False LB.ByteString dst where  interpolate _ = B . showString . LUTF8.toStringinstance {-# OVERLAPS #-} (IsString dst, IsCustomSink dst ~ 'False) => Interpolatable 'False LB.Builder dst where  interpolate _ = B . showString . LUTF8.toString . LB.toLazyByteStringinstance {-# OVERLAPPABLE #-} Show src => Interpolatable 'True src T.Text where  interpolate _ = B . LT.fromString . showinstance {-# OVERLAPS #-} Interpolatable 'True Char T.Text where  interpolate _ = B . LT.singletoninstance {-# OVERLAPS #-} Interpolatable 'True String T.Text where  interpolate _ = B . LT.fromStringinstance {-# OVERLAPS #-} Interpolatable 'True T.Text T.Text where  interpolate _ = B . LT.fromTextinstance {-# OVERLAPS #-} Interpolatable 'True LT.Text T.Text where  interpolate _ = B . LT.fromLazyTextinstance {-# OVERLAPS #-} Interpolatable 'True LT.Builder T.Text where  interpolate _ = Binstance {-# OVERLAPS #-} Interpolatable 'True B.ByteString T.Text where  interpolate _ = B . bsToTextBuilderinstance {-# OVERLAPS #-} Interpolatable 'True LB.ByteString T.Text where  interpolate _ = B . lbsToTextBuilderinstance {-# OVERLAPS #-} Interpolatable 'True LB.Builder T.Text where  interpolate _ = B . lbsToTextBuilder . LB.toLazyByteStringinstance {-# OVERLAPPABLE #-} Show src => Interpolatable 'True src LT.Text where  interpolate _ = B . LT.fromString . showinstance {-# OVERLAPS #-} Interpolatable 'True Char LT.Text where  interpolate _ = B . LT.singletoninstance {-# OVERLAPS #-} Interpolatable 'True String LT.Text where  interpolate _ = B . LT.fromStringinstance {-# OVERLAPS #-} Interpolatable 'True T.Text LT.Text where  interpolate _ = B . LT.fromTextinstance {-# OVERLAPS #-} Interpolatable 'True LT.Text LT.Text where  interpolate _ = B . LT.fromLazyTextinstance {-# OVERLAPS #-} Interpolatable 'True LT.Builder LT.Text where  interpolate _ = Binstance {-# OVERLAPS #-} Interpolatable 'True B.ByteString LT.Text where  interpolate _ = B . bsToTextBuilderinstance {-# OVERLAPS #-} Interpolatable 'True LB.ByteString LT.Text where  interpolate _ = B . lbsToTextBuilderinstance {-# OVERLAPS #-} Interpolatable 'True LB.Builder LT.Text where  interpolate _ = B . lbsToTextBuilder . LB.toLazyByteStringinstance {-# OVERLAPPABLE #-} Show src => Interpolatable 'True src LT.Builder where  interpolate _ = B . LT.fromString . showinstance {-# OVERLAPS #-} Interpolatable 'True Char LT.Builder where  interpolate _ = B . LT.singletoninstance {-# OVERLAPS #-} Interpolatable 'True String LT.Builder where  interpolate _ = B . LT.fromStringinstance {-# OVERLAPS #-} Interpolatable 'True T.Text LT.Builder where  interpolate _ = B . LT.fromTextinstance {-# OVERLAPS #-} Interpolatable 'True LT.Text LT.Builder where  interpolate _ = B . LT.fromLazyTextinstance {-# OVERLAPS #-} Interpolatable 'True LT.Builder LT.Builder where  interpolate _ = Binstance {-# OVERLAPS #-} Interpolatable 'True B.ByteString LT.Builder where  interpolate _ = B . bsToTextBuilderinstance {-# OVERLAPS #-} Interpolatable 'True LB.ByteString LT.Builder where  interpolate _ = B . lbsToTextBuilderinstance {-# OVERLAPS #-} Interpolatable 'True LB.Builder LT.Builder where  interpolate _ = B . lbsToTextBuilder . LB.toLazyByteStringinstance {-# OVERLAPPABLE #-} Show src => Interpolatable 'True src B.ByteString where  interpolate _ = B . LB.byteString . unUTF8 . convertText . showinstance {-# OVERLAPS #-} Interpolatable 'True Char B.ByteString where  interpolate _ = B . encodeCharUTF8instance {-# OVERLAPS #-} Interpolatable 'True String B.ByteString where  interpolate _ = B . LB.byteString . unUTF8 . convertTextinstance {-# OVERLAPS #-} Interpolatable 'True T.Text B.ByteString where  interpolate _ = B . LB.byteString . unUTF8 . convertTextinstance {-# OVERLAPS #-} Interpolatable 'True LT.Text B.ByteString where  interpolate _ = B . LB.byteString . unUTF8 . convertTextinstance {-# OVERLAPS #-} Interpolatable 'True LT.Builder B.ByteString where  interpolate _ = B . LB.byteString . unUTF8 . convertText . LT.toLazyTextinstance {-# OVERLAPS #-} Interpolatable 'True B.ByteString B.ByteString where  interpolate _ = B . LB.byteStringinstance {-# OVERLAPS #-} Interpolatable 'True LB.ByteString B.ByteString where  interpolate _ = B . LB.lazyByteStringinstance {-# OVERLAPS #-} Interpolatable 'True LB.Builder B.ByteString where  interpolate _ = Binstance {-# OVERLAPPABLE #-} Show src => Interpolatable 'True src LB.ByteString where  interpolate _ = B . LB.lazyByteString . unUTF8 . convertText . showinstance {-# OVERLAPS #-} Interpolatable 'True Char LB.ByteString where  interpolate _ = B . encodeCharUTF8instance {-# OVERLAPS #-} Interpolatable 'True String LB.ByteString where  interpolate _ = B . LB.lazyByteString . unUTF8 . convertTextinstance {-# OVERLAPS #-} Interpolatable 'True T.Text LB.ByteString where  interpolate _ = B . LB.lazyByteString . unUTF8 . convertTextinstance {-# OVERLAPS #-} Interpolatable 'True LT.Text LB.ByteString where  interpolate _ = B . LB.lazyByteString . unUTF8 . convertTextinstance {-# OVERLAPS #-} Interpolatable 'True LT.Builder LB.ByteString where  interpolate _ = B . LB.lazyByteString . unUTF8 . convertText . LT.toLazyTextinstance {-# OVERLAPS #-} Interpolatable 'True B.ByteString LB.ByteString where  interpolate _ = B . LB.byteStringinstance {-# OVERLAPS #-} Interpolatable 'True LB.ByteString LB.ByteString where  interpolate _ = B . LB.lazyByteStringinstance {-# OVERLAPS #-} Interpolatable 'True LB.Builder LB.ByteString where  interpolate _ = Binstance {-# OVERLAPPABLE #-} Show src => Interpolatable 'True src LB.Builder where  interpolate _ = B . LB.lazyByteString . unUTF8 . convertText . showinstance {-# OVERLAPS #-} Interpolatable 'True Char LB.Builder where  interpolate _ = B . encodeCharUTF8instance {-# OVERLAPS #-} Interpolatable 'True String LB.Builder where  interpolate _ = B . LB.lazyByteString . unUTF8 . convertTextinstance {-# OVERLAPS #-} Interpolatable 'True T.Text LB.Builder where  interpolate _ = B . LB.lazyByteString . unUTF8 . convertTextinstance {-# OVERLAPS #-} Interpolatable 'True LT.Text LB.Builder where  interpolate _ = B . LB.lazyByteString . unUTF8 . convertTextinstance {-# OVERLAPS #-} Interpolatable 'True LT.Builder LB.Builder where  interpolate _ = B . LB.lazyByteString . unUTF8 . convertText . LT.toLazyTextinstance {-# OVERLAPS #-} Interpolatable 'True B.ByteString LB.Builder where  interpolate _ = B . LB.byteStringinstance {-# OVERLAPS #-} Interpolatable 'True LB.ByteString LB.Builder where  interpolate _ = B . LB.lazyByteStringinstance {-# OVERLAPS #-} Interpolatable 'True LB.Builder LB.Builder where  interpolate _ = Binstance {-# OVERLAPPABLE #-} (Show a, IsString a) => SpaceChompable a where  chompSpaces = fromString . chompSpaces . showinstance {-# OVERLAPS #-} SpaceChompable String where  chompSpaces = unwords . wordsinstance {-# OVERLAPS #-} SpaceChompable T.Text where  chompSpaces = T.unwords . T.wordsinstance {-# OVERLAPS #-} SpaceChompable LT.Text where  chompSpaces = LT.unwords . LT.words-- | For storing state while we fold over the ByteString.data BSChomper = BSChomper  { bscNumWS :: !Int64  , bscBuilder :: !(Maybe LB.Builder)  -- ^ We use Maybe here to know if we've processed                                       --   non-whitespace characters yet.  }chompBS :: BSChomper -> Char -> BSChomperchompBS bsc c = case (isSpace c, bscNumWS bsc, bscBuilder bsc) of  (True, _, Nothing) -> bsc  (True, n, Just _) -> bsc { bscNumWS = n + 1 }  (False, _, Nothing) -> bsc { bscBuilder = Just (encodeCharUTF8 c) }  (False, 0, Just builder) -> bsc { bscBuilder = Just (builder <> encodeCharUTF8 c) }  (False, _, Just builder) -> bsc { bscBuilder = Just (builder <> encodeCharUTF8 ' ' <> encodeCharUTF8 c)                                  , bscNumWS = 0                                  }finalizeBSC :: BSChomper -> LB.ByteStringfinalizeBSC bsc = case bscBuilder bsc of  Nothing -> mempty  Just builder -> LB.toLazyByteString builderinstance {-# OVERLAPS #-} SpaceChompable B.ByteString where  chompSpaces = UTF8.foldl chompBS (BSChomper 0 Nothing)    >>> finalizeBSC    >>> LB.toStrictinstance {-# OVERLAPS #-} SpaceChompable LB.ByteString where  chompSpaces = LUTF8.foldl chompBS (BSChomper 0 Nothing)    >>> finalizeBSC-- |-- Convert a strict ByteString into a Text `LT.Builder', converting any invalid-- characters into the Unicode replacement character � (U+FFFD).bsToTextBuilder :: B.ByteString -> LT.BuilderbsToTextBuilder = UTF8.foldr (\char bldr -> LT.singleton char <> bldr) mempty-- |-- Convert a lazy ByteString into a Text `LT.Builder', converting any invalid-- characters into the Unicode replacement character � (U+FFFD).lbsToTextBuilder :: LB.ByteString -> LT.BuilderlbsToTextBuilder = LUTF8.foldr (\char bldr -> LT.singleton char <> bldr) mempty-- |-- "Data.ByteString.Builder" provides `charUtf8' to do this, but it doesn't-- correctly handle invalid characters.encodeCharUTF8 :: Char -> LB.BuilderencodeCharUTF8 c =  let normalized = case c of        '\xFFFE' -> '\xFFFD'        '\xFFFF' -> '\xFFFD'        _        -> c  in LB.charUtf8 normalized