{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

module Data.StringVariants.NullableNonEmptyText
  ( -- Safe to construct, so we can export the contents
    NullableNonEmptyText (..),

    -- * Constructing
    mkNonEmptyTextWithTruncate,
    compileNullableNonEmptyText,
    IsNullableNonEmptyText,
    literalNullableNonEmptyText,
    mkNullableNonEmptyText,
    parseNullableNonEmptyText,
    nullNonEmptyText,

    -- * Conversion
    maybeTextToTruncateNullableNonEmptyText,
    nonEmptyTextToNullable,
    maybeNonEmptyTextToNullable,
    nullableNonEmptyTextToMaybeText,
    nullableNonEmptyTextToMaybeNonEmptyText,
    fromMaybeNullableText,

    -- * Functions
    isNullNonEmptyText,
  )
where

import Control.Monad
import Data.Aeson
import Data.Aeson qualified as J
import Data.Aeson.Key qualified as J
import Data.Aeson.Types qualified as J
import Data.Data (Proxy (..))
import Data.Maybe (fromMaybe)
import Data.StringVariants.NonEmptyText
import Data.StringVariants.Util
import Data.Text (Text)
import Data.Text qualified as T
import GHC.Generics (Generic)
import GHC.TypeLits (KnownNat, KnownSymbol, Nat, Symbol, natVal)
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Language.Haskell.TH.Syntax (Lift (..))
import Prelude

-- | Newtype wrapper around Maybe NonEmptyText that converts empty string to 'Nothing'.
--
--   @'NullableNonEmptyText' n@ is used in API types to represent optional text fields when you do not want an empty string to fail to parse.
--   Like 'NonEmptyText', the payload 'Text' is guaranteed to be non-empty, within the character limit, and stripped of whitespace.
--   Unlike 'NonEmptyText', it will successfully parse empty strings as 'nullNonEmptyText'.
--
--   Since Aeson version 2.2, fields of this type maybe be missing, @null@, or empty without failing to parse.
--   Avoid using @Maybe (NullableNonEmptyText n)@ in API types, since it creates unnecessary edge cases that complicate the code.
--
--   __NB:__ When using a version of Aeson prior to 2.2, you /must/ use @Maybe (NullableNonEmptyText n)@ if you want to allow missing or null fields to parse.
--
--   @
--   data Person = Person
--     { name :: 'NonEmptyText' 50
--     , catchphrase :: 'NullableNonEmptyText' 500
--     }
--   @
--
--   With this type definition, these four JSON objects below are valid and parse as @Person "Daniel" nullNonEmptyText@.
--
--   > {"name": "Daniel"}
--   > {"name": "Daniel", catchphrase: null}
--   > {"name": "Daniel", catchphrase: ""}
--   > {"name": "Daniel", catchphrase: " "}
--
--   These two JSON objects parses as @Person "Daniel" (mkNullableNonEmptyText "Yabba-Dabba Do!")@
--
--   > {"name": "Daniel", catchphrase: "Yabba-Dabba Do!"}
--   > {"name": "Daniel", catchphrase: "    Yabba-Dabba Do!   "}
--
--   Use 'nullableNonEmptyTextToMaybeNonEmptyText' to extract @Maybe (NonEmptyText n)@ from @NullableNonEmptyText n@.
newtype NullableNonEmptyText n = NullableNonEmptyText (Maybe (NonEmptyText n))
  deriving stock ((forall x.
 NullableNonEmptyText n -> Rep (NullableNonEmptyText n) x)
-> (forall x.
    Rep (NullableNonEmptyText n) x -> NullableNonEmptyText n)
-> Generic (NullableNonEmptyText n)
forall (n :: Nat) x.
Rep (NullableNonEmptyText n) x -> NullableNonEmptyText n
forall (n :: Nat) x.
NullableNonEmptyText n -> Rep (NullableNonEmptyText n) x
forall x. Rep (NullableNonEmptyText n) x -> NullableNonEmptyText n
forall x. NullableNonEmptyText n -> Rep (NullableNonEmptyText n) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall (n :: Nat) x.
NullableNonEmptyText n -> Rep (NullableNonEmptyText n) x
from :: forall x. NullableNonEmptyText n -> Rep (NullableNonEmptyText n) x
$cto :: forall (n :: Nat) x.
Rep (NullableNonEmptyText n) x -> NullableNonEmptyText n
to :: forall x. Rep (NullableNonEmptyText n) x -> NullableNonEmptyText n
Generic, Int -> NullableNonEmptyText n -> ShowS
[NullableNonEmptyText n] -> ShowS
NullableNonEmptyText n -> [Char]
(Int -> NullableNonEmptyText n -> ShowS)
-> (NullableNonEmptyText n -> [Char])
-> ([NullableNonEmptyText n] -> ShowS)
-> Show (NullableNonEmptyText n)
forall (n :: Nat). Int -> NullableNonEmptyText n -> ShowS
forall (n :: Nat). [NullableNonEmptyText n] -> ShowS
forall (n :: Nat). NullableNonEmptyText n -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall (n :: Nat). Int -> NullableNonEmptyText n -> ShowS
showsPrec :: Int -> NullableNonEmptyText n -> ShowS
$cshow :: forall (n :: Nat). NullableNonEmptyText n -> [Char]
show :: NullableNonEmptyText n -> [Char]
$cshowList :: forall (n :: Nat). [NullableNonEmptyText n] -> ShowS
showList :: [NullableNonEmptyText n] -> ShowS
Show, ReadPrec [NullableNonEmptyText n]
ReadPrec (NullableNonEmptyText n)
Int -> ReadS (NullableNonEmptyText n)
ReadS [NullableNonEmptyText n]
(Int -> ReadS (NullableNonEmptyText n))
-> ReadS [NullableNonEmptyText n]
-> ReadPrec (NullableNonEmptyText n)
-> ReadPrec [NullableNonEmptyText n]
-> Read (NullableNonEmptyText n)
forall (n :: Nat). ReadPrec [NullableNonEmptyText n]
forall (n :: Nat). ReadPrec (NullableNonEmptyText n)
forall (n :: Nat). Int -> ReadS (NullableNonEmptyText n)
forall (n :: Nat). ReadS [NullableNonEmptyText n]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall (n :: Nat). Int -> ReadS (NullableNonEmptyText n)
readsPrec :: Int -> ReadS (NullableNonEmptyText n)
$creadList :: forall (n :: Nat). ReadS [NullableNonEmptyText n]
readList :: ReadS [NullableNonEmptyText n]
$creadPrec :: forall (n :: Nat). ReadPrec (NullableNonEmptyText n)
readPrec :: ReadPrec (NullableNonEmptyText n)
$creadListPrec :: forall (n :: Nat). ReadPrec [NullableNonEmptyText n]
readListPrec :: ReadPrec [NullableNonEmptyText n]
Read, (forall (m :: * -> *). Quote m => NullableNonEmptyText n -> m Exp)
-> (forall (m :: * -> *).
    Quote m =>
    NullableNonEmptyText n -> Code m (NullableNonEmptyText n))
-> Lift (NullableNonEmptyText n)
forall (n :: Nat) (m :: * -> *).
Quote m =>
NullableNonEmptyText n -> m Exp
forall (n :: Nat) (m :: * -> *).
Quote m =>
NullableNonEmptyText n -> Code m (NullableNonEmptyText n)
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => NullableNonEmptyText n -> m Exp
forall (m :: * -> *).
Quote m =>
NullableNonEmptyText n -> Code m (NullableNonEmptyText n)
$clift :: forall (n :: Nat) (m :: * -> *).
Quote m =>
NullableNonEmptyText n -> m Exp
lift :: forall (m :: * -> *). Quote m => NullableNonEmptyText n -> m Exp
$cliftTyped :: forall (n :: Nat) (m :: * -> *).
Quote m =>
NullableNonEmptyText n -> Code m (NullableNonEmptyText n)
liftTyped :: forall (m :: * -> *).
Quote m =>
NullableNonEmptyText n -> Code m (NullableNonEmptyText n)
Lift)
  deriving newtype (NullableNonEmptyText n -> NullableNonEmptyText n -> Bool
(NullableNonEmptyText n -> NullableNonEmptyText n -> Bool)
-> (NullableNonEmptyText n -> NullableNonEmptyText n -> Bool)
-> Eq (NullableNonEmptyText n)
forall (n :: Nat).
NullableNonEmptyText n -> NullableNonEmptyText n -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall (n :: Nat).
NullableNonEmptyText n -> NullableNonEmptyText n -> Bool
== :: NullableNonEmptyText n -> NullableNonEmptyText n -> Bool
$c/= :: forall (n :: Nat).
NullableNonEmptyText n -> NullableNonEmptyText n -> Bool
/= :: NullableNonEmptyText n -> NullableNonEmptyText n -> Bool
Eq)

mkNullableNonEmptyText :: forall n. (KnownNat n, 1 <= n) => Text -> Maybe (NullableNonEmptyText n)
mkNullableNonEmptyText :: forall (n :: Nat).
(KnownNat n, 1 <= n) =>
Text -> Maybe (NullableNonEmptyText n)
mkNullableNonEmptyText Text
t
  | Text -> Int -> Ordering
T.compareLength Text
stripped (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @n)) Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT = Maybe (NullableNonEmptyText n)
forall a. Maybe a
Nothing -- we can't store text that is too long
  | Bool
