module Binrep.Type.Text.Encoding.Utf32 where

import Binrep.Type.Text.Internal
import Binrep.Util.ByteOrder

import Refined
import Data.Typeable ( Typeable )

import Data.Text.Encoding qualified as Text
import Data.Text ( Text )

data Utf32 (end :: ByteOrder)

instance Encode (Utf32 BE) where encode' :: Text -> Bytes
encode' = Text -> Bytes
Text.encodeUtf32BE
instance Encode (Utf32 LE) where encode' :: Text -> Bytes
encode' = Text -> Bytes
Text.encodeUtf32LE

instance Decode (Utf32 BE) where decode :: Bytes -> Either String (AsText (Utf32 BE))
decode = (UnicodeException -> String)
-> (Bytes -> Either UnicodeException Text)
-> Bytes
-> Either String (AsText (Utf32 BE))
forall {k} (enc :: k) e.
(e -> String)
-> (Bytes -> Either e Text) -> Bytes -> Either String (AsText enc)
decodeText UnicodeException -> String
forall a. Show a => a -> String
show ((Bytes -> Either UnicodeException Text)
 -> Bytes -> Either String (AsText (Utf32 BE)))
-> (Bytes -> Either UnicodeException Text)
-> Bytes
-> Either String (AsText (Utf32 BE))
forall a b. (a -> b) -> a -> b
$ (Bytes -> Text) -> Bytes -> Either UnicodeException Text
wrapUnsafeDecoder Bytes -> Text
Text.decodeUtf32BE
instance Decode (Utf32 LE) where decode :: Bytes -> Either String (AsText (Utf32 LE))
decode = (UnicodeException -> String)
-> (Bytes -> Either UnicodeException Text)
-> Bytes
-> Either String (AsText (Utf32 LE))
forall {k} (enc :: k) e.
(e -> String)
-> (Bytes -> Either e Text) -> Bytes -> Either String (AsText enc)
decodeText UnicodeException -> String
forall a. Show a => a -> String
show ((Bytes -> Either UnicodeException Text)
 -> Bytes -> Either String (AsText (Utf32 LE)))
-> (Bytes -> Either UnicodeException Text)
-> Bytes
-> Either String (AsText (Utf32 LE))
forall a b. (a -> b) -> a -> b
$ (Bytes -> Text) -> Bytes -> Either UnicodeException Text
wrapUnsafeDecoder Bytes -> Text
Text.decodeUtf32LE

-- | Any 'Text' value is always valid UTF-32.
instance Typeable end => Predicate (Utf32 end) Text where validate :: Proxy (Utf32 end) -> Text -> Maybe RefineException
validate Proxy (Utf32 end)
_ Text
_ = Maybe RefineException
success