Skip to content

Commit 78d6039

Browse files
committed
Change the wording of the TypeErrors
1 parent 7459716 commit 78d6039

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

DESIGN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ You will notice that `display` itself is not part of the Typeclass. And indeed,
7878

7979
## Usage restrictions
8080

81-
### "🚫 You should not derive Display for function types!"
81+
### "🚫 You should not try to display function types!"
8282

8383
Sometimes, when using the library, you may encounter this message:
8484

8585
```
86-
• 🚫 You should not derive Display for function types!
86+
• 🚫 You should not try to display function types!
8787
💡 Write a 'newtype' wrapper that represents your domain more accurately.
8888
If you are not consciously trying to use `display` on a function,
8989
make sure that you are not missing an argument somewhere.
@@ -100,7 +100,7 @@ But these usages are extremely dependent on their domain of application.
100100
That is why it is best to wrap them in a newtype that can better
101101
express and enforce the domain.
102102
103-
### "🚫 You should not derive Display for ByteStrings!"
103+
### "🚫 You should not try to display ByteStrings!"
104104
105105
An arbitrary ByteStrings cannot be safely converted to text without prior knowledge of its encoding.
106106
As such, in order to avoid dangerously blind conversions, it is recommended to use a specialised

src/Data/Text/Display.hs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Maintainer : hecate@glitchbra.in
1818
Stability : stable
1919
20-
The Display typeclass provides a solution for user-facing output that does not have to abide by the rules of the Show typeclass.
20+
Use 'display' to produce user-facing text
2121
2222
-}
2323
module Data.Text.Display
@@ -135,8 +135,7 @@ class Display a where
135135
displayPrec _ = displayBuilder
136136

137137

138-
-- | @since 0.0.1.0
139-
-- Convert a value to a readable 'Text'.
138+
-- | Convert a value to a readable 'Text'.
140139
--
141140
-- === Examples
142141
-- >>> display 3
@@ -145,10 +144,11 @@ class Display a where
145144
-- >>> display True
146145
-- "True"
147146
--
147+
-- @since 0.0.1.0
148148
display :: Display a => a -> Text
149149
display a = TL.toStrict $ TB.toLazyText $ displayBuilder a
150150

151-
-- | 🚫 You should not derive Display for function types!
151+
-- | 🚫 You should not try to display functions!
152152
--
153153
-- 💡 Write a 'newtype' wrapper that represents your domain more accurately.
154154
-- If you are not consciously trying to use `display` on a function,
@@ -161,41 +161,42 @@ instance CannotDisplayBareFunctions => Display (a -> b) where
161161
-- | @since 0.0.1.0
162162
type family CannotDisplayBareFunctions :: Constraint where
163163
CannotDisplayBareFunctions = TypeError
164-
( 'Text "🚫 You should not derive Display for function types!" ':$$:
164+
( 'Text "🚫 You should not try to display functions!" ':$$:
165165
'Text "💡 Write a 'newtype' wrapper that represents your domain more accurately." ':$$:
166166
'Text " If you are not consciously trying to use `display` on a function," ':$$:
167167
'Text " make sure that you are not missing an argument somewhere."
168168
)
169169

170-
-- | 🚫 You should not derive Display for strict ByteStrings!
170+
-- | 🚫 You should not try to display strict ByteStrings!
171171
--
172172
-- 💡 Always provide an explicit encoding.
173-
-- Use 'decodeUtf8'' or 'decodeUtf8With' to convert from UTF-8
173+
-- Use 'Data.Text.Encoding.decudeUtf8'' or 'Data.Text.Encoding.decudeUtf8With' to convert from UTF-8
174174
--
175175
-- @since 0.0.1.0
176176
instance CannotDisplayByteStrings => Display ByteString where
177177
displayBuilder = undefined
178178

179-
-- | 🚫 You should not derive Display for lazy ByteStrings!
179+
-- | 🚫 You should not try to display lazy ByteStrings!
180180
--
181181
-- 💡 Always provide an explicit encoding.
182-
-- Use 'decodeUtf8'' or 'decodeUtf8With' to convert from UTF-8
182+
-- Use 'Data.Text.Encoding.decudeUtf8'' or 'Data.Text.Encoding.decudeUtf8With' to convert from UTF-8
183183
--
184184
-- @since 0.0.1.0
185185
instance CannotDisplayByteStrings => Display BL.ByteString where
186186
displayBuilder = undefined
187187

188188
type family CannotDisplayByteStrings :: Constraint where
189189
CannotDisplayByteStrings = TypeError
190-
( 'Text "🚫 You should not derive Display for ByteStrings!" ':$$:
190+
( 'Text "🚫 You should not try to display ByteStrings!" ':$$:
191191
'Text "💡 Always provide an explicit encoding" ':$$:
192-
'Text "Use 'decodeUtf8'' or 'decodeUtf8With' to convert from UTF-8"
192+
'Text "Use 'Data.Text.Encoding.decudeUtf8'' or 'Data.Text.Encoding.decudeUtf8With' to convert from UTF-8"
193193
)
194194

195-
-- | @since 0.0.1.0
196-
-- A utility function that surrounds the given 'Builder' with parentheses when the Bool parameter is True.
195+
-- | A utility function that surrounds the given 'Builder' with parentheses when the Bool parameter is True.
197196
-- Useful for writing instances that may require nesting. See the 'displayPrec' documentation for more
198197
-- information.
198+
--
199+
-- @since 0.0.1.0
199200
displayParen :: Bool -> Builder -> Builder
200201
displayParen b txt = if b then "(" <> txt <> ")" else txt
201202

@@ -210,12 +211,17 @@ displayParen b txt = if b then "(" <> txt <> ")" else txt
210211
--
211212
-- > display $ UserToken "7a01d2ce-31ff-11ec-8c10-5405db82c3cd"
212213
-- > "[REDACTED]"
213-
-- @since 0.0.1.0
214214
--
215+
-- @since 0.0.1.0
215216
newtype OpaqueInstance (str :: Symbol) (a :: Type) = Opaque a
216217

218+
-- | This wrapper allows you to create an opaque instance for your type,
219+
-- useful for redacting sensitive content like tokens or passwords.
220+
--
221+
-- @since 0.0.1.0
217222
instance KnownSymbol str => Display (OpaqueInstance str a) where
218223
displayBuilder _ = TB.fromString $ symbolVal (Proxy @str)
224+
219225
-- | This wrapper allows you to rely on a pre-existing 'Show' instance in order to
220226
-- derive 'Display' from it.
221227
--
@@ -379,11 +385,11 @@ instance (Display a, Display b, Display c, Display d) => Display (a, b, c, d) wh
379385
-- but the truth is that there are not any laws to ask of the consumer that are not already enforced
380386
-- by the type system and the internals of the `Data.Text.Internal.Text` type.
381387
--
382-
-- === "🚫 You should not derive Display for function types!"
388+
-- === "🚫 You should not try to display functions!"
383389
--
384390
-- Sometimes, when using the library, you may encounter this message:
385391
--
386-
-- > • 🚫 You should not derive Display for function types!
392+
-- > • 🚫 You should not try to display functions!
387393
-- > 💡 Write a 'newtype' wrapper that represents your domain more accurately.
388394
-- > If you are not consciously trying to use `display` on a function,
389395
-- > make sure that you are not missing an argument somewhere.
@@ -399,10 +405,10 @@ instance (Display a, Display b, Display c, Display d) => Display (a, b, c, d) wh
399405
-- That is why it is best to wrap them in a newtype that can better
400406
-- express and enforce the domain.
401407
--
402-
-- === "🚫 You should not derive Display for ByteStrings!"
408+
-- === "🚫 You should not try to display ByteStrings!"
403409
--
404410
-- An arbitrary ByteStrings cannot be safely converted to text without prior knowledge of its encoding.
405411
--
406412
-- As such, in order to avoid dangerously blind conversions, it is recommended to use a specialised
407-
-- function such as `decodeUtf8'` or `decodeUtf8With` if you wish to turn a UTF8-encoded ByteString
413+
-- function such as `Data.Text.Encoding.decudeUtf8'` or `Data.Text.Encoding.decudeUtf8With` if you wish to turn a UTF8-encoded ByteString
408414
-- to Text.

0 commit comments

Comments
 (0)