Skip to content

Commit

Permalink
[#164] Reexport ShortByteString (#184)
Browse files Browse the repository at this point in the history
* [#164] Reexport ShortByteString

Resolves #164

* Upgrade dhall, add HLint rules for ShortByteString

* Fix
  • Loading branch information
vrom911 authored and chshersh committed Sep 11, 2019
1 parent 62203b1 commit a6e670f
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 16 deletions.
15 changes: 15 additions & 0 deletions .hlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2659,6 +2659,21 @@
note: '''ByteString'' is already exported from Relude'
name: Use 'ByteString' from Relude
rhs: ByteString
- warn:
lhs: Data.ByteString.Short.ShortByteString
note: '''ShortByteString'' is already exported from Relude'
name: Use 'ShortByteString' from Relude
rhs: ShortByteString
- warn:
lhs: Data.ByteString.Short.toShort
note: '''toShort'' is already exported from Relude'
name: Use 'toShort' from Relude
rhs: toShort
- warn:
lhs: Data.ByteString.Short.fromShort
note: '''fromShort'' is already exported from Relude'
name: Use 'fromShort' from Relude
rhs: fromShort
- warn:
lhs: Data.String.IsString
note: '''IsString'' is already exported from Relude'
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ The changelog is available [on GitHub][2].
Improve usage of performance pragmas.
* [#178](https://github.com/kowainik/relude/issues/178):
Made `die` be polymorphic in its return type.
* [#164](https://github.com/kowainik/relude/issues/164):
Reexport `ShortByteString`, `toShort`/`fromShort` functions.

## 0.5.0 — Mar 18, 2019

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ First time:
$ cabal new-install dhall-json
```

Dhall 3.0.0 is required, so make sure that the previous command installed
dhall-json >= 1.2.4.
Dhall 9.0.0 is required, so make sure that the previous command installed
dhall-json >= 1.4.0.

To generate `hlint` file:

Expand Down
11 changes: 7 additions & 4 deletions hlint/hlint.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ in let warnSimple = warn.warnSimple
in let warnNote = warn.warnNote
in let hintNote = warn.hintNote
in let hintNoteOp = warn.hintNoteOp
in let rule = constructors ./Rule.dhall
in [ rule.Arguments { arguments =
in let Rule = ./Rule.dhall
in [ Rule.Arguments { arguments =
[ "-XConstraintKinds"
, "-XDeriveGeneric"
, "-XGeneralizedNewtypeDeriving"
Expand All @@ -25,9 +25,9 @@ in [ rule.Arguments { arguments =
-- Ignore
-------------
-- There's no 'head' in Relude
, rule.Ignore {ignore = {name = "Use head"}}
, Rule.Ignore {ignore = {name = "Use head"}}
-- We have 'whenJust' for this
, rule.Ignore {ignore = {name = "Use Foldable.forM_"}}
, Rule.Ignore {ignore = {name = "Use Foldable.forM_"}}

-------------
-- Relude specific
Expand Down Expand Up @@ -805,6 +805,9 @@ in [ rule.Arguments { arguments =

-- String
, warnReexport "ByteString" "Data.ByteString"
, warnReexport "ShortByteString" "Data.ByteString.Short"
, warnReexport "toShort" "Data.ByteString.Short"
, warnReexport "fromShort" "Data.ByteString.Short"
, warnReexport "IsString" "Data.String"
, warnReexport "fromString" "Data.String"
, warnReexport "Text" "Data.Text"
Expand Down
13 changes: 6 additions & 7 deletions hlint/warn.dhall
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
let Rule = ./Rule.dhall
in let rule = constructors Rule
in let warnSimple
: Text -> Text -> Rule
= \(lhsTxt : Text) -> \(rhsTxt : Text) ->
rule.Warn
Rule.Warn
{ warn =
{ name = None Text
, lhs = "${lhsTxt}"
Expand All @@ -15,7 +14,7 @@ in let warnSimple
in let warnNote
: Text -> Text -> Text -> Rule
= \(lhsTxt : Text) -> \(rhsTxt : Text) -> \(n : Text) ->
rule.Warn {warn =
Rule.Warn {warn =
{ name = None Text
, lhs = "${lhsTxt}"
, rhs = "${rhsTxt}"
Expand All @@ -26,7 +25,7 @@ in let warnNote
in let warnReexport
: Text -> Text -> Rule
= \(f : Text) -> \(mod : Text) ->
rule.Warn
Rule.Warn
{ warn =
{ name = Some "Use '${f}' from Relude"
, lhs = "${mod}.${f}"
Expand All @@ -37,7 +36,7 @@ in let warnReexport

in let warnReexportOp : Text -> Text -> Rule
= \(f : Text) -> \(mod : Text) ->
rule.Warn
Rule.Warn
{ warn =
{ name = Some "Use '${f}' from Relude"
, lhs = "(${mod}.${f})"
Expand All @@ -49,7 +48,7 @@ in let warnReexportOp : Text -> Text -> Rule
in let warnLifted
: Text -> Text -> Rule
= \(f : Text) -> \(args : Text) ->
rule.Warn
Rule.Warn
{ warn =
{ name = Some "'liftIO' is not needed"
, lhs = "(liftIO (${f} ${args}))"
Expand All @@ -61,7 +60,7 @@ in let warnLifted
in let hintNote
: Text -> Text -> Text -> Rule
= \(lhsTxt : Text) -> \(rhsTxt : Text) -> \(n : Text) ->
rule.Hint
Rule.Hint
{ hint =
{ lhs = "${lhsTxt}"
, rhs = "${rhsTxt}"
Expand Down
16 changes: 16 additions & 0 deletions src/Relude/Container/One.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import qualified Data.Sequence as SEQ

import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Short as SBS

import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
Expand All @@ -47,6 +48,7 @@ import qualified Data.Set as Set
-- >>> import qualified Data.HashMap.Strict as HashMap
-- >>> import qualified Data.Text as Text
-- >>> import qualified Data.ByteString as ByteString
-- >>> import qualified Data.ByteString.Short as ShortByteString
-- >>> import qualified Data.Text.Lazy as LText
-- >>> import qualified Data.ByteString.Lazy as LByteString

Expand Down Expand Up @@ -177,6 +179,20 @@ instance One BSL.ByteString where
one = BSL.singleton
{-# INLINE one #-}

{- | Create singleton 'SBS.ShortByteString'.
>>> one 97 :: ShortByteString
"a"
prop> ShortByteString.length (one x) == 1
-}
instance One SBS.ShortByteString where
type OneItem SBS.ShortByteString = Word8

one :: Word8 -> SBS.ShortByteString
one x = SBS.pack [x]
{-# INLINE one #-}

-- Maps

{- | Create singleton 'Map' from key-value pair.
Expand Down
42 changes: 41 additions & 1 deletion src/Relude/String/Conversion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import Data.Function (id, (.))
import Data.String (String)

import Relude.Functor ((<$>))
import Relude.String.Reexport (ByteString, IsString, Read, Text, fromString)
import Relude.String.Reexport (ByteString, IsString, Read, ShortByteString, Text, fromShort,
fromString, toShort)

import qualified Data.ByteString.Lazy as LB
import qualified Data.Text as T
Expand Down Expand Up @@ -177,6 +178,45 @@ instance ConvertUtf8 LText LByteString where
decodeUtf8Strict = LT.decodeUtf8'
{-# INLINE decodeUtf8Strict #-}

instance ConvertUtf8 String ShortByteString where
encodeUtf8 :: String -> ShortByteString
encodeUtf8 = toShort . encodeUtf8
{-# INLINE encodeUtf8 #-}

decodeUtf8 :: ShortByteString -> String
decodeUtf8 = decodeUtf8 . fromShort
{-# INLINE decodeUtf8 #-}

decodeUtf8Strict :: ShortByteString -> Either T.UnicodeException String
decodeUtf8Strict = decodeUtf8Strict . fromShort
{-# INLINE decodeUtf8Strict #-}

instance ConvertUtf8 Text ShortByteString where
encodeUtf8 :: Text -> ShortByteString
encodeUtf8 = toShort . encodeUtf8
{-# INLINE encodeUtf8 #-}

decodeUtf8 :: ShortByteString -> Text
decodeUtf8 = decodeUtf8 . fromShort
{-# INLINE decodeUtf8 #-}

decodeUtf8Strict :: ShortByteString -> Either T.UnicodeException Text
decodeUtf8Strict = decodeUtf8Strict . fromShort
{-# INLINE decodeUtf8Strict #-}

instance ConvertUtf8 LText ShortByteString where
encodeUtf8 :: LText -> ShortByteString
encodeUtf8 = toShort . encodeUtf8
{-# INLINE encodeUtf8 #-}

decodeUtf8 :: ShortByteString -> LText
decodeUtf8 = decodeUtf8 . fromShort
{-# INLINE decodeUtf8 #-}

decodeUtf8Strict :: ShortByteString -> Either T.UnicodeException LText
decodeUtf8Strict = decodeUtf8Strict . fromShort
{-# INLINE decodeUtf8Strict #-}

-- | Type class for converting other strings to 'T.Text'.
class ToText a where
toText :: a -> Text
Expand Down
11 changes: 9 additions & 2 deletions src/Relude/String/Reexport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Copyright: (c) 2016 Stephen Diehl
License: MIT
Maintainer: Kowainik <xrom.xkov@gmail.com>
Reexports functions to work with 'Text' and 'ByteString' types.
Reexports functions to work with 'Text', 'ByteString'
and 'ShortByteString' types.
-}

module Relude.String.Reexport
Expand All @@ -19,10 +20,16 @@ module Relude.String.Reexport
, module Text.Read

-- * ByteString
, module Data.ByteString
, ByteString

-- * ShortByteString
, ShortByteString
, toShort
, fromShort
) where

import Data.ByteString (ByteString)
import Data.ByteString.Short (ShortByteString, fromShort, toShort)
import Data.String (IsString (..), String)
import Data.Text (Text, lines, unlines, unwords, words)
import Data.Text.Encoding (decodeUtf8', decodeUtf8With)
Expand Down

0 comments on commit a6e670f

Please sign in to comment.