text-conversions 0.3.0 → 0.3.1
raw patch · 6 files changed
+96/−55 lines, 6 filesdep −hspec-discoverdep ~base16-bytestringdep ~base64-bytestringdep ~bytestring
Dependencies removed: hspec-discover
Dependency ranges changed: base16-bytestring, base64-bytestring, bytestring, errors, text
Files
- CHANGELOG.md +10/−0
- README.md +1/−1
- package.yaml +10/−8
- src/Data/Text/Conversions.hs +51/−28
- stack.yaml +1/−1
- text-conversions.cabal +23/−17
CHANGELOG.md view
@@ -1,5 +1,15 @@ # Changelog +## 0.3.1 (September 29th, 2020)++- Added support for `base16-bytestring-1.0`.++## 0.3.0 (June 9, 2016)++### New Features++- The `Base16` and `Base64` newtypes are now provided for managing safe conversions between binary data and Base16 and Base64 textual encodings of that data.+ ## 0.2.0 (May 25, 2016) ### Breaking Changes
README.md view
@@ -1,4 +1,4 @@-# text-conversions+# text-conversions [](https://travis-ci.org/cjdev/text-conversions) This is a small library to ease the pain when converting between the many different string types in Haskell. Unlike some other libraries that attempt to solve the same problem, text-conversions is:
package.yaml view
@@ -1,5 +1,5 @@ name: text-conversions-version: '0.3.0'+version: 0.3.1 category: Data synopsis: Safe conversions between textual types description: Safe conversions between textual types@@ -25,12 +25,12 @@ library: source-dirs: src dependencies:- - base >= 4.7 && < 5- - bytestring- - base16-bytestring- - base64-bytestring- - errors- - text+ - base >=4.7 && <5+ - bytestring <1+ - base16-bytestring <2+ - base64-bytestring <2+ - errors <3+ - text <2 tests: text-conversions-test-suite:@@ -45,5 +45,7 @@ - text-conversions - bytestring - hspec- - hspec-discover - text+ verbatim: |+ build-tool-depends:+ hspec-discover:hspec-discover
src/Data/Text/Conversions.hs view
@@ -1,21 +1,21 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveFunctor #-} {-| Module: Data.Text.Conversions This module provides a set of typeclasses for safely converting between- textual data. The built-in 'String' type, as well as strict 'Data.Text.Text'- and lazy 'Data.Text.Lazy.Text', are safely convertible between one another.- The 'Data.ByteString.ByteString' type is frequently treated in much the same- manner, but this is unsafe for two reasons:+ textual data. The built-in 'String' type, as well as strict 'T.Text' and lazy+ 'TL.Text', are safely convertible between one another. The 'B.ByteString' type+ is frequently treated in much the same manner, but this is unsafe for two+ reasons: - * Since 'Data.ByteString.ByteString' encodes binary data, it does not specify- a particular encoding, so assuming a particular encoding like UTF-8 would- be incorrect.+ * Since 'B.ByteString' encodes binary data, it does not specify a particular+ encoding, so assuming a particular encoding like UTF-8 would be incorrect. * Furthermore, decoding binary data into text given a particular encoding can- fail. Most systems simply use 'Data.Text.Encoding.decodeUtf8' and similar- functions, which will dangerously throw exceptions when given invalid data.+ fail. Most systems simply use 'T.decodeUtf8' and similar functions, which+ will dangerously throw exceptions when given invalid data. This module addresses both problems by providing a 'DecodeText' typeclass for decoding binary data in a way that can fail and by providing a 'UTF8' wrapper@@ -35,15 +35,17 @@ >>> decodeConvertText (UTF8 ("\xc3\x28" :: ByteString)) :: Maybe Text Nothing -}-module Data.Text.Conversions- ( Base16(..)- , Base64(..)- , DecodeText(..)- , FromText(..)+module Data.Text.Conversions (+ -- * Conversion typeclasses and functions+ FromText(..) , ToText(..)- , UTF8(..)+ , DecodeText(..) , convertText , decodeConvertText+ -- * Encoding newtypes+ , UTF8(..)+ , Base16(..)+ , Base64(..) ) where import Control.Error.Util (hush)@@ -63,7 +65,16 @@ {-| Simple wrapper type that is used to select a desired encoding when encoding or- decoding text from binary data, such as 'Data.ByteString.ByteString's.+ decoding text from binary data, such as 'B.ByteString's. The conversion is not+ partial; it will result in 'Nothing' when a 'B.ByteString' is provided with+ data that is not valid in UTF-8.++ >>> convertText ("hello" :: Text) :: UTF8 ByteString+ UTF8 "hello"+ >>> decodeConvertText (UTF8 ("hello" :: ByteString)) :: Maybe Text+ Just "hello"+ >>> decodeConvertText (UTF8 ("invalid \xc3\x28" :: ByteString)) :: Maybe Text+ Nothing -} newtype UTF8 a = UTF8 { unUTF8 :: a } deriving (Eq, Show, Functor)@@ -83,28 +94,28 @@ deriving (Eq, Show, Functor) {-|- A simple typeclass that handles converting arbitrary datatypes to- 'Data.Text.Text' when the operation cannot fail. If you have a type that- satisfies that requirement, implement this typeclass, but if the operation can- fail, use 'DecodeText' instead.+ A simple typeclass that handles converting arbitrary datatypes to 'T.Text'+ when the operation cannot fail. If you have a type that satisfies that+ requirement, implement this typeclass, but if the operation can fail, use+ 'DecodeText' instead. -} class ToText a where toText :: a -> T.Text {-|- A simple typeclass that handles converting 'Data.Text.Text' to arbitrary- datatypes. If you have a type that can be produced from text, implement this- typeclass, /not/ 'ConvertText'. However, you probably do not want to call- 'fromText' directly; call 'convertText', instead.+ A simple typeclass that handles converting 'T.Text' to arbitrary datatypes. If+ you have a type that can be produced from text, implement this typeclass.+ However, you probably do not want to call 'fromText' directly; call+ 'convertText', instead. -} class FromText a where fromText :: T.Text -> a {-| A simple typeclass that handles converting arbitrary datatypes to- 'Data.Text.Text' when the operation can fail. If you have a type that- satisfies that requirement, implement this typeclass, but if the operation- cannot fail, use 'ToText' instead.+ 'T.Text' when the operation can fail. If you have a type that satisfies that+ requirement, implement this typeclass, but if the operation cannot fail, use+ 'ToText' instead. -} class Functor f => DecodeText f a where decodeText :: a -> f T.Text@@ -127,7 +138,7 @@ encodings, it is necessary to use a newtype that picks the particular encoding, like 'UTF8': - >>> convertText (UTF8 ("hello" :: ByteString)) :: Maybe Text+ >>> decodeConvertText (UTF8 ("hello" :: ByteString)) :: Maybe Text Just "hello" -} decodeConvertText :: (DecodeText f a, FromText b) => a -> f b@@ -148,9 +159,15 @@ instance ToText (Base16 B.ByteString) where toText = T.decodeUtf8 . Base16.encode . unBase16 instance FromText (Maybe (Base16 B.ByteString)) where+#if MIN_VERSION_base16_bytestring(1,0,0) fromText txt = case Base16.decode (T.encodeUtf8 txt) of+ Right bs -> Just $ Base16 bs+ Left _ -> Nothing+#else+ fromText txt = case Base16.decode (T.encodeUtf8 txt) of (bs, "") -> Just $ Base16 bs (_, _) -> Nothing+#endif instance ToText (Base64 B.ByteString) where toText = T.decodeUtf8 . Base64.encode . unBase64@@ -160,9 +177,15 @@ instance ToText (Base16 BL.ByteString) where toText = TL.toStrict . TL.decodeUtf8 . Base16L.encode . unBase16 instance FromText (Maybe (Base16 BL.ByteString)) where+#if MIN_VERSION_base16_bytestring(1,0,0) fromText txt = case Base16L.decode (TL.encodeUtf8 $ TL.fromStrict txt) of+ Right bs -> Just $ Base16 bs+ Left _ -> Nothing+#else+ fromText txt = case Base16L.decode (TL.encodeUtf8 $ TL.fromStrict txt) of (bs, "") -> Just $ Base16 bs (_, _) -> Nothing+#endif instance ToText (Base64 BL.ByteString) where toText = TL.toStrict . TL.decodeUtf8 . Base64L.encode . unBase64
stack.yaml view
@@ -1,4 +1,4 @@-resolver: lts-5.18+resolver: lts-16.16 packages: ['.'] extra-deps: []
text-conversions.cabal view
@@ -1,9 +1,13 @@--- This file has been generated from package.yaml by hpack version 0.14.0.+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.33.0. -- -- see: https://github.com/sol/hpack+--+-- hash: 7c0bf9ebe6887baff27c40e9fdb7bb6569b32eac6b9e701e2ab9355feb75353d name: text-conversions-version: 0.3.0+version: 0.3.1 synopsis: Safe conversions between textual types description: Safe conversions between textual types category: Data@@ -14,13 +18,11 @@ license: ISC license-file: LICENSE build-type: Simple-cabal-version: >= 1.10- extra-source-files:+ README.md CHANGELOG.md LICENSE package.yaml- README.md stack.yaml source-repository head@@ -28,35 +30,39 @@ location: https://github.com/cjdev/text-conversions library+ exposed-modules:+ Data.Text.Conversions+ other-modules:+ Paths_text_conversions hs-source-dirs: src default-extensions: FlexibleInstances MultiParamTypeClasses OverloadedStrings ghc-options: -Wall build-depends:- base >= 4.7 && < 5- , bytestring- , base16-bytestring- , base64-bytestring- , errors- , text- exposed-modules:- Data.Text.Conversions+ base >=4.7 && <5+ , base16-bytestring <2+ , base64-bytestring <2+ , bytestring <1+ , errors <3+ , text <2 default-language: Haskell2010 test-suite text-conversions-test-suite type: exitcode-stdio-1.0 main-is: Main.hs+ other-modules:+ Data.Text.ConversionsSpec+ Paths_text_conversions hs-source-dirs: test default-extensions: FlexibleInstances MultiParamTypeClasses OverloadedStrings ghc-options: -Wall -rtsopts -threaded -with-rtsopts=-N build-depends: base- , text-conversions , bytestring , hspec- , hspec-discover , text- other-modules:- Data.Text.ConversionsSpec+ , text-conversions default-language: Haskell2010+ build-tool-depends:+ hspec-discover:hspec-discover