{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

-- | This module defines some example "do-" encodings
-- 
-- See "Examples.TypedEncoding.Overview" for usage examples.
-- 
-- (moved from @Data.TypedEncoding.Instances.Do.Sample@)
--
-- @since 0.5.0.0
module Examples.TypedEncoding.Instances.Do.Sample where

import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.ByteString as B

import           Data.Char

import           Data.TypedEncoding.Instances.Support
import           Data.TypedEncoding.Instances.Support.Unsafe
import           Examples.TypedEncoding.Util (HasA (..))
-- |
-- @since 0.3.0.0 
instance Applicative f => Encode f "do-UPPER" "do-UPPER" c T.Text where
    encoding :: Encoding f "do-UPPER" "do-UPPER" c Text
encoding = (Text -> Text) -> Encoding f "do-UPPER" (AlgNm "do-UPPER") c Text
forall (nm :: Symbol) (f :: * -> *) c str.
Applicative f =>
(str -> str) -> Encoding f nm (AlgNm nm) c str
_implEncodingP Text -> Text
T.toUpper

instance (RecreateErr f, Applicative f) => Validate f "do-UPPER" "do-UPPER" c T.Text where
    validation :: Validation f "do-UPPER" "do-UPPER" c Text
validation = (forall (xs :: [Symbol]).
 Enc @[Symbol] ((':) @Symbol "do-UPPER" xs) c Text
 -> f (Enc @[Symbol] xs c Text))
-> Validation f "do-UPPER" (AlgNm "do-UPPER") c Text
forall (f :: * -> *) (nm :: Symbol) conf str.
(forall (xs :: [Symbol]).
 Enc @[Symbol] ((':) @Symbol nm xs) conf str
 -> f (Enc @[Symbol] xs conf str))
-> Validation f nm (AlgNm nm) conf str
_mkValidation ((forall (xs :: [Symbol]).
  Enc @[Symbol] ((':) @Symbol "do-UPPER" xs) c Text
  -> f (Enc @[Symbol] xs c Text))
 -> Validation f "do-UPPER" (AlgNm "do-UPPER") c Text)
-> (forall (xs :: [Symbol]).
    Enc @[Symbol] ((':) @Symbol "do-UPPER" xs) c Text
    -> f (Enc @[Symbol] xs c Text))
-> Validation f "do-UPPER" (AlgNm "do-UPPER") c Text
forall a b. (a -> b) -> a -> b
$
                          (Text -> f Text)
-> Enc @[Symbol] ((':) @Symbol "do-UPPER" xs) c Text
-> f (Enc @[Symbol] xs c Text)
forall k1 k2 (f :: * -> *) str (enc1 :: k1) conf (enc2 :: k2).
Functor f =>
(str -> f str)
-> Enc @k1 enc1 conf str -> f (Enc @k2 enc2 conf str)
implTranF (forall (x :: Symbol) (f :: * -> *) err a.
(RecreateErr @* f, Applicative f, Show err, KnownSymbol x) =>
Either err a -> f a
forall (f :: * -> *) err a.
(RecreateErr @* f, Applicative f, Show err,
 KnownSymbol "do-UPPER") =>
Either err a -> f a
asRecreateErr @"do-UPPER" (Either [Char] Text -> f Text)
-> (Text -> Either [Char] Text) -> Text -> f Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\Text
t -> 
                                 let (Text
g,Text
b) = (Char -> Bool) -> Text -> (Text, Text)
T.partition Char -> Bool
isUpper Text
t
                                 in if Text -> Bool
T.null Text
b
                                    then Text -> Either [Char] Text
forall a b. b -> Either a b
Right Text
t
                                    else [Char] -> Either [Char] Text
forall a b. a -> Either a b
Left ([Char] -> Either [Char] Text) -> [Char] -> Either [Char] Text
forall a b. (a -> b) -> a -> b
$ [Char]
"Found not upper case chars " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
b)
                           )

instance Applicative f => Encode f "do-UPPER" "do-UPPER" c TL.Text where
    encoding :: Encoding f "do-UPPER" "do-UPPER" c Text
encoding = (Text -> Text) -> Encoding f "do-UPPER" (AlgNm "do-UPPER") c Text
forall (nm :: Symbol) (f :: * -> *) c str.
Applicative f =>
(str -> str) -> Encoding f nm (AlgNm nm) c str
_implEncodingP Text -> Text
TL.toUpper


-- |
-- @since 0.3.0.0 
instance Applicative f => Encode f "do-lower" "do-lower" c T.Text where
    encoding :: Encoding f "do-lower" "do-lower" c Text
encoding = (Text -> Text) -> Encoding f "do-lower" (AlgNm "do-lower") c Text
forall (nm :: Symbol) (f :: * -> *) c str.
Applicative f =>
(str -> str) -> Encoding f nm (AlgNm nm) c str
_implEncodingP Text -> Text
T.toLower   

instance Applicative f => Encode f "do-lower" "do-lower" c TL.Text where
    encoding :: Encoding f "do-lower" "do-lower" c Text
encoding = (Text -> Text) -> Encoding f "do-lower" (AlgNm "do-lower") c Text
forall (nm :: Symbol) (f :: * -> *) c str.
Applicative f =>
(str -> str) -> Encoding f nm (AlgNm nm) c str
_implEncodingP Text -> Text
TL.toLower 

-- |
-- @since 0.3.0.0 
instance Applicative f => Encode f "do-Title" "do-Title" c T.Text where
    encoding :: Encoding f "do-Title" "do-Title" c Text
encoding = (Text -> Text) -> Encoding f "do-Title" (AlgNm "do-Title") c Text
forall (nm :: Symbol) (f :: * -> *) c str.
Applicative f =>
(str -> str) -> Encoding f nm (AlgNm nm) c str
_implEncodingP Text -> Text
T.toTitle   

instance Applicative f => Encode f "do-Title" "do-Title" c TL.Text where
    encoding :: Encoding f "do-Title" "do-Title" c Text
encoding = (Text -> Text) -> Encoding f "do-Title" (AlgNm "do-Title") c Text
forall (nm :: Symbol) (f :: * -> *) c str.
Applicative f =>
(str -> str) -> Encoding f nm (AlgNm nm) c str
_implEncodingP Text -> Text
TL.toTitle   

-- |
-- @since 0.3.0.0 
instance Applicative f => Encode f "do-reverse" "do-reverse" c T.Text where
    encoding :: Encoding f "do-reverse" "do-reverse" c Text
encoding = (Text -> Text)
-> Encoding f "do-reverse" (AlgNm "do-reverse") c Text
forall (nm :: Symbol) (f :: * -> *) c str.
Applicative f =>
(str -> str) -> Encoding f nm (AlgNm nm) c str
_implEncodingP Text -> Text
T.reverse 
instance Applicative f => Encode f "do-reverse" "do-reverse" c TL.Text where
    encoding :: Encoding f "do-reverse" "do-reverse" c Text
encoding = (Text -> Text)
-> Encoding f "do-reverse" (AlgNm "do-reverse") c Text
forall (nm :: Symbol) (f :: * -> *) c str.
Applicative f =>
(str -> str) -> Encoding f nm (AlgNm nm) c str
_implEncodingP Text -> Text
TL.reverse    

-- |
-- @since 0.1.0.0
newtype SizeLimit = SizeLimit {SizeLimit -> Int
unSizeLimit :: Int} deriving (SizeLimit -> SizeLimit -> Bool
(SizeLimit -> SizeLimit -> Bool)
-> (SizeLimit -> SizeLimit -> Bool) -> Eq SizeLimit
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SizeLimit -> SizeLimit -> Bool
$c/= :: SizeLimit -> SizeLimit -> Bool
== :: SizeLimit -> SizeLimit -> Bool
$c== :: SizeLimit -> SizeLimit -> Bool
Eq, Int -> SizeLimit -> [Char] -> [Char]
[SizeLimit] -> [Char] -> [Char]
SizeLimit -> [Char]
(Int -> SizeLimit -> [Char] -> [Char])
-> (SizeLimit -> [Char])
-> ([SizeLimit] -> [Char] -> [Char])
-> Show SizeLimit
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
showList :: [SizeLimit] -> [Char] -> [Char]
$cshowList :: [SizeLimit] -> [Char] -> [Char]
show :: SizeLimit -> [Char]
$cshow :: SizeLimit -> [Char]
showsPrec :: Int -> SizeLimit -> [Char] -> [Char]
$cshowsPrec :: Int -> SizeLimit -> [Char] -> [Char]
Show)

-- |
-- @since 0.3.0.0 
instance (HasA SizeLimit c, Applicative f) => Encode f "do-size-limit" "do-size-limit" c T.Text where
    encoding :: Encoding f "do-size-limit" "do-size-limit" c Text
encoding = (c -> Text -> Text)
-> Encoding f "do-size-limit" (AlgNm "do-size-limit") c Text
forall (nm :: Symbol) (f :: * -> *) c str.
Applicative f =>
(c -> str -> str) -> Encoding f nm (AlgNm nm) c str
_implEncodingConfP (Int -> Text -> Text
T.take (Int -> Text -> Text) -> (c -> Int) -> c -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SizeLimit -> Int
unSizeLimit (SizeLimit -> Int) -> (c -> SizeLimit) -> c -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasA SizeLimit c => c -> SizeLimit
forall a c. HasA a c => c -> a
has @SizeLimit) 
instance (HasA SizeLimit c, Applicative f) => Encode f "do-size-limit" "do-size-limit" c B.ByteString where
    encoding :: Encoding f "do-size-limit" "do-size-limit" c ByteString
encoding = (c -> ByteString -> ByteString)
-> Encoding f "do-size-limit" (AlgNm "do-size-limit") c ByteString
forall (nm :: Symbol) (f :: * -> *) c str.
Applicative f =>
(c -> str -> str) -> Encoding f nm (AlgNm nm) c str
_implEncodingConfP (Int -> ByteString -> ByteString
B.take (Int -> ByteString -> ByteString)
-> (c -> Int) -> c -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SizeLimit -> Int
unSizeLimit (SizeLimit -> Int) -> (c -> SizeLimit) -> c -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.  forall c. HasA SizeLimit c => c -> SizeLimit
forall a c. HasA a c => c -> a
has @SizeLimit)