otherwise = NullableNonEmptyText n -> Maybe (NullableNonEmptyText n)
forall a. a -> Maybe a
Just (NullableNonEmptyText n -> Maybe (NullableNonEmptyText n))
-> NullableNonEmptyText n -> Maybe (NullableNonEmptyText n)
forall a b. (a -> b) -> a -> b
$ Maybe (NonEmptyText n) -> NullableNonEmptyText n
forall (n :: Nat). Maybe (NonEmptyText n) -> NullableNonEmptyText n
NullableNonEmptyText (Maybe (NonEmptyText n) -> NullableNonEmptyText n)
-> Maybe (NonEmptyText n) -> NullableNonEmptyText n
forall a b. (a -> b) -> a -> b
$ Text -> Maybe (NonEmptyText n)
forall (n :: Nat).
(KnownNat n, 1 <= n) =>
Text -> Maybe (NonEmptyText n)
mkNonEmptyText Text
t
  where
    stripped :: Text
stripped = (Char -> Bool) -> Text -> Text
T.filter (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\NUL') (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
T.strip Text
t

nullNonEmptyText :: NullableNonEmptyText n
nullNonEmptyText :: forall (n :: Nat). NullableNonEmptyText n
nullNonEmptyText = Maybe (NonEmptyText n) -> NullableNonEmptyText n
forall (n :: Nat). Maybe (NonEmptyText n) -> NullableNonEmptyText n
NullableNonEmptyText Maybe (NonEmptyText n)
forall a. Maybe a
Nothing

isNullNonEmptyText :: NullableNonEmptyText n -> Bool
isNullNonEmptyText :: forall (n :: Nat). NullableNonEmptyText n -> Bool
isNullNonEmptyText = (NullableNonEmptyText n -> NullableNonEmptyText n -> Bool
forall a. Eq a => a -> a -> Bool
== NullableNonEmptyText n
forall (n :: Nat). NullableNonEmptyText n
nullNonEmptyText)

instance ToJSON (NullableNonEmptyText n) where
  toJSON :: NullableNonEmptyText n -> Value
toJSON (NullableNonEmptyText Maybe (NonEmptyText n)
t) = Maybe (NonEmptyText n) -> Value
forall a. ToJSON a => a -> Value
toJSON Maybe (NonEmptyText n)
t

  toEncoding :: NullableNonEmptyText n -> Encoding
toEncoding (NullableNonEmptyText Maybe (NonEmptyText n)
t) = Maybe (NonEmptyText n) -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding Maybe (NonEmptyText n)
t

#if MIN_VERSION_aeson(2,2,0)
  omitField (NullableNonEmptyText Nothing) = True
  omitField _ = False
#endif

instance (KnownNat n, 1 <= n) => FromJSON (NullableNonEmptyText n) where
  parseJSON :: Value -> Parser (NullableNonEmptyText n)
parseJSON = \case
    J.String Text
t -> case Text -> Maybe (NullableNonEmptyText n)
forall (n :: Nat).
(KnownNat n, 1 <= n) =>
Text -> Maybe (NullableNonEmptyText n)
mkNullableNonEmptyText Text
t of
      Just NullableNonEmptyText n
txt -> NullableNonEmptyText n -> Parser (NullableNonEmptyText n)
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NullableNonEmptyText n
txt
      Maybe (NullableNonEmptyText n)
Nothing -> [Char] -> Parser (NullableNonEmptyText n)
forall a. [Char] -> Parser a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Parser (NullableNonEmptyText n))
-> [Char] -> Parser (NullableNonEmptyText n)
forall a b. (a -> b) -> a -> b
$ [Char]
"Data/StringVariants/NullableNonEmptyText.hs: When trying to parse a NullableNonEmptyText, expected a String of length < " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Integer -> [Char]
forall a. Show a => a -> [Char]
show (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @n)) [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
", but received: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
t
    Value
J.Null -> NullableNonEmptyText n -> Parser (NullableNonEmptyText n)
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NullableNonEmptyText n -> Parser (NullableNonEmptyText n))
-> NullableNonEmptyText n -> Parser (NullableNonEmptyText n)
forall a b. (a -> b) -> a -> b
$ Maybe (NonEmptyText n) -> NullableNonEmptyText n
forall (n :: Nat). Maybe (NonEmptyText n) -> NullableNonEmptyText n
NullableNonEmptyText Maybe (NonEmptyText n)
forall a. Maybe a
Nothing
    Value
