Skip to content

Commit

Permalink
[#174] Add _LText and lazyText (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
vrom911 authored and chshersh committed Jan 11, 2019
1 parent 45f6af8 commit cda93dd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion src/Toml/Bi/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Toml.Bi.Combinators
, float
-- ** Text types
, text
, lazyText
, byteString
, lazyByteString
, string
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions src/Toml/Bi/Map.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Toml.Bi.Map
-- * Helpers for BiMap and AnyValue
, mkAnyValueBiMap
, _TextBy
, _LTextText
, _NaturalInteger
, _StringText
, _ReadString
Expand All @@ -37,6 +38,7 @@ module Toml.Bi.Map
, _Double
, _Integer
, _Text
, _LText
, _ZonedTime
, _LocalTime
, _Day
Expand Down Expand Up @@ -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.
-}
Expand Down
5 changes: 5 additions & 0 deletions test/Test/Toml/BiMap/Property.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cda93dd

Please sign in to comment.