{-# LANGUAGE OverloadedStrings #-}
module Binrep.Type.Text.Encoding.Ascii where
import Binrep.Type.Text.Internal
import Binrep.Type.Text.Encoding.Utf8
import Rerefined.Predicate.Common
import Rerefined.Refine ( unsafeRefine )
import Data.Text qualified as Text
import Data.Text ( Text )
import Data.Text.Encoding qualified as Text
data Ascii
instance Predicate Ascii where type PredicateName d Ascii = "ASCII"
instance Refine Ascii Text where
validate :: Proxy# Ascii -> Text -> Maybe RefineFailure
validate Proxy# Ascii
p Text
t = Proxy# Ascii -> Bool -> Builder -> Maybe RefineFailure
forall {k} (p :: k).
(Predicate p, KnownPredicateName p) =>
Proxy# p -> Bool -> Builder -> Maybe RefineFailure
validateBool Proxy# Ascii
p (Text -> Bool
Text.isAscii Text
t) Builder
"not valid 7-bit ASCII"
instance Encode Ascii where encode' :: Text -> Bytes
encode' = forall enc. Encode enc => Text -> Bytes
forall {k} (enc :: k). Encode enc => Text -> Bytes
encode' @Utf8
instance Decode Ascii where
decode :: Bytes -> Either String (AsText Ascii)
decode Bytes
bs =
case Bytes -> Maybe Text
Text.decodeASCII' Bytes
bs of
Just Text
t -> AsText Ascii -> Either String (AsText Ascii)
forall a b. b -> Either a b
Right (AsText Ascii -> Either String (AsText Ascii))
-> AsText Ascii -> Either String (AsText Ascii)
forall a b. (a -> b) -> a -> b
$ Text -> AsText Ascii
forall {k} a (p :: k). a -> Refined p a
unsafeRefine Text
t
Maybe Text
Nothing -> String -> Either String (AsText Ascii)
forall a b. a -> Either a b
Left String
"not valid 7-bit ASCII"