{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- | Common /restriction/ "r-" instances
--
-- Renamed from @Data.TypedEncoding.Instances.Restriction.Common@ (v0.3)
--
-- @since 0.2.0.0
module Data.TypedEncoding.Instances.Restriction.Misc where

import           Data.Word
-- import           Data.Functor.Identity
import           Data.String
import           Data.Proxy
import           Text.Read

import           Data.TypedEncoding.Common.Class.IsStringR
import           Data.TypedEncoding.Instances.Support

-- $setup
-- >>> import qualified Data.Text as T


instance (IsStringR str) =>  Encode (Either EncodeEx) "r-Word8-decimal" "r-Word8-decimal" c str where
    encoding :: Encoding
  (Either EncodeEx) "r-Word8-decimal" "r-Word8-decimal" c str
encoding = Encoding
  (Either EncodeEx) "r-Word8-decimal" "r-Word8-decimal" c str
forall str c.
IsStringR str =>
Encoding
  (Either EncodeEx) "r-Word8-decimal" "r-Word8-decimal" c str
encWord8Dec
instance (Applicative f) => Decode f "r-Word8-decimal" "r-Word8-decimal" c str where
    decoding :: Decoding f "r-Word8-decimal" "r-Word8-decimal" c str
decoding = Decoding f "r-Word8-decimal" "r-Word8-decimal" c str
forall (r :: Symbol) (f :: * -> *) c str.
(Restriction r, Applicative f) =>
Decoding f r r c str
decAnyR
instance (IsStringR str) =>  Validate (Either RecreateEx) "r-Word8-decimal" "r-Word8-decimal" c str where
    validation :: Validation
  (Either RecreateEx) "r-Word8-decimal" "r-Word8-decimal" c str
validation = Encoding
  (Either EncodeEx) "r-Word8-decimal" "r-Word8-decimal" c str
-> Validation
     (Either RecreateEx) "r-Word8-decimal" "r-Word8-decimal" c str
forall (nm :: Symbol) (f :: * -> *) c str.
(Restriction nm, KnownSymbol nm, RecreateErr @* f,
 Applicative f) =>
Encoding (Either EncodeEx) nm nm c str -> Validation f nm nm c str
validR Encoding
  (Either EncodeEx) "r-Word8-decimal" "r-Word8-decimal" c str
forall str c.
IsStringR str =>
Encoding
  (Either EncodeEx) "r-Word8-decimal" "r-Word8-decimal" c str
encWord8Dec
instance (IsString str, Applicative f) => ToEncString f "r-Word8-decimal" "r-Word8-decimal" Word8 str where
    toEncF :: Word8
-> f (Enc
        @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str)
toEncF  Word8
i = Enc @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str
-> f (Enc
        @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Enc
   @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str
 -> f (Enc
         @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str))
-> Enc
     @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str
-> f (Enc
        @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str)
forall a b. (a -> b) -> a -> b
$ Proxy @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol))
-> ()
-> str
-> Enc
     @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str
forall k (nms :: k) conf str.
Proxy @k nms -> conf -> str -> Enc @k nms conf str
UnsafeMkEnc Proxy @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol))
forall k (t :: k). Proxy @k t
Proxy () (String -> str
forall a. IsString a => String -> a
fromString (String -> str) -> (Word8 -> String) -> Word8 -> str
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> String
forall a. Show a => a -> String
show (Word8 -> str) -> Word8 -> str
forall a b. (a -> b) -> a -> b
$ Word8
i)
instance (IsStringR str, UnexpectedDecodeErr f, Applicative f) => FromEncString f "r-Word8-decimal" "r-Word8-decimal" Word8 str where
    fromEncF :: Enc @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str
-> f Word8
fromEncF  = forall (x :: Symbol) (f :: * -> *) err a.
(KnownSymbol x, UnexpectedDecodeErr @* f, Applicative f,
 Show err) =>
Either err a -> f a
forall (f :: * -> *) err a.
(KnownSymbol "r-Word8-decimal", UnexpectedDecodeErr @* f,
 Applicative f, Show err) =>
Either err a -> f a
asUnexpected @"r-Word8-decimal" (Either String Word8 -> f Word8)
-> (Enc
      @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str
    -> Either String Word8)
-> Enc
     @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str
-> f Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Either String Word8
forall a. Read a => String -> Either String a
readEither (String -> Either String Word8)
-> (Enc
      @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str
    -> String)
-> Enc
     @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str
-> Either String Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. str -> String
forall a. IsStringR a => a -> String
toString (str -> String)
-> (Enc
      @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str
    -> str)
-> Enc
     @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Enc @[Symbol] ((':) @Symbol "r-Word8-decimal" ('[] @Symbol)) () str
-> str
forall k (enc :: k) conf str. Enc @k enc conf str -> str
getPayload

encWord8Dec :: (IsStringR str) => Encoding (Either EncodeEx) "r-Word8-decimal" "r-Word8-decimal" c str
encWord8Dec :: Encoding
  (Either EncodeEx) "r-Word8-decimal" "r-Word8-decimal" c str