x -> [Char] -> Parser (NullableNonEmptyText n)
forall a. [Char] -> Parser a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Parser (NullableNonEmptyText n))
-> [Char] -> Parser (NullableNonEmptyText n)
forall a b. (a -> b) -> a -> b
$ [Char]
"Data/StringVariants/NullableNonEmptyText.hs: When trying to parse a NullableNonEmptyText, expected a String or Null, but received: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Value -> [Char]
forall a. Show a => a -> [Char]
show Value
x

#if MIN_VERSION_aeson(2,2,0)
  omittedField = Just (NullableNonEmptyText Nothing)
#endif

parseNullableNonEmptyText :: (KnownNat n, 1 <= n) => Text -> J.Object -> J.Parser (NullableNonEmptyText n)
parseNullableNonEmptyText :: forall (n :: Nat).
(KnownNat n, 1 <= n) =>
Text -> Object -> Parser (NullableNonEmptyText n)
parseNullableNonEmptyText Text
fieldName Object
obj = Object
obj Object -> Key -> Parser (Maybe (NullableNonEmptyText n))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Text -> Key
J.fromText Text
fieldName Parser (Maybe (NullableNonEmptyText n))
-> NullableNonEmptyText n -> Parser (NullableNonEmptyText n)
forall a. Parser (Maybe a) -> a -> Parser a
.!= NullableNonEmptyText n
forall (n :: Nat). NullableNonEmptyText n
nullNonEmptyText

