Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Thin wrapper around Text
, but without unsafe characters.
Synopsis
- unsafeChars :: [Char]
- data SafeText
- getText :: SafeText -> Text
- isSafeChar :: Char -> Bool
- isSafeText :: Text -> Bool
- isSafeString :: String -> Bool
- safePackText :: Text -> Maybe SafeText
- safePack :: String -> Maybe SafeText
- safeModify :: (Text -> Text) -> SafeText -> Maybe SafeText
- safeTextParser :: ReadS SafeText
Definition of unsafe
unsafeChars :: [Char] Source #
A list of all characters considered unsafe in our setting:
'\NUL'
and '\r'
.
A thin Text
wrapper with smart data constructor
SafeText
is only a thin newtype
wrapper around Text
. To ensure
that its characters are safe, the standard data constructor is hidden
from exports.
How to create SafeText
values?
- Use
safePack
orsafePackText
to create
values (orJust
SafeText
Nothing
if the given text or string was unsafe). - Use
SafeText
'sRead
instance together withread
orreadMaybe
. - Use
SafeText
'sIsString
instance together with theOverloadedStrings
extension to createSafeText
directly from string literals:"Hello, world" :: SafeText
Can SafeText
be used exactly like Text
?
No. There are very useful instances: Arbitrary
for property tests with
Test.QuickCheck, ToJSON
and FromJSON
for JSON stuff with Data.Aeson
and ToField
and FromField
for CSV stuff with Data.Csv (Cassava).
Also, there's safeModify
that allows Text
modifying functions to be
applied inside a SafeText
. But it seems to be a bit overkill to export
everything Data.Text has to offer.
A simple newtype
wrapper around Text
, but ensures the absence
of unsafe characters.
Instances
Arbitrary SafeText Source # | |
FromJSON SafeText Source # | |
Defined in LiBro.Data.SafeText | |
ToJSON SafeText Source # | |
IsString SafeText Source # | |
Defined in LiBro.Data.SafeText fromString :: String -> SafeText # | |
Read SafeText Source # | |
Show SafeText Source # | |
FromField SafeText Source # | |
Defined in LiBro.Data.SafeText parseField :: Field -> Parser SafeText # | |
ToField SafeText Source # | |
Defined in LiBro.Data.SafeText | |
Eq SafeText Source # | |
Safety checks
Explicit value creation
safeModify :: (Text -> Text) -> SafeText -> Maybe SafeText Source #
Safe application of a function that modifies Text
values.