encWord8Dec = (str -> Either String str)
-> Encoding
     (Either EncodeEx) "r-Word8-decimal" (AlgNm "r-Word8-decimal") c str
forall (nm :: Symbol) err c str.
(KnownSymbol nm, Show err) =>
(str -> Either err str)
-> Encoding (Either EncodeEx) nm (AlgNm nm) c str
_implEncodingEx (String -> str -> Either String str
forall a str.
(IsStringR str, Read a, Show a) =>
String -> str -> Either String str
verifyWithRead @Word8 String
"Word8-decimal")


instance (IsStringR str) =>  Encode (Either EncodeEx) "r-Int-decimal" "r-Int-decimal" c str where
    encoding :: Encoding (Either EncodeEx) "r-Int-decimal" "r-Int-decimal" c str
encoding = Encoding (Either EncodeEx) "r-Int-decimal" "r-Int-decimal" c str
forall str c.
IsStringR str =>
Encoding (Either EncodeEx) "r-Int-decimal" "r-Int-decimal" c str
encIntDec
instance (Applicative f) => Decode f "r-Int-decimal" "r-Int-decimal" c str where
    decoding :: Decoding f "r-Int-decimal" "r-Int-decimal" c str
decoding = Decoding f "r-Int-decimal" "r-Int-decimal" c str
forall (r :: Symbol) (f :: * -> *) c str.
(Restriction r, Applicative f) =>
Decoding f r r c str
decAnyR
instance (IsStringR str) =>  Validate (Either RecreateEx) "r-Int-decimal" "r-Int-decimal" c str where
    validation :: Validation
  (Either RecreateEx) "r-Int-decimal" "r-Int-decimal" c str
validation = Encoding (Either EncodeEx) "r-Int-decimal" "r-Int-decimal" c str
-> Validation
     (Either RecreateEx) "r-Int-decimal" "r-Int-decimal" c str
forall (nm :: Symbol) (f :: * -> *) c str.
(Restriction nm, KnownSymbol nm, RecreateErr @* f,
 Applicative f) =>
Encoding (Either EncodeEx) nm nm c str -> Validation f nm nm c str
validR Encoding (Either EncodeEx) "r-Int-decimal" "r-Int-decimal" c str
forall str c.
IsStringR str =>
Encoding (Either EncodeEx) "r-Int-decimal" "r-Int-decimal" c str
encIntDec
instance (IsString str, Applicative f) => ToEncString f "r-Int-decimal" "r-Int-decimal" Int str where
    toEncF :: Int
-> f (Enc
        @[Symbol] ((':) @Symbol "r-Int-decimal" ('[] @Symbol)) () str)
toEncF  Int
i = Enc @[Symbol] ((':) @Symbol "r-Int-decimal" ('[] @Symbol)) () str
-> f (Enc
        @[Symbol] ((':) @Symbol "r-Int-decimal" ('[] @Symbol)) () str)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Enc @[Symbol] ((':) @Symbol "r-Int-decimal" ('[] @Symbol)) () str
 -> f (Enc
         @[Symbol] ((':) @Symbol "r-Int-decimal" ('[] @Symbol)) () str))
-> Enc
     @[Symbol] ((':) @Symbol "r-Int-decimal" ('[] @Symbol)) () str
-> f (Enc
        @[Symbol] ((':) @Symbol "r-Int-decimal" ('[] @Symbol)) () str)
forall a b. (a -> b) -> a -> b
$ Proxy @[Symbol] ((':) @Symbol "r-Int-decimal" ('[] @Symbol))
-> ()
-> str
-> Enc
     @[Symbol] ((':) @Symbol "r-Int-decimal" ('[] @Symbol)) () str
forall k (nms :: k) conf str.
Proxy @k nms -> conf -> str -> Enc @k nms conf str
UnsafeMkEnc Proxy @[Symbol] ((':) @Symbol "r-Int-decimal" ('[] @Symbol))
forall k (t :: k). Proxy @k t
Proxy () (String -> str
forall a. IsString a => String -> a
fromString (String -> str) -> (Int -> String) -> Int -> str
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String
forall a. Show a => a -> String
show (Int -> str) -> Int -> str
forall a b. (a -> b) -> a -> b
$ Int
i)

encIntDec :: (IsStringR str) => Encoding (Either EncodeEx) "r-Int-decimal" "r-Int-decimal" c str
encIntDec :: Encoding (Either EncodeEx) "r-Int-decimal" "r-Int-decimal" c str
encIntDec =  (str -> Either String str)
-> Encoding
     (Either EncodeEx) "r-Int-decimal" (AlgNm "r-Int-decimal") c str
forall (nm :: Symbol) err c str.
(KnownSymbol nm, Show err) =>
(str -> Either err str)
-> Encoding (Either EncodeEx) nm (AlgNm nm) c str
_implEncodingEx (String -> str -> Either String str
forall a str.
(IsStringR str, Read a, Show a) =>
String -> str -> Either String str
verifyWithRead @Int String
"Int-decimal")





-- All instances of "r-Word8-decimal" are  @Show@ / @Read@ based