fromMaybeNullableText :: Maybe (NullableNonEmptyText n) -> NullableNonEmptyText n
fromMaybeNullableText :: forall (n :: Nat).
Maybe (NullableNonEmptyText n) -> NullableNonEmptyText n
fromMaybeNullableText = NullableNonEmptyText n
-> Maybe (NullableNonEmptyText n) -> NullableNonEmptyText n
forall a. a -> Maybe a -> a
fromMaybe NullableNonEmptyText n
forall (n :: Nat). NullableNonEmptyText n
nullNonEmptyText

nonEmptyTextToNullable :: NonEmptyText n -> NullableNonEmptyText n
nonEmptyTextToNullable :: forall (n :: Nat). NonEmptyText n -> NullableNonEmptyText n
nonEmptyTextToNullable = Maybe (NonEmptyText n) -> NullableNonEmptyText n
forall (n :: Nat). Maybe (NonEmptyText n) -> NullableNonEmptyText n
NullableNonEmptyText (Maybe (NonEmptyText n) -> NullableNonEmptyText n)
-> (NonEmptyText n -> Maybe (NonEmptyText n))
-> NonEmptyText n
-> NullableNonEmptyText n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmptyText n -> Maybe (NonEmptyText n)
forall a. a -> Maybe a
Just

maybeNonEmptyTextToNullable :: Maybe (NonEmptyText n) -> NullableNonEmptyText n
maybeNonEmptyTextToNullable :: forall (n :: Nat). Maybe (NonEmptyText n) -> NullableNonEmptyText n
maybeNonEmptyTextToNullable Maybe (NonEmptyText n)
Nothing = NullableNonEmptyText n
forall (n :: Nat). NullableNonEmptyText n
nullNonEmptyText
maybeNonEmptyTextToNullable Maybe (NonEmptyText n)
jt = Maybe (NonEmptyText n) -> NullableNonEmptyText n
forall (n :: Nat). Maybe (NonEmptyText n) -> NullableNonEmptyText n
NullableNonEmptyText Maybe (NonEmptyText n)
jt

