{-# LANGUAGE DeriveAnyClass #-}

{- |
Module      : Servant.API.Routes.Internal.Param
Copyright   : (c) Frederick Pringle, 2025
License     : BSD-3-Clause
Maintainer  : freddyjepringle@gmail.com

Internal module, subject to change.
-}
module Servant.API.Routes.Internal.Param
  ( Param (..)
  )
where

import Data.Aeson
import Data.Function (on)
import GHC.Generics
import qualified Servant.Links as S

{- | Newtype wrapper around servant's 'S.Param' so we can define a sensible
'Eq' instance for it.
-}
newtype Param = Param
  { Param -> Param
unParam :: S.Param
  }
  deriving (Int -> Param -> ShowS
[Param] -> ShowS
Param -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Param] -> ShowS
$cshowList :: [Param] -> ShowS
show :: Param -> String
$cshow :: Param -> String
showsPrec :: Int -> Param -> ShowS
$cshowsPrec :: Int -> Param -> ShowS
Show) via S.Param

instance Eq Param where
  == :: Param -> Param -> Bool
(==) = Param -> Param -> Bool
eq forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Param -> Param
unParam
    where
      S.SingleParam String
name1 Text
rep1 eq :: Param -> Param -> Bool
`eq` S.SingleParam String
name2 Text
rep2 =
        String
name1 forall a. Eq a => a -> a -> Bool
== String
name2 Bool -> Bool -> Bool
&& Text
rep1 forall a. Eq a => a -> a -> Bool
== Text
rep2
      S.ArrayElemParam String
name1 Text
rep1 `eq` S.ArrayElemParam String
name2 Text
rep2 =
        String
name1 forall a. Eq a => a -> a -> Bool
== String
name2 Bool -> Bool -> Bool
&& Text
rep1 forall a. Eq a => a -> a -> Bool
== Text
rep2
      S.FlagParam String
name1 `eq` S.FlagParam String
name2 =
        String
name1 forall a. Eq a => a -> a -> Bool
== String
name2
      Param
_ `eq` Param
_ = Bool
False

instance Ord Param where
  compare :: Param -> Param -> Ordering
compare = Param -> Param -> Ordering
comp forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Param -> Param
unParam
    where
      S.SingleParam String
name1 Text
rep1 comp :: Param -> Param -> Ordering
`comp` S.SingleParam String
name2 Text
rep2 =
        String
name1 forall a. Ord a => a -> a -> Ordering
`compare` String
name2 forall a. Semigroup a => a -> a -> a
<> Text
rep1 forall a. Ord a => a -> a -> Ordering
`compare` Text
rep2
      S.ArrayElemParam String
name1 Text
rep1 `comp` S.ArrayElemParam String
name2 Text
rep2 =
        String
name1 forall a. Ord a => a -> a -> Ordering
`compare` String
name2 forall a. Semigroup a => a -> a -> a
<> Text
rep1 forall a. Ord a => a -> a -> Ordering
`compare` Text
rep2
      S.FlagParam String
name1 `comp` S.FlagParam String
name2 =
        String
name1 forall a. Ord a => a -> a -> Ordering
`compare` String
name2
      S.SingleParam {} `comp` Param
_ = Ordering
LT
      Param
_ `comp` S.SingleParam {} = Ordering
LT
      S.ArrayElemParam {} `comp` Param
_ = Ordering
LT
      Param
_ `comp` S.ArrayElemParam {} = Ordering
LT

data ParamType
  = SingleParam
  | ArrayElemParam
  | FlagParam
  deriving (Int -> ParamType -> ShowS
[ParamType] -> ShowS
ParamType -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParamType] -> ShowS
$cshowList :: [ParamType] -> ShowS
show :: ParamType -> String
$cshow :: ParamType -> String
showsPrec :: Int -> ParamType -> ShowS
$cshowsPrec :: Int -> ParamType -> ShowS
Show, ParamType -> ParamType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ParamType -> ParamType -> Bool
$c/= :: ParamType -> ParamType -> Bool
== :: ParamType -> ParamType -> Bool
$c== :: ParamType -> ParamType -> Bool
Eq, Int -> ParamType
ParamType -> Int
ParamType -> [ParamType]
ParamType -> ParamType
ParamType -> ParamType -> [ParamType]
ParamType -> ParamType -> ParamType -> [ParamType]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: ParamType -> ParamType -> ParamType -> [ParamType]
$cenumFromThenTo :: ParamType -> ParamType -> ParamType -> [ParamType]
enumFromTo :: ParamType -> ParamType -> [ParamType]
$cenumFromTo :: ParamType -> ParamType -> [ParamType]
enumFromThen :: ParamType -> ParamType -> [ParamType]
$cenumFromThen :: ParamType -> ParamType -> [ParamType]
enumFrom :: ParamType -> [ParamType]
$cenumFrom :: ParamType -> [ParamType]
fromEnum :: ParamType -> Int
$cfromEnum :: ParamType -> Int
toEnum :: Int -> ParamType
$ctoEnum :: Int -> ParamType
pred :: ParamType -> ParamType
$cpred :: ParamType -> ParamType
succ :: ParamType -> ParamType
$csucc :: ParamType -> ParamType
Enum, ParamType
forall a. a -> a -> Bounded a
maxBound :: ParamType
$cmaxBound :: ParamType
minBound :: ParamType
$cminBound :: ParamType
Bounded, forall x. Rep ParamType x -> ParamType
forall x. ParamType -> Rep ParamType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ParamType x -> ParamType
$cfrom :: forall x. ParamType -> Rep ParamType x
Generic)
  deriving ([ParamType] -> Encoding
[ParamType] -> Value
ParamType -> Encoding
ParamType -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [ParamType] -> Encoding
$ctoEncodingList :: [ParamType] -> Encoding
toJSONList :: [ParamType] -> Value
$ctoJSONList :: [ParamType] -> Value
toEncoding :: ParamType -> Encoding
$ctoEncoding :: ParamType -> Encoding
toJSON :: ParamType -> Value
$ctoJSON :: ParamType -> Value
ToJSON)

instance ToJSON Param where
  toJSON :: Param -> Value
toJSON (Param Param
p) =
    [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$ case Param
p of
      S.SingleParam String
name Text
rep ->
        forall {a} {v}. (KeyValue a, ToJSON v) => v -> [a] -> [a]
withType ParamType
SingleParam [Key
"name" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= String
name, Key
"param_type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
rep]
      S.ArrayElemParam String
name Text
rep ->
        forall {a} {v}. (KeyValue a, ToJSON v) => v -> [a] -> [a]
withType ParamType
ArrayElemParam [Key
"name" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= String
name, Key
"param_type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
rep]
      S.FlagParam String
name ->
        forall {a} {v}. (KeyValue a, ToJSON v) => v -> [a] -> [a]
withType ParamType
FlagParam [Key
"name" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= String
name]
    where
      withType :: v -> [a] -> [a]
withType v
t [a]
ps = (Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= v
t) forall a. a -> [a] -> [a]
: [a]
ps