{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.TypedEncoding.Instances.Restriction.D76 where
import Data.TypedEncoding.Instances.Support
import Data.TypedEncoding.Common.Class.Util.StringConstraints
import Data.TypedEncoding.Internal.Util (explainBool)
import Data.Char
newtype NonTextChar = NonTextChar Char deriving (Eq, Show)
instance Encode (Either EncodeEx) "r-UNICODE.D76" "r-UNICODE.D76" c Char where
encoding = encD76Char
instance Encode (Either EncodeEx) "r-UNICODE.D76" "r-UNICODE.D76" c String where
encoding = encD76
encD76Char :: Encoding (Either EncodeEx) "r-UNICODE.D76" "r-UNICODE.D76" c Char
encD76Char = _implEncodingEx (\c -> explainBool NonTextChar (c, nonTextChar c))
encD76 :: Encoding (Either EncodeEx) "r-UNICODE.D76" "r-UNICODE.D76" c String
encD76 = _implEncodingEx @"r-UNICODE.D76" encImpl
trustMe :: Applicative f => Encoding f "r-UNICODE.D76" "r-UNICODE.D76" c String
trustMe = _implEncodingP id
instance (Applicative f) => Decode f "r-UNICODE.D76" "r-UNICODE.D76" c str where
decoding = decAnyR
instance (RecreateErr f, Applicative f) => Validate f "r-UNICODE.D76" "r-UNICODE.D76" () String where
validation = validR encD76
nonTextChar :: Char -> Bool
nonTextChar c = x >= 55296 && x <= 57343
where x = ord c
encImpl :: String -> Either NonTextChar String
encImpl str = case find nonTextChar str of
Nothing -> Right str
Just ch -> Left $ NonTextChar ch