maybeTextToTruncateNullableNonEmptyText :: forall n. (KnownNat n, 1 <= n) => Maybe Text -> NullableNonEmptyText n
maybeTextToTruncateNullableNonEmptyText :: forall (n :: Nat).
(KnownNat n, 1 <= n) =>
Maybe Text -> NullableNonEmptyText n
maybeTextToTruncateNullableNonEmptyText Maybe Text
mText = Maybe (NonEmptyText n) -> NullableNonEmptyText n
forall (n :: Nat). Maybe (NonEmptyText n) -> NullableNonEmptyText n
NullableNonEmptyText (Maybe (NonEmptyText n) -> NullableNonEmptyText n)
-> Maybe (NonEmptyText n) -> NullableNonEmptyText n
forall a b. (a -> b) -> a -> b
$ Maybe Text
mText Maybe Text
-> (Text -> Maybe (NonEmptyText n)) -> Maybe (NonEmptyText n)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Maybe (NonEmptyText n)
forall (n :: Nat).
(KnownNat n, 1 <= n) =>
Text -> Maybe (NonEmptyText n)
mkNonEmptyText (Text -> Maybe (NonEmptyText n))
-> (Text -> Text) -> Text -> Maybe (NonEmptyText n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Text -> Text
T.take (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @n)))

nullableNonEmptyTextToMaybeNonEmptyText :: NullableNonEmptyText n -> Maybe (NonEmptyText n)
nullableNonEmptyTextToMaybeNonEmptyText :: forall (n :: Nat). NullableNonEmptyText n -> Maybe (NonEmptyText n)
nullableNonEmptyTextToMaybeNonEmptyText (NullableNonEmptyText Maybe (NonEmptyText n)
t) = Maybe (NonEmptyText n)
t

nullableNonEmptyTextToMaybeText :: NullableNonEmptyText n -> Maybe Text
nullableNonEmptyTextToMaybeText :: forall (n :: Nat). NullableNonEmptyText n -> Maybe Text
nullableNonEmptyTextToMaybeText (NullableNonEmptyText Maybe (NonEmptyText n)
t) = NonEmptyText n -> Text
forall (n :: Nat). NonEmptyText n -> Text
nonEmptyTextToText (NonEmptyText n -> Text) -> Maybe (NonEmptyText n) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NonEmptyText n)
t

