diff --git a/CHANGELOG.md b/CHANGELOG.md index cffe578d..e59e9d50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,8 @@ The change log is available [on GitHub][2]. Parser for arrays of tables. * [#144](https://github.com/kowainik/tomland/issues/144): Added tests for arrays of tables. +* [#174](https://github.com/kowainik/tomland/issues/174): + Add `_LText` and `lazyText` codecs. ## 0.5.0 — Nov 12, 2018 diff --git a/src/Toml/Bi/Combinators.hs b/src/Toml/Bi/Combinators.hs index 19295643..69df1c48 100644 --- a/src/Toml/Bi/Combinators.hs +++ b/src/Toml/Bi/Combinators.hs @@ -18,6 +18,7 @@ module Toml.Bi.Combinators , float -- ** Text types , text + , lazyText , byteString , lazyByteString , string @@ -69,7 +70,7 @@ import Numeric.Natural (Natural) import Toml.Bi.Code (DecodeException (..), Env, St, TomlCodec, execTomlCodec) import Toml.Bi.Map (BiMap (..), TomlBiMap, _Array, _Bool, _ByteString, _Day, _Double, _Float, - _HashSet, _Int, _IntSet, _Integer, _LByteString, _LocalTime, _Natural, + _HashSet, _Int, _IntSet, _Integer, _LByteString, _LText, _LocalTime, _Natural, _NonEmpty, _Read, _Set, _String, _Text, _TextBy, _TimeOfDay, _Word, _ZonedTime) import Toml.Bi.Monad (Codec (..)) import Toml.PrefixTree (Key) @@ -78,6 +79,7 @@ import Toml.Type (AnyValue (..), TOML (..), insertKeyAnyVal, insertTable, insert import qualified Data.ByteString.Lazy as BL import qualified Data.HashMap.Strict as HashMap import qualified Toml.PrefixTree as Prefix +import qualified Data.Text.Lazy as L {- | General function to create bidirectional converters for key-value pairs. In @@ -144,6 +146,10 @@ float = match _Float text :: Key -> TomlCodec Text text = match _Text +-- | Codec for lazy text values. +lazyText :: Key -> TomlCodec L.Text +lazyText = match _LText + -- | Codec for text values with custom error messages for parsing. textBy :: (a -> Text) -> (Text -> Either Text a) -> Key -> TomlCodec a textBy to from = match (_TextBy to from) diff --git a/src/Toml/Bi/Map.hs b/src/Toml/Bi/Map.hs index d8454818..9696b198 100644 --- a/src/Toml/Bi/Map.hs +++ b/src/Toml/Bi/Map.hs @@ -24,6 +24,7 @@ module Toml.Bi.Map -- * Helpers for BiMap and AnyValue , mkAnyValueBiMap , _TextBy + , _LTextText , _NaturalInteger , _StringText , _ReadString @@ -37,6 +38,7 @@ module Toml.Bi.Map , _Double , _Integer , _Text + , _LText , _ZonedTime , _LocalTime , _Day @@ -297,6 +299,16 @@ _Double = mkAnyValueBiMap matchDouble Double _Text :: TomlBiMap Text AnyValue _Text = mkAnyValueBiMap matchText Text +-- | Helper bimap for 'Data.Text.Lazy.Text' and 'Data.Text.Text'. +_LTextText :: BiMap e TL.Text Text +_LTextText = iso TL.toStrict TL.fromStrict + +{- | 'Data.Text.Lazy.Text' bimap for 'AnyValue'. Usually used as +'Toml.Bi.Combinators.lazyText' combinator. +-} +_LText :: TomlBiMap TL.Text AnyValue +_LText = _LTextText >>> _Text + {- | 'Data.Time.ZonedTime' bimap for 'AnyValue'. Usually used as 'Toml.Bi.Combinators.zonedTime' combinator. -} diff --git a/test/Test/Toml/BiMap/Property.hs b/test/Test/Toml/BiMap/Property.hs index fe41c095..8329e7ff 100644 --- a/test/Test/Toml/BiMap/Property.hs +++ b/test/Test/Toml/BiMap/Property.hs @@ -15,6 +15,7 @@ import qualified Data.ByteString.Lazy as BL import qualified Data.HashSet as HS import qualified Data.IntSet as IS import qualified Data.List.NonEmpty as NE +import qualified Data.Text.Lazy as L import qualified Hedgehog.Gen as Gen import qualified Hedgehog.Range as Range import qualified Test.Toml.Gen as G @@ -42,6 +43,9 @@ genLByteString = BB.toLazyByteString . BB.byteString <$> Gen.utf8 range100 Gen.unicodeAll +genLText :: Gen L.Text +genLText = L.fromStrict <$> G.genText + -- Tests testBiMap @@ -73,6 +77,7 @@ test_BiMaps = pure $ testGroup "BiMap roundtrip tests" $ concat , prop "Float" (testBiMap B._Float (Gen.float $ Range.constant (-10000.0) 10000.0)) , prop "Text" (testBiMap B._Text G.genText) + , prop "LazyText" (testBiMap B._LText genLText) , prop "String" (testBiMap B._String (Gen.string range100 Gen.unicode)) , prop "Read (Integer)" (testBiMap B._Read G.genInteger)