Skip to content

Commit

Permalink
Add default ToText instance using Buildable
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl authored and Anviking committed Jun 18, 2021
1 parent ca9cbed commit 61fbe16
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/text-class/src/Data/Text/Class.hs
@@ -1,3 +1,4 @@
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
Expand Down Expand Up @@ -54,8 +55,10 @@ import Data.Word
( Word32, Word64 )
import Data.Word.Odd
( Word31 )
import Fmt
( Buildable )
import Formatting
( builder, sformat )
import Formatting.Buildable
( Buildable (..) )
import GHC.Generics
( Generic )
import Numeric.Natural
Expand All @@ -80,6 +83,9 @@ class ToText a where
-- | Encode the specified value as text.
toText :: a -> Text

default toText :: Buildable a => a -> Text
toText = sformat builder . build

-- | Defines a textual decoding for a type.
class FromText a where
-- | Decode the specified text as a value.
Expand Down
2 changes: 1 addition & 1 deletion lib/text-class/text-class.cabal
Expand Up @@ -31,7 +31,7 @@ library
base
, casing
, extra
, fmt
, formatting
, text
, time
, hspec
Expand Down

0 comments on commit 61fbe16

Please sign in to comment.