compileNullableNonEmptyText :: Integer -> QuasiQuoter
compileNullableNonEmptyText :: Integer -> QuasiQuoter
compileNullableNonEmptyText Integer
n =
  QuasiQuoter
    { quoteExp :: [Char] -> Q Exp
quoteExp = [Char] -> Q Exp
compileNullableNonEmptyText'
    , quotePat :: [Char] -> Q Pat
quotePat = [Char] -> [Char] -> Q Pat
forall a. HasCallStack => [Char] -> a
error [Char]
"NullableNonEmptyText is not a pattern; use `nullableNonEmptyTextToMaybeText` instead"
    , quoteDec :: [Char] -> Q [Dec]
quoteDec = [Char] -> [Char] -> Q [Dec]
forall a. HasCallStack => [Char] -> a
error [Char]
"NullableNonEmptyText is not supported at top-level"
    , quoteType :: [Char] -> Q Type
quoteType = [Char] -> [Char] -> Q Type
forall a. HasCallStack => [Char] -> a
error [Char]
"NullableNonEmptyText is not supported as a type"
    }
  where
    compileNullableNonEmptyText' :: String -> Q Exp
    compileNullableNonEmptyText' :: [Char] -> Q Exp
compileNullableNonEmptyText' [Char]
s = Integer
-> Q Exp
-> (forall {n :: Nat} {proxy :: Nat -> *}.
    (KnownNat n, 1 <= n) =>
    proxy n -> Q Exp)
-> Q Exp
forall a.
Integer
-> a
-> (forall (n :: Nat) (proxy :: Nat -> *).
    (KnownNat n, 1 <= n) =>
    proxy n -> a)
-> a
usePositiveNat Integer
n Q Exp
errorMessage ((forall {n :: Nat} {proxy :: Nat -> *}.
  (KnownNat n, 1 <= n) =>
  proxy n -> Q Exp)
 -> Q Exp)
-> (forall {n :: Nat} {proxy :: Nat -> *}.
    (KnownNat n, 1 <= n) =>
    proxy n -> Q Exp)
-> Q Exp
forall a b. (a -> b) -> a -> b
$ \(proxy n
_ :: proxy n) ->
      case forall (n :: Nat).
(KnownNat n, 1 <= n) =>
Text -> Maybe (NullableNonEmptyText n)
mkNullableNonEmptyText @n ([Char] -> Text
T.pack [Char]
s) of
        Just NullableNonEmptyText n
txt -> [|$(NullableNonEmptyText n -> Q Exp
forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
forall (m :: * -> *). Quote m => NullableNonEmptyText n -> m Exp
lift NullableNonEmptyText n
txt)|]
        Maybe (NullableNonEmptyText n)
Nothing -> Q Exp
errorMessage
      where
        errorMessage :: Q Exp
errorMessage = [Char] -> Q Exp
forall a. [Char] -> Q a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Q Exp) -> [Char] -> Q Exp
forall a b. (a -> b) -> a -> b
$ [Char]
"Invalid NullableNonEmptyText. Needs to be < " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Integer -> [Char]
forall a. Show a => a -> [Char]
show (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" characters, and not entirely whitespace: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
s

type IsNullableNonEmptyText n s =
  ( KnownSymbol s
  , KnownNat n
  , SymbolNonEmpty s
  , SymbolWithNoSpaceAround s
  , SymbolNoLongerThan s n
  )

-- | This requires the text to be non-empty. For the empty text just use the constructor `NullableNonEmptyText Nothing`
literalNullableNonEmptyText :: forall (s :: Symbol) (n :: Nat). IsNullableNonEmptyText n s => NullableNonEmptyText n
literalNullableNonEmptyText :: forall (s :: Symbol) (n :: Nat).
IsNullableNonEmptyText n s =>
NullableNonEmptyText n
literalNullableNonEmptyText = Maybe (NonEmptyText n) -> NullableNonEmptyText n
forall (n :: Nat). Maybe (NonEmptyText n) -> NullableNonEmptyText n
NullableNonEmptyText (NonEmptyText n -> Maybe (NonEmptyText n)
forall a. a -> Maybe a
Just (forall (s :: Symbol) (n :: Nat).
IsNonEmptyText n s =>
NonEmptyText n
literalNonEmptyText